Skip to content

Instantly share code, notes, and snippets.

View Chocksy's full-sized avatar
🏠
Working from home

Ciocanel Razvan Chocksy

🏠
Working from home
View GitHub Profile
@Chocksy
Chocksy / mongo_to_sql.rb
Created January 6, 2015 10:06
Transfer MongoDB collection to SQL
# Taken from a rails app using mongoid and activrecord at the same time.
def transfer_mongo_to_sql(collection_name, fields=[])
ic = Mongoid.default_session.collections.select { |c| c.name==collection_name }.first
total_items = ic.find.count
per_batch = 1000
0.step(total_items, per_batch) do |offset|
to_fields = fields.map { |c| c[:to_name] }.join(', ')
insert_raw_sql = "INSERT INTO #{collection_name} (#{to_fields}) VALUES "
before = Time.now
@Chocksy
Chocksy / track_spec.rb
Created July 13, 2016 17:51
Temporary disable verify_partial_doubles
it 'tracks and event', verify_partial_doubles: false do
expect_any_instance_of(Analytics).to receive(:track) do |_o, options|
expect(options).to include(event: 'invited user')
expect(options[:properties]).to include(organization: organization.name)
end
post_with owner, :create_invites, params
end
@Chocksy
Chocksy / keybase.md
Created May 12, 2017 07:48
Keybase key

Keybase proof

I hereby claim:

  • I am chocksy on github.
  • I am chocksy (https://keybase.io/chocksy) on keybase.
  • I have a public key ASBvEeMzFwcVcTJuReETgNhjWE-ZwPxiU5Nl0hd1ewS8Two

To claim this, I am signing this object:

@Chocksy
Chocksy / precompile_assets.rb
Created September 7, 2016 11:40
Precompile js files in rails
# use this file in the rails app root directory and run it with
# ruby precompile_assets.rb to see if there is any error in your
# js file assets by using Uglifier. This would save you from running
# rake assets:precompile and using production as env var.
require './config/environment'
JS_PATH = 'app/assets/javascripts/**/*.js'.freeze
Dir[JS_PATH].each do |file_name|
puts "\n#{file_name}"
puts Uglifier.compile(File.read(file_name))
@Chocksy
Chocksy / ActiveRecordPluckToHash.rb
Created January 21, 2019 18:44
Add .pluck_to_hash to ActiveRecord. The result will get the columns specified as keys for the hash and their values.
module ActiveRecordPluckToHash
def pluck_to_hash(*keys)
formatted_keys = keys.map do |k|
"'#{k.to_s.split(/\b.\b/i)[-1].strip}', #{k}"
end.join(', ')
pluck(Arel.sql("json_build_object(#{formatted_keys})")).map(&:with_indifferent_access)
end
end
@Chocksy
Chocksy / insecure-markdown.md
Last active October 17, 2019 11:17
Insecure markdown examples that should be tested.
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
@Chocksy
Chocksy / idea.properties
Last active February 18, 2020 11:24
Rubymine config options
# custom RubyMine properties
editor.zero.latency.typing=true
@Chocksy
Chocksy / db_migration.md
Last active March 3, 2020 12:53
Wordpress DB migration (via queries)

DB migration WP

  • export the wp_posts, wp_postmeta tables from the old DB.
  • import the exported data into 2 new tables wp_old_posts, wp_old_postmeta into the new DB.
  • change collation on the table from utf8mb4_unicode_ci to utf8mb4_unicode_520_ci
  • add to the current/new table a column old_id (to store the id of the row in the old table. In our case wp_posts)
  • make a similar query to this:
INSERT INTO wp_posts (old_id, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) 
SELECT * FROM wp_old_posts;
@Chocksy
Chocksy / .1details.md
Last active April 9, 2020 16:26
Gemfile local that will allow for special gems only on local env

Differences between team gemfile and your preferences

The solution for the above problem is to have a different gemfile that you run your rails app against. It can be easily done by adding some terminal aliases to use a different gemfile file. I made that in the current configuration and it works well.