Skip to content

Commit

Permalink
This Year filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mlt committed May 22, 2018
1 parent a3363a7 commit 02d5879
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/views/tabulatr/_tabulatr_filter_dialog.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
option value="this_week"= I18n.t("tabulatr.date_filter.this_week")
option value="last_7_days"= I18n.t("tabulatr.date_filter.last_7_days")
option value="this_month"= I18n.t("tabulatr.date_filter.this_month")
option value="this_year"= I18n.t("tabulatr.date_filter.this_year")
option value="last_30_days"= I18n.t("tabulatr.date_filter.last_30_days")
option value="from_to"= "#{I18n.t('tabulatr.date_filter.from')} - #{I18n.t('tabulatr.date_filter.to')}"
.row.from_to.hidden
Expand Down
3 changes: 3 additions & 0 deletions lib/tabulatr/data/filtering.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#--
# Copyright (c) 2010-2014 Peter Horn & Florian Thomas, metaminded UG
#
Expand Down Expand Up @@ -83,6 +84,8 @@ def apply_date_condition(n, cond)
when 'last_7_days' then date_in_between((today - 6.day).beginning_of_day, today.at_end_of_day, n)
when 'this_month' then date_in_between(today.at_beginning_of_month.beginning_of_day,
today.at_end_of_month.end_of_day, n)
when 'this_year' then date_in_between(today.at_beginning_of_year.beginning_of_day,
today.at_end_of_year.end_of_day, n)
when 'last_30_days' then date_in_between((today - 29.day).beginning_of_day, today.at_end_of_day, n)
when 'from_to' then date_in_between((Date.parse(cond['from']) rescue nil), (Date.parse(cond['to']) rescue nil), n)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tabulatr/generators/tabulatr/templates/tabulatr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ en:
this_week: This Week
last_7_days: Last 7 Days
this_month: This Month
this_year: This Year
last_30_days: Last 30 Days
from: From
to: To
Expand All @@ -39,6 +40,7 @@ de:
this_week: Diese Woche
last_7_days: Letzte 7 Tage
this_month: Dieser Monat
this_year: Dieses Jahr
last_30_days: Letzte 30 Tage
from: Von
to: Bis
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/config/locales/tabulatr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ en:
this_week: This Week
last_7_days: Last 7 Days
this_month: This Month
this_year: This Year
last_30_days: Last 30 Days
from: From
to: To
Expand All @@ -38,6 +39,7 @@ de:
this_week: Diese Woche
last_7_days: Letzte 7 Tage
this_month: Dieser Monat
this_year: Dieses Jahr
last_30_days: Letzte 30 Tage
from: Von
to: Bis
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/tabulatr/data/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def filters; []; end
@last_thirty_days = Product.create!(publish_at: DateTime.new(2013, 12, 3, 0, 0))
@outside_last_thirty_days = Product.create!(publish_at: DateTime.new(2013, 12, 2, 23, 59))
@this_month = Product.create!(publish_at: DateTime.new(2014, 1, 15, 0, 0))
@this_year = Product.create!(publish_at: DateTime.new(2014, 2, 15, 0, 0))
@next_year = Product.create!(publish_at: DateTime.new(2015, 1, 1, 12, 0))
allow(Date).to receive(:today).and_return(Date.new(2014,1,1))
end
Expand Down Expand Up @@ -68,6 +69,14 @@ def filters; []; end
expect(result.map(&:id).sort).to eq ([@today.id, @week_two.id, @this_month.id])
end

it "filters for 'this year'" do
fake_obj = double()
allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'}
@dummy.apply_date_condition(fake_obj, {'simple'=>'this_year'})
result = @dummy.instance_variable_get('@relation')
expect(result.map(&:id).sort).to eq ([@today.id, @week_two.id, @this_month.id, @this_year.id])
end

it "filters for 'last 30 days'" do
fake_obj = double()
allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'}
Expand Down

0 comments on commit 02d5879

Please sign in to comment.