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
|