Skip to content

Instantly share code, notes, and snippets.

@linjian
linjian / role_model.rb
Created March 22, 2013 04:14
Add scope with_role to Role Model
# Copy from easy_roles
# https://github.com/platform45/easy_roles/blob/cf9c5f6e5db46adf8c48cc1a2e43c83ece3c2f4f/lib/methods/bitmask.rb
#
# Put it to config/initializers/
module RoleModel
class << self
alias_method :origin_included, :included
end
def self.included(base)
@linjian
linjian / mongo_mapper_soft_delete.rb
Created July 19, 2012 08:14
MongoMapper Soft Delete
# lib/mongo_mapper_ext/mongo_mapper_soft_delete.rb
#
# Usage:
# class Site
# include MongoMapper::Document
#
# plugin MongoMapper::Plugins::DefaultScope
# plugin MongoMapper::Plugins::SoftDelete
#
# default_scope :deleted_at => nil
@linjian
linjian / deferred_garbage_collection_all_in_one.rb
Created July 19, 2012 08:09
Deferred Garbage Collection All in One
# http://ariejan.net/2011/09/24/rspec-speed-up-by-tweaking-ruby-garbage-collection
#
# Usage:
# DEFER_GC=10 rspec spec/
# DEFER_GC=10 cucumber features/
#
# put it to spec/support/deferred_garbage_collection_all_in_one.rb
# or feature/support/hooks.rb
class DeferredGarbageCollection
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || -1).to_f
@linjian
linjian / mongo_mapper_default_scope.rb
Created July 19, 2012 07:56
MongoMapper Default Scope
# lib/mongo_mapper_ext/mongo_mapper_default_scope.rb
#
# Copy from rails-ext-0.3.29/lib/mongo_mapper_ext/plugins/default_scope.rb
# See http://rubygems.org/gems/rails-ext
#
# The implementation has been modified based on origin version:
# * Add query method to replace methods find_one, find_many, count.
# * Remove dependence on ruby-ext.
# * Support dynamic methods xxx_with_exclusive_scope.
#
@linjian
linjian / init_variables_for_rails_console_1.rb
Created July 19, 2012 07:44
Initialize Variables for Rails Console
# Solution 1:
# Puts it in ~/.irbrc
# then type '__init_vars_by_binding(binding).call' after rails console loaded.
if defined? ::Rails
def __init_vars_by_binding(binding)
Proc.new do
__quiet_name_error { binding.eval("user = User.first(:email => 'linjian815@gmail.com')") }
__quiet_name_error { binding.eval("account = user.account") }
end
end
@linjian
linjian / puts_to_log.rb
Created July 19, 2012 07:35
Puts to Log
# Add the following line to config/environments/development.rb or config/initializers/
# So I can use puts or p to print debug message to log file when I use nginx, which has no way to see STDOUT directly.
if Rails.env.development? && $PROGRAM_NAME !~ /script\/rails/ && $PROGRAM_NAME !~ /rake/
STDOUT.reopen(File.open("#{Rails.root}/log/development.log", 'a+'))
end
@linjian
linjian / without_callback.rb
Created July 19, 2012 07:34
Without Callback
# Put it in config/initializers/without_callback.rb
#
# Usage:
# Site.without_callback(:destroy, :after, :decrease_sites_count) do
# ...
# end
module ActiveSupport::Callbacks::ClassMethods
def without_callback(*args, &block)
skip_callback(*args)
yield
@linjian
linjian / get_routes_in_console.rb
Created July 19, 2012 07:31
Get Rails Routes in Console
# Copy from railties-3.0.10/lib/rails/tasks/routes.rake
#
# Put it to .irbrc
# Call it in console as:
# __routes # => all routes
# __routes('pause') # => all routes matched "pause"
# __routes('pause|resume') # => all routes matched "pause" or "resume"
# __routes(nil, 'sites') # => all routes of sites controller
# __routes('user', nil, true) # => reload routes, and show all routes matched "user"
def __routes(filter = nil, controller = nil, reload = false)
@linjian
linjian / one_association_stub.rb
Created July 19, 2012 07:23
Fix One Association Stub
# Put it in spec/support/one_association_stub.rb
module OneAssociationStub
def stub(*args, &block)
if is_mongo_mapper_model? && is_one_association? && self.respond_to?(:target)
self.target.stub(*args, &block)
else
super
end
end