Module: ActiveRecord::TimeScope::ClassMethods

Defined in:
lib/active_record/time_scope.rb

Instance Method Summary collapse

Instance Method Details

#create_time_range_scope(verb, from_name, to_name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/active_record/time_scope.rb', line 32

def create_time_range_scope(verb, from_name, to_name)
  model = self
  from_name = "#{table_name}.#{from_name}"
  to_name = "#{table_name}.#{to_name}"
  model.scope "#{verb}_before", ->(time, opts = {}){ TimeRangeProxy.new(model, from_name, to_name).before(time, opts) }
  scope "#{verb}_after", ->(time, opts = {}){ TimeRangeProxy.new(model, from_name, to_name).after(time, opts) }
  scope "#{verb}_within", ->(from, to, from_opts = {}, to_opts = {}){ TimeRangeProxy.new(model, from_name, to_name).within(from, to, from_opts, to_opts) }
end

#create_time_scope(verb, name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_record/time_scope.rb', line 24

def create_time_scope(verb, name)
  model = self
  name = "#{table_name}.#{name}"
  scope "#{verb}_before", ->(time, opts = {}){ TimeProxy.new(model, name).before(time, opts) }
  scope "#{verb}_after", ->(time, opts = {}){ TimeProxy.new(model, name).after(time, opts) }
  scope "#{verb}_within", ->(from, to, from_opts = {}, to_opts = {}){ TimeProxy.new(model, name).within(from, to, from_opts, to_opts) }
end

#inherited(subclass) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/active_record/time_scope.rb', line 13

def inherited(subclass)
  super
  if subclass.table_exists?
    subclass.column_names.each do |cn|
      verb = cn.sub TIME_POSTFIX_REGEXP, ''
      next if verb == cn
      subclass.create_time_scope verb, cn
    end
  end
end