Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #4922 #4925

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base
helper_method :current_role

rescue_from ActiveRecord::RecordNotFound, with: :not_found!
rescue_from Errors::InvalidDateRange, with: :invalid_date

def current_organization
return @current_organization if @current_organization
Expand Down Expand Up @@ -114,6 +115,11 @@ def swaddled
response.headers["swaddled-by"] = "rubyforgood"
end

def invalid_date(date_value)
flash[:error] = "Date range '#{date_value}' not properly formatted. Filters reset."
redirect_to controller: params["controller"], action: params["action"]
end

protected

def setup_date_range_picker
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/date_range_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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}"
raise Errors::InvalidDateRange.new(date_range_params)
end
end

Expand Down
9 changes: 9 additions & 0 deletions app/models/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ def message
"Could not complete action: inventory already has items stored"
end
end

class InvalidDateRange < StandardError
attr_reader :invalid_date

def initialize(invalid_date)
super
@invalid_date = invalid_date
end
end
end
18 changes: 18 additions & 0 deletions spec/helpers/date_range_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.describe DateRangeHelper, type: :helper do
context "when parsing date range params" do
it "should return the default date value" do
expect(date_range_params).to eq(default_date)
end
end

context "when creating a date interval using selected_interval" do
it "should return the correct date interval using the default date" do
expect(selected_interval).to eq([2.months.ago.to_date, 1.month.from_now.to_date])
end

it "should raise Errors:InvalidDateRange when input is invalid" do
allow_any_instance_of(DateRangeHelper).to receive(:default_date).and_return("nov 08 to feb 08")
expect { selected_interval }.to raise_error(Errors::InvalidDateRange, "nov 08 to feb 08")
end
end
end
Loading