Skip to content

Instantly share code, notes, and snippets.

@ciscou
Created March 25, 2014 12:03
Show Gist options
  • Save ciscou/9760422 to your computer and use it in GitHub Desktop.
Save ciscou/9760422 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
def self.scope(name, callable)
define_singleton_method name do
options = callable.respond_to?(:call) ? callable.call : callable
puts "Fetching records with options #{options.inspect}"
end
end
end
end
class MyModel < ActiveRecord::Base
scope :active1, { "published_at.lt" => Time.now }
scope :active2, -> { puts 2 ; { "published_at.lt" => Time.now } }
end
MyModel.active1
sleep 1
MyModel.active1
sleep 1
MyModel.active1
sleep 1
MyModel.active1
sleep 1
MyModel.active2
sleep 1
MyModel.active2
sleep 1
MyModel.active2
sleep 1
MyModel.active2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment