Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created July 6, 2012 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miyagawa/3057813 to your computer and use it in GitHub Desktop.
Save miyagawa/3057813 to your computer and use it in GitHub Desktop.
require 'timecop'
module ActiveRecordDetectTimeInScopes
extend ActiveSupport::Concern
module ClassMethods
def scope(*args)
scope_caller = caller
Timecop.travel(Time.now) do
Timecop.top_stack_item.define_singleton_method :time do
warn "WARNING: You have a scope with Time evaluated immediately in its args. " +
"You might want to use lambda.\n" +
Rails.backtrace_cleaner.clean(scope_caller).map { |trace| "\t#{trace}" }.join("\n")
super()
end
super
end
end
end
end
ActiveRecord::Base.send(:include, ActiveRecordDetectTimeInScopes)
@miyagawa
Copy link
Author

miyagawa commented Jul 6, 2012

Crap it won't work - needs to hook before the scope args are evaluated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment