Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def setup_date_range_picker
@selected_date_interval = helpers.selected_interval
@selected_date_range = helpers.selected_interval.map { |d| d.to_fs(:long) }.join(" - ")
@selected_date_range_label = helpers.date_range_label

if helpers.date_range_params_invalid?
flash.now[:error] = "The date range you supplied was invalid, so we used a default range instead."
end
end

def configure_permitted_parameters
Expand Down
20 changes: 15 additions & 5 deletions app/helpers/date_range_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ def default_date
"#{start_date.strftime("%B %d, %Y")} - #{end_date.strftime("%B %d, %Y")}"
end

def parse_date_range(date_range_string)
parts = date_range_string.to_s.split(" - ")
return nil unless parts.size == 2

parts.map { |d| Date.strptime(d.strip, "%B %d, %Y") }
rescue ArgumentError => e
Rails.logger.warn("Invalid date range '#{date_range_string}': #{e.message}")
nil
end

def selected_interval
date_range_params.split(" - ").map do |d|
Date.strptime(d, "%B %d, %Y")
rescue
raise "Invalid date: #{d} in #{date_range_params}"
end
parse_date_range(date_range_params) || parse_date_range(default_date)
end

def selected_range
Expand All @@ -56,4 +62,8 @@ def selected_range_described
"during the period #{start_date.to_fs(:short)} to #{end_date.to_fs(:short)}"
end
end

def date_range_params_invalid?
parse_date_range(date_range_params).nil?
end
end
9 changes: 9 additions & 0 deletions spec/requests/adjustments_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
end
end
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get adjustments_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end
end

context "csv" do
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
expect(response).to be_successful
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get distributions_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end

it "sums distribution totals accurately" do
create(:distribution, :with_items, item_quantity: 5, organization: organization)
create(:line_item, :distribution, itemizable_id: distribution.id, quantity: 7)
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
expect(subject.body).to_not include("<td>#{full_comment}</td>")
end
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get donations_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end
end

context "csv" do
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/events_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@
expect(response.body).to include("88<br>")
expect(response.body).not_to include("99<br>")
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get events_path(format: :html, params: {
filters: {date_range: "Foo 10, 2025 - Bar 20, 2025"}
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end
end

context "with eventable_id" do
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/product_drives_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
let(:index_path) { product_drives_path(params) }
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get product_drives_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end

context "csv" do
it 'is successful' do
get product_drives_path(format: :csv)
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/purchases_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
expect(subject.body).to include("Purchase Comment")
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get purchases_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end

context "with multiple purchases" do
let!(:storage_location) { create(:storage_location, organization: organization) }
let(:vendor) { create(:vendor, organization: organization) }
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/requests_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
let(:response_format) { 'html' }

it { is_expected.to be_successful }

it "falls back to default date range and renders without error when given an invalid date_range param" do
get requests_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end
end

context "csv" do
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/transfers_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
get transfers_path(filters: { date_range: "#{start_date} - #{end_date}" })
expect(assigns(:transfers)).to eq([new_transfer])
end

it "falls back to default date range and renders without error when given an invalid date_range param" do
get transfers_path(format: :html, params: {
filters: { date_range: "Foo 10, 2025 - Bar 20, 2025" }
})

expect(response).to be_successful
expect(response.body).to include("The date range you supplied was invalid")
end
end

context 'when date parameters are not supplied' do
Expand Down