Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
class ChangePrimaryKeyTypeOnContentUnit < ActiveRecord::Migration
def change
change_column :content_units, :guoid, :integer, limit: 8
end
end
@amitpatelx
amitpatelx / create_content_units.rb
Created May 23, 2016 10:02
Override default primary key Raw - Solution
class CreateContentUnits < ActiveRecord::Migration
def up
create_table :content_units, id: false do |t|
t.boolean :active, default: false
end
execute 'ALTER TABLE content_units ADD guoid BIGINT(20) PRIMARY KEY AUTO_INCREMENT FIRST;'
end
def down
drop_table :guoids
@amitpatelx
amitpatelx / create_content_units.rb
Created May 23, 2016 11:01
Override default primary key - Using a Gem
class CreateContentUnits < ActiveRecord::Migration
def change
create_table :content_units, id: false do |t|
t.integer :guoid, limit: 8, auto_increment: true, primary_key: true
t.boolean :active, default: false
end
end
end
class ContentUnit < ActiveRecord::Base
self.primary_key = :guoid
end
@amitpatelx
amitpatelx / create_content_units.rb
Last active May 25, 2016 10:53
How to increase primary key column size(type) using Rails migration with MySQL?
class CreateContentUnits < ActiveRecord::Migration
def up
create_table :content_units do |t|
t.string :name, null: false
t.boolean :active, default: false
end
execute('ALTER TABLE `content_units` CHANGE `id` `id` BIGINT(20) NOT NULL AUTO_INCREMENT')
end
def down
@amitpatelx
amitpatelx / content_unit.json
Created June 17, 2016 11:43
/api/content_unit.json?id=1
{
"id": 1,
"active": null,
"visibility": null,
"created_at": "2016-06-17T04:31:44.000Z",
"title_field": null,
"owner_id": 1,
"owner_type": "Project",
"content": [
{
@amitpatelx
amitpatelx / 2.3.0
Last active June 22, 2016 10:49
Comparing Array#max time between Ruby 2.4.0-preview1 and 2.3.0
2.3.0 :001 > require 'benchmark'
=> true
2.3.0 :002 > x, y = 1, 2;
2.3.0 :003 > puts Benchmark.measure { 10000000.times { [x, y].max } }
3.300000 0.000000 3.300000 ( 3.301698)
=> nil
@amitpatelx
amitpatelx / download_image.rb
Created June 24, 2016 14:14
How to download images from different rails application sharing a session?
# This code is assumed to be part of one application(http://microservices2.btdashboard.com) sharing session with other(http://microservices1.btdashboard.com)
# '_your_app_session' is session_store key config in session_store.rb
# 'KzBxYy9jUURwbEYxR3ZCRmI3' is value of session cookie read in controller action
cookie = Mechanize::Cookie.new('_your_app_session', 'KzBxYy9jUURwbEYxR3ZCRmI3')
cookie.domain = ".btdashboard.com" # check domain config in session_store.rb
cookie.path='/' # Set as per your application settings
# More cookie settins as required
uri = URI.parse('http://microservices1.btdashboard.com')
@amitpatelx
amitpatelx / camscan.rb
Created July 18, 2016 12:09
Scan for virus
# scan file on disk
options = {location: '/path/to/file'}
# initiate scan - returns ClamScan::Response object
respone = ClamScan::Client.scan(options)
# check output from clamscan
puts response.body
# check response status
@amitpatelx
amitpatelx / clamscan_initializer.rb
Created July 18, 2016 12:11
ClamScan Initializer to remove infected file
ClamScan.configure do |config|
config.default_scan_options.merge {remove: 'yes'}
end