Module: LessSimpleDateScopes::ActiveRecord::ClassMethods
- Defined in:
- lib/less_simple_date_scopes/active_record.rb
Instance Method Summary collapse
- #simple_date_scopes ⇒ Object
-
#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.
Instance Method Details
#simple_date_scopes ⇒ Object
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.
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, = {}) table_name = .delete(:table_name) || self.table_name prefix = .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, ) } named_scope acts_as_date_scope_name(field, :today, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :day, ) } named_scope acts_as_date_scope_name(field, :tomorrow, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.day, field, :day, ) } named_scope acts_as_date_scope_name(field, :in_week_of, prefix), lambda {|date| in_x_of(table_name, date, field, :week, ) } named_scope acts_as_date_scope_name(field, :last_week, prefix), lambda { in_x_of(table_name, Time.zone.now - 7.days, field, :week, ) } named_scope acts_as_date_scope_name(field, :this_week, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :week, ) } named_scope acts_as_date_scope_name(field, :next_week, prefix), lambda { in_x_of(table_name, Time.zone.now + 7.days, field, :week, ) } named_scope acts_as_date_scope_name(field, :in_month_of, prefix), lambda {|date| in_x_of(table_name, date, field, :month, ) } named_scope acts_as_date_scope_name(field, :last_month, prefix), lambda { in_x_of(table_name, Time.zone.now - 1.month, field, :month, ) } named_scope acts_as_date_scope_name(field, :this_month, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :month, ) } named_scope acts_as_date_scope_name(field, :next_month, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.month, field, :month, ) } named_scope acts_as_date_scope_name(field, :in_year_of, prefix), lambda {|date| in_x_of(table_name, date, field, :year, ) } named_scope acts_as_date_scope_name(field, :last_year, prefix), lambda { in_x_of(table_name, Time.zone.now - 1.year, field, :year, ) } named_scope acts_as_date_scope_name(field, :this_year, prefix), lambda { in_x_of(table_name, Time.zone.now, field, :year, ) } named_scope acts_as_date_scope_name(field, :next_year, prefix), lambda { in_x_of(table_name, Time.zone.now + 1.year, field, :year, ) } 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 .merge({:conditions => {table_name => {field => (start_date..end_date)}}}) } end |