Module: ArelDateScopes::ActiveRecord::ClassMethods

Defined in:
lib/arel_date_scopes/active_record.rb

Instance Method Summary collapse

Instance Method Details

#all_column(field = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/arel_date_scopes/active_record.rb', line 45

def all_column(field = nil)
  rows = scoped.all
  return [] if rows.empty?
  field ||= rows.first.attributes.keys.first
  rows.map { |row| row[field] }
end

#date_scopes_for(field) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/arel_date_scopes/active_record.rb', line 10

def date_scopes_for(field)
  scope :"#{field}_year_eq", lambda { |year|
    t = arel_table
    where(t[field].year.eq(year))
  }
  
  scope :"#{field}_month_eq", lambda { |month|
    t = arel_table
    where(t[field].month.eq(month))
  }

  scope :"#{field}_day_eq", lambda { |day|
    t = arel_table
    where(t[field].dayofmonth.eq(day))
  }
  
  scope :"#{field}_years", lambda {
    t = arel_table
    select(t[field].year.as("#{field}_year")).group(t[field].year)
  }

  scope :"#{field}_months", lambda {
    t = arel_table
    select(t[field].month.as("#{field}_month")).group(t[field].month)
  }

  scope :"#{field}_days", lambda {
    t = arel_table
    select(t[field].dayofmonth.as("#{field}_day")).group(t[field].dayofmonth)
  }
  
  scope :"descend_by_#{field}", order("#{field} DESC")
  scope :"ascend_by_#{field}", order("#{field} ASC")        
end