Skip to content

Instantly share code, notes, and snippets.

@dira
dira / rename_mongoid_collection.rb
Created March 28, 2012 10:14
Rename a Mongoid collection
# want to nest `Video` under `Media`; had a `videos` collection
# rename the collection:
Mongoid.database.drop_collection('videos')
Mongoid.database.rename_collection('videos', 'media')
# or
Mongoid.database.collection('videos').rename('media')
# change the type of all the existing records
@dira
dira / omniauth.rb
Created December 1, 2010 01:56
OmniAuth strategy for a custom provider
# config/initializers/omniauth.rb
module OmniAuth
module Strategies
# tell OmniAuth to load our strategy
autoload :Pixelation, 'lib/pixelation_strategy'
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "app_name", "secret"
@dira
dira / gist:d172776adaef81876df7b519fe32b64c
Last active January 28, 2020 09:32
Convert a pdf (table with text) to very readable jpgs, one per pdf page
# install gs (Ghostscript)
# install ImageMagick
# add to ~/.bash_profile:
pdf_to_jpg () {
filename=${1:-5}
precision=${2:-1}
convert -density 150 $filename -background white -alpha background -alpha off +adjoin -quality 100 -sharpen 0x1.0 "${filename%.*}-%0${precision}d.jpg"
}
kill -s sigusr2 `ps -aux | awk '/puma/ {print $2}' | awk 'NR==1'`
@dira
dira / .bash_profile
Created November 27, 2018 11:10
Git flow: bash commands for quick deploy
alias get_in_master='git checkout develop && git pull && git checkout master && git pull && git merge --no-edit develop'
alias push_it='git push && git checkout develop'
@dira
dira / reflect_on_associations.rb
Created January 9, 2018 14:03
Rails - use reflection to get all declared ActiveRecord associations between entities
Rails.application.eager_load!
data = ActiveRecord::Base.descendants.map{|klass| [klass, klass.reflections.map{|k, v| [v.class.name, k]}] }
data.map{|klass, relations| relations.map{|k, v| "#{klass} #{k.gsub('ActiveRecord::Reflection::', '').gsub('Reflection', '').underscore.humanize.downcase} #{v}"} }.flatten
@dira
dira / ProxyForCharles.rb
Created January 26, 2011 23:08
How to set the proxy for testing HTTParty requests with Charles
http_proxy '10.2.2.1', 8888
# the ones inspired by the documentation do not work
# http_proxy 'http://10.2.2.1', 8888
# http_proxy 'http://localhost', 8888
# http_proxy 'http://127.0.0.1', 8888
class LegacyInspector
def initialize(should_have_data_newer_than=nil)
@should_have_data_newer_than = should_have_data_newer_than || 6.months.ago
end
def connection
@connection ||= ActiveRecord::Base.connection;
end
def tables
@dira
dira / count_todos.bash
Created July 3, 2017 08:41
Count how many #TODO you have in a (Rails) app
ack TODO app | grep TODO | wc -l; ack TODO lib | grep TODO | wc -l; ack TODO spec | grep TODO | wc -l
@dira
dira / gist:7102780
Last active December 26, 2015 05:48
refer to an object's constructor in coffeescript
object.constructor