Module: LessSimpleDateScopes::ActiveRecord::ClassMethods

Defined in:
lib/less_simple_date_scopes/active_record.rb

Instance Method Summary collapse

Instance Method Details

#simple_date_scopesObject



62
63
64
# File 'lib/less_simple_date_scopes/active_record.rb', line 62

def simple_date_scopes
  simple_date_scopes_on DEFAULT_FIELD
end

#simple_date_scopes_on(field, options = {}) ⇒ Object

Generates some named scopes in the current model which allow easy date based access on records for often needed tasks.

Parameters:

  • The table column (date or datetime) the scopes will be running on

  • (defaults to: {})

    Options to customize the generated scopes

Options Hash (options):

  • :table_name (String) — default: ActiveRecord::Base.table_name

    The table name the scopes will be running on. This is useful if the scope is used to filter based on an association rather than on the model’s own table

  • :prefix (String) — default: nil

    An optional prefix for the generated scope name. Useful if there are generated scopes for two tables which share the same column name (e.g. generated_at)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/less_simple_date_scopes/active_record.rb', line 31

def simple_date_scopes_on(field, options = {})
  table_name = options.delete(:table_name) || self.table_name
  prefix     = options.delete(:prefix)

  named_scope acts_as_date_scope_name(field, :yesterday, prefix),   lambda { in_x_of(table_name, Time.zone.now - 1.day, field, :day, options) }
  named_scope acts_as_date_scope_name(field, :today, prefix),       lambda { in_x_of(table_name, Time.zone.now, field, :day, options) }
  named_scope acts_as_date_scope_name(field, :tomorrow, prefix),    lambda { in_x_of(table_name, Time.zone.now + 1.day, field, :day, options) }

  named_scope acts_as_date_scope_name(field, :in_week_of, prefix),  lambda {|date| in_x_of(table_name, date, field, :week, options) }
  named_scope acts_as_date_scope_name(field, :last_week, prefix),   lambda { in_x_of(table_name, Time.zone.now - 7.days, field, :week, options) }
  named_scope acts_as_date_scope_name(field, :this_week, prefix),   lambda { in_x_of(table_name, Time.zone.now, field, :week, options) }
  named_scope acts_as_date_scope_name(field, :next_week, prefix),   lambda { in_x_of(table_name, Time.zone.now + 7.days, field, :week, options) }

  named_scope acts_as_date_scope_name(field, :in_month_of, prefix), lambda {|date| in_x_of(table_name, date, field, :month, options) }
  named_scope acts_as_date_scope_name(field, :last_month, prefix),  lambda { in_x_of(table_name, Time.zone.now - 1.month, field, :month, options) }
  named_scope acts_as_date_scope_name(field, :this_month, prefix),  lambda { in_x_of(table_name, Time.zone.now, field, :month, options) }
  named_scope acts_as_date_scope_name(field, :next_month, prefix),  lambda { in_x_of(table_name, Time.zone.now + 1.month, field, :month, options) }

  named_scope acts_as_date_scope_name(field, :in_year_of, prefix),  lambda {|date| in_x_of(table_name, date, field, :year, options) }
  named_scope acts_as_date_scope_name(field, :last_year, prefix),   lambda { in_x_of(table_name, Time.zone.now - 1.year, field, :year, options) }
  named_scope acts_as_date_scope_name(field, :this_year, prefix),   lambda { in_x_of(table_name, Time.zone.now, field, :year, options) }
  named_scope acts_as_date_scope_name(field, :next_year, prefix),   lambda { in_x_of(table_name, Time.zone.now + 1.year, field, :year, options) }

  named_scope acts_as_date_scope_name(field, :between, prefix),
              lambda {|s, e|
                start_date = (s.is_a?(String) ? Time.zone.parse(s) : s).beginning_of_day
                end_date   = (e.is_a?(String) ? Time.zone.parse(e) : e).end_of_day
                options.merge({:conditions => {table_name => {field => (start_date..end_date)}}})
              }
end