Skip to content

Instantly share code, notes, and snippets.

linjian

Block or report user

Report or block linjian

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@linjian
linjian / role_model.rb
Created Mar 22, 2013
Add scope with_role to Role Model
View role_model.rb
# 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)
View mongo_mapper_soft_delete.rb
# 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 Jul 19, 2012
Deferred Garbage Collection All in One
View deferred_garbage_collection_all_in_one.rb
# 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
View mongo_mapper_default_scope.rb
# 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 Jul 19, 2012
Initialize Variables for Rails Console
View init_variables_for_rails_console_1.rb
# 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
View puts_to_log.rb
# 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
View without_callback.rb
# 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 Jul 19, 2012
Get Rails Routes in Console
View get_routes_in_console.rb
# 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 Jul 19, 2012
Fix One Association Stub
View one_association_stub.rb
# 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
You can’t perform that action at this time.