Skip to content

Commit 4fcf1e6

Browse files
Resolves #4740 - Add new default date range (#4752)
* add new default date method and update application.js * add test cases for default method in date_range_picker_shared_example * fix product_drive_system_spec to use updated default date
1 parent 417534d commit 4fcf1e6

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

app/helpers/date_range_helper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Encapsulates methods used on the Dashboard that need some business logic
22
module DateRangeHelper
33
def date_range_params
4-
params.dig(:filters, :date_range).presence || this_year
4+
params.dig(:filters, :date_range).presence || default_date
55
end
66

77
def date_range_label
@@ -23,8 +23,10 @@ def date_range_label
2323
end
2424
end
2525

26-
def this_year
27-
"January 1, #{Time.zone.today.year} - December 31, #{Time.zone.today.year}"
26+
def default_date
27+
start_date = 2.months.ago.to_date
28+
end_date = 1.month.from_now.to_date
29+
"#{start_date.strftime("%B %d, %Y")} - #{end_date.strftime("%B %d, %Y")}"
2830
end
2931

3032
def selected_interval

app/javascript/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ $(document).ready(function(){
9090
format: "MMMM D, YYYY",
9191
ranges: {
9292
customRanges: {
93+
'Default': [today.minus({'months': 2}).toJSDate(), today.plus({'months': 1}).toJSDate()],
9394
'All Time': [today.minus({ 'years': 100 }).toJSDate(), today.plus({ 'years': 1 }).toJSDate()],
9495
'Today': [today.toJSDate(), today.toJSDate()],
9596
'Yesterday': [today.minus({'days': 1}).toJSDate(), today.minus({'days': 1}).toJSDate()],

spec/support/date_range_picker_shared_example.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,30 @@ def date_range_picker_select_range(range_name)
2020
let(:user) { create(:user, organization: organization) }
2121

2222
let!(:very_old) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2000, 7, 31), :organization => organization) }
23+
let!(:two_months_ago) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 5, 31), :organization => organization) }
2324
let!(:recent) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 24), :organization => organization) }
2425
let!(:today) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 31), :organization => organization) }
26+
let!(:one_month_ahead) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 8, 31), :organization => organization) }
2527
let!(:one_year_ahead) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2020, 7, 31), :organization => organization) }
2628
let!(:two_years_ahead) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2021, 7, 31), :organization => organization) }
2729

30+
context "when choosing 'Default'" do
31+
before do
32+
sign_out user
33+
travel_to Time.zone.local(2019, 7, 31)
34+
sign_in user
35+
end
36+
37+
after do
38+
travel_back
39+
end
40+
41+
it "shows only 4 records" do
42+
visit subject
43+
expect(page).to have_css("table tbody tr", count: 4)
44+
end
45+
end
46+
2847
context "when choosing 'All Time'" do
2948
before do
3049
sign_out user
@@ -41,7 +60,7 @@ def date_range_picker_select_range(range_name)
4160
date_range = "#{Time.zone.local(1919, 7, 1).to_formatted_s(:date_picker)} - #{Time.zone.local(2020, 7, 31).to_formatted_s(:date_picker)}"
4261
fill_in "filters_date_range", with: date_range
4362
find(:id, 'filters_date_range').native.send_keys(:enter)
44-
expect(page).to have_css("table tbody tr", count: 4)
63+
expect(page).to have_css("table tbody tr", count: 6)
4564
end
4665
end
4766

spec/system/product_drive_system_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
it "Shows the expected filters with the expected values and in alphabetical order for name filter" do
3030
expect(page.find("select[name='filters[by_name]']").find(:xpath, 'option[2]').text).to eq "Alpha Test name 3"
3131
expect(page.has_select?('filters[by_name]', with_options: @product_drives.map(&:name))).to be true
32-
expect(page.has_field?('filters_date_range', with: this_year))
32+
expect(page.has_field?('filters_date_range', with: default_date))
3333
end
3434

3535
it "shows the expected product drives" do

0 commit comments

Comments
 (0)