lib/datetime-scopes/datetime_proxy.rb in datetime-scopes-1.0.0.alpha1 vs lib/datetime-scopes/datetime_proxy.rb in datetime-scopes-1.0.0.alpha2
- old
+ new
@@ -32,68 +32,123 @@
PROXY_METHODS
end
# Intervals
- def within(from, to, time_zone: @time_zone)
- @klass.where(
+ def within(rel, from, to, time_zone: @time_zone)
+ rel.where(
"#{@attribute} >= ? AND #{@attribute} <= ?",
from.in_time_zone(time_zone),
to.in_time_zone(time_zone)
)
end
- def within_days(from, to, time_zone: @time_zone)
+ def within_days(rel, from, to, time_zone: @time_zone)
within(
+ rel,
from.in_time_zone(time_zone).beginning_of_day,
to.in_time_zone(time_zone).end_of_day,
time_zone: time_zone
)
end
- def within_months(from, to, time_zone: @time_zone)
+ def within_months(rel, from, to, time_zone: @time_zone)
within(
+ rel,
from.in_time_zone(time_zone).beginning_of_month,
to.in_time_zone(time_zone).end_of_month,
time_zone: time_zone
)
end
- def within_years(from, to, time_zone: @time_zone)
+ def within_years(rel, from, to, time_zone: @time_zone)
within(
+ rel,
from.in_time_zone(time_zone).beginning_of_year,
to.in_time_zone(time_zone).end_of_year,
time_zone: time_zone
)
end
# Specific day/month/year
- def on_day(day, time_zone: @time_zone) ; within_days day, day, time_zone: time_zone ; end
- def on_month(month, time_zone: @time_zone) ; within_months month, month, time_zone: time_zone ; end
- def on_year(year, time_zone: @time_zone) ; within_years year, year, time_zone: time_zone ; end
+ def on_day(rel, day, time_zone: @time_zone)
+ within_days rel, day, day, time_zone: time_zone
+ end
+ def on_month(rel, month, time_zone: @time_zone)
+ within_months rel, month, month, time_zone: time_zone
+ end
+
+ def on_year(rel, year, time_zone: @time_zone)
+ within_years rel, year, year, time_zone: time_zone
+ end
+
# Strict equations
- def before(time, time_zone: @time_zone) ; @klass.where "#{@attribute} < ?", time.in_time_zone(time_zone) ; end
- def before_day(day, time_zone: @time_zone) ; before day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone ; end
- def before_month(month, time_zone: @time_zone) ; before month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
- def before_year(year, time_zone: @time_zone) ; before year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
+ def before(rel, time, time_zone: @time_zone)
+ rel.where "#{@attribute} < ?", time.in_time_zone(time_zone)
+ end
- def after(time, time_zone: @time_zone) ; @klass.where "#{@attribute} > ?", time.in_time_zone(time_zone) ; end
- def after_day(day, time_zone: @time_zone) ; after day.in_time_zone(time_zone).end_of_day, time_zone: time_zone ; end
- def after_month(month, time_zone: @time_zone) ; after month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
- def after_year(year, time_zone: @time_zone) ; after year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
+ def before_day(rel, day, time_zone: @time_zone)
+ before rel, day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone
+ end
+ def before_month(rel, month, time_zone: @time_zone)
+ before rel, month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone
+ end
+
+ def before_year(rel, year, time_zone: @time_zone)
+ before rel, year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone
+ end
+
+ def after(rel, time, time_zone: @time_zone)
+ rel.where "#{@attribute} > ?", time.in_time_zone(time_zone)
+ end
+
+ def after_day(rel, day, time_zone: @time_zone)
+ after rel, day.in_time_zone(time_zone).end_of_day, time_zone: time_zone
+ end
+
+ def after_month(rel, month, time_zone: @time_zone)
+ after rel, month.in_time_zone(time_zone).end_of_month, time_zone: time_zone
+ end
+
+ def after_year(rel, year, time_zone: @time_zone)
+ after rel, year.in_time_zone(time_zone).end_of_year, time_zone: time_zone
+ end
+
# Non-strict equations
- def on_or_before(time, time_zone: @time_zone) ; @klass.where "#{@attribute} <= ?", time.in_time_zone(time_zone) ; end
- def on_or_before_day(day, time_zone: @time_zone) ; on_or_before day.in_time_zone(time_zone).end_of_day, time_zone: time_zone ; end
- def on_or_before_month(month, time_zone: @time_zone) ; on_or_before month.in_time_zone(time_zone).end_of_month, time_zone: time_zone ; end
- def on_or_before_year(year, time_zone: @time_zone) ; on_or_before year.in_time_zone(time_zone).end_of_year, time_zone: time_zone ; end
+ def on_or_before(rel, time, time_zone: @time_zone)
+ rel.where "#{@attribute} <= ?", time.in_time_zone(time_zone)
+ end
- def on_or_after(time, time_zone: @time_zone) ; @klass.where "#{@attribute} >= ?", time.in_time_zone(time_zone) ; end
- def on_or_after_day(day, time_zone: @time_zone) ; on_or_after day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone ; end
- def on_or_after_month(month, time_zone: @time_zone) ; on_or_after month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone ; end
- def on_or_after_year(year, time_zone: @time_zone) ; on_or_after year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone ; end
+ def on_or_before_day(rel, day, time_zone: @time_zone)
+ on_or_before rel, day.in_time_zone(time_zone).end_of_day, time_zone: time_zone
+ end
+
+ def on_or_before_month(rel, month, time_zone: @time_zone)
+ on_or_before rel, month.in_time_zone(time_zone).end_of_month, time_zone: time_zone
+ end
+
+ def on_or_before_year(rel, year, time_zone: @time_zone)
+ on_or_before rel, year.in_time_zone(time_zone).end_of_year, time_zone: time_zone
+ end
+
+ def on_or_after(rel, time, time_zone: @time_zone)
+ rel.where "#{@attribute} >= ?", time.in_time_zone(time_zone)
+ end
+
+ def on_or_after_day(rel, day, time_zone: @time_zone)
+ on_or_after rel, day.in_time_zone(time_zone).beginning_of_day, time_zone: time_zone
+ end
+
+ def on_or_after_month(rel, month, time_zone: @time_zone)
+ on_or_after rel, month.in_time_zone(time_zone).beginning_of_month, time_zone: time_zone
+ end
+
+ def on_or_after_year(rel, year, time_zone: @time_zone)
+ on_or_after rel, year.in_time_zone(time_zone).beginning_of_year, time_zone: time_zone
+ end
end
end