From 800d3349f4c7d3f05f9243f99d110dd99842edbb Mon Sep 17 00:00:00 2001 From: Gabe Date: Tue, 14 Jan 2025 13:28:35 -0600 Subject: [PATCH 1/7] Handling Date::Error with redirect --- app/controllers/application_controller.rb | 8 ++++++++ app/helpers/date_range_helper.rb | 2 -- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e80333a9ee..db4d316e3c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base helper_method :current_role rescue_from ActiveRecord::RecordNotFound, with: :not_found! + rescue_from Date::Error, with: :invalid_date def current_organization return @current_organization if @current_organization @@ -111,10 +112,17 @@ def omgwtfbbq! end end + def swaddled response.headers["swaddled-by"] = "rubyforgood" end + def invalid_date + flash[:error] = "Date range not properly formatted." + params["filters"]["date_range"] = helpers.default_date + redirect_back fallback_location: root_path + end + protected def setup_date_range_picker diff --git a/app/helpers/date_range_helper.rb b/app/helpers/date_range_helper.rb index cc73af80d8..ce384e6ba8 100644 --- a/app/helpers/date_range_helper.rb +++ b/app/helpers/date_range_helper.rb @@ -36,8 +36,6 @@ def default_date 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 end From 59f68ab4559fb32a4428e42d80566a7eced6d8d3 Mon Sep 17 00:00:00 2001 From: Gabe Date: Tue, 14 Jan 2025 20:52:57 -0600 Subject: [PATCH 2/7] Redirecting to controller in context. Started on RSpec tests --- app/controllers/application_controller.rb | 2 +- .../application_controller_spec.rb | 27 +++++++++++++++++++ spec/helpers/date_range_helper_spec.rb | 18 +++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/date_range_helper_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index db4d316e3c..45f3c7cb42 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -120,7 +120,7 @@ def swaddled def invalid_date flash[:error] = "Date range not properly formatted." params["filters"]["date_range"] = helpers.default_date - redirect_back fallback_location: root_path + redirect_to controller: params["controller"], action: 'index' end protected diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 3ff2f7af64..095a7edcd9 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -95,4 +95,31 @@ end end end + + context "when user input date range is invalid" do + controller do + def setup_date_range_picker + super + end + end + + before(:each) do + # {"filters"=>{"date_range"=>"January 12, 2025 - January 13, 2025", "date_range_label"=>"since 2025-01-12", "by_type"=>"", "by_storage_location"=>"", "by_item"=>""}, "button"=>"", "controller"=>"events", "action"=>"index"} + + allow(controller).to receive(:params).and_return({"filters"=>{"date_range"=>"nov 08 to feb 08", "date_range_label"=>"since 2025-01-12"}, "controller"=>"distributions", "action"=>"index"}) + controller.setup_date_range_picker + end + + it "should flash an error message" do + expect(flash[:error]).to eq("Date range not properly formatted.") + end + + it "should reset the date range to the default date value" do + expect(:params["filters"]["date_range"]).to eq(helpers.default_date) + end + + it "should redirect to the correct controller and action" do + expect(controller).to receive(:redirect_to).with("distributions", "index") + end + end end diff --git a/spec/helpers/date_range_helper_spec.rb b/spec/helpers/date_range_helper_spec.rb new file mode 100644 index 0000000000..6c8f98cc8f --- /dev/null +++ b/spec/helpers/date_range_helper_spec.rb @@ -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 throw a Date::Error 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(Date::Error, "invalid date") + end + end +end From 8bde160c27e7476f0d50e6eaaa58a89c61a66d9f Mon Sep 17 00:00:00 2001 From: Gabe Date: Wed, 15 Jan 2025 14:18:24 -0600 Subject: [PATCH 3/7] Cleaning up code before PR and lint fixes --- app/controllers/application_controller.rb | 3 +-- .../application_controller_spec.rb | 27 ------------------- spec/helpers/date_range_helper_spec.rb | 2 +- 3 files changed, 2 insertions(+), 30 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 45f3c7cb42..14737b1f62 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -112,7 +112,6 @@ def omgwtfbbq! end end - def swaddled response.headers["swaddled-by"] = "rubyforgood" end @@ -120,7 +119,7 @@ def swaddled def invalid_date flash[:error] = "Date range not properly formatted." params["filters"]["date_range"] = helpers.default_date - redirect_to controller: params["controller"], action: 'index' + redirect_to controller: params["controller"], action: "index" end protected diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 095a7edcd9..3ff2f7af64 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -95,31 +95,4 @@ end end end - - context "when user input date range is invalid" do - controller do - def setup_date_range_picker - super - end - end - - before(:each) do - # {"filters"=>{"date_range"=>"January 12, 2025 - January 13, 2025", "date_range_label"=>"since 2025-01-12", "by_type"=>"", "by_storage_location"=>"", "by_item"=>""}, "button"=>"", "controller"=>"events", "action"=>"index"} - - allow(controller).to receive(:params).and_return({"filters"=>{"date_range"=>"nov 08 to feb 08", "date_range_label"=>"since 2025-01-12"}, "controller"=>"distributions", "action"=>"index"}) - controller.setup_date_range_picker - end - - it "should flash an error message" do - expect(flash[:error]).to eq("Date range not properly formatted.") - end - - it "should reset the date range to the default date value" do - expect(:params["filters"]["date_range"]).to eq(helpers.default_date) - end - - it "should redirect to the correct controller and action" do - expect(controller).to receive(:redirect_to).with("distributions", "index") - end - end end diff --git a/spec/helpers/date_range_helper_spec.rb b/spec/helpers/date_range_helper_spec.rb index 6c8f98cc8f..2ab6b3ac36 100644 --- a/spec/helpers/date_range_helper_spec.rb +++ b/spec/helpers/date_range_helper_spec.rb @@ -12,7 +12,7 @@ it "should throw a Date::Error 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(Date::Error, "invalid date") + expect { selected_interval }.to raise_error(Date::Error, "invalid date") end end end From 7b6ab0bbd023e310e653c7d8dbbc8abb1c5533ef Mon Sep 17 00:00:00 2001 From: Gabe Date: Fri, 17 Jan 2025 12:47:04 -0600 Subject: [PATCH 4/7] Using current action for redirect for reports workflows --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 14737b1f62..5c797e16e2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -119,7 +119,7 @@ def swaddled def invalid_date flash[:error] = "Date range not properly formatted." params["filters"]["date_range"] = helpers.default_date - redirect_to controller: params["controller"], action: "index" + redirect_to controller: params["controller"], action: params["action"] end protected From ca4de357928aceb12b4c75f516df6f9ca4eead86 Mon Sep 17 00:00:00 2001 From: Gabe Date: Sat, 18 Jan 2025 08:30:30 -0600 Subject: [PATCH 5/7] Updated error message with invalid date value and filter status --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5c797e16e2..9f271dd969 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -117,7 +117,7 @@ def swaddled end def invalid_date - flash[:error] = "Date range not properly formatted." + flash[:error] = "Date range '#{params.dig(:filters, :date_range).presence}' not properly formatted. Filters reset." params["filters"]["date_range"] = helpers.default_date redirect_to controller: params["controller"], action: params["action"] end From d06e1a703522c4715951aade83e214aef5f67397 Mon Sep 17 00:00:00 2001 From: Gabe Date: Sat, 18 Jan 2025 08:54:21 -0600 Subject: [PATCH 6/7] Created custom InvalidDateRange error for future-proofing --- app/controllers/application_controller.rb | 6 +++--- app/helpers/date_range_helper.rb | 2 ++ app/models/errors.rb | 9 +++++++++ spec/helpers/date_range_helper_spec.rb | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9f271dd969..e49e53ca84 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,7 +13,7 @@ class ApplicationController < ActionController::Base helper_method :current_role rescue_from ActiveRecord::RecordNotFound, with: :not_found! - rescue_from Date::Error, with: :invalid_date + rescue_from Errors::InvalidDateRange, with: :invalid_date def current_organization return @current_organization if @current_organization @@ -116,8 +116,8 @@ def swaddled response.headers["swaddled-by"] = "rubyforgood" end - def invalid_date - flash[:error] = "Date range '#{params.dig(:filters, :date_range).presence}' not properly formatted. Filters reset." + def invalid_date(date_value) + flash[:error] = "Date range '#{date_value}' not properly formatted. Filters reset." params["filters"]["date_range"] = helpers.default_date redirect_to controller: params["controller"], action: params["action"] end diff --git a/app/helpers/date_range_helper.rb b/app/helpers/date_range_helper.rb index ce384e6ba8..1b99ab8441 100644 --- a/app/helpers/date_range_helper.rb +++ b/app/helpers/date_range_helper.rb @@ -36,6 +36,8 @@ def default_date def selected_interval date_range_params.split(" - ").map do |d| Date.strptime(d, "%B %d, %Y") + rescue + raise Errors::InvalidDateRange.new(date_range_params) end end diff --git a/app/models/errors.rb b/app/models/errors.rb index 1d9cc89cad..163ea0616b 100644 --- a/app/models/errors.rb +++ b/app/models/errors.rb @@ -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 diff --git a/spec/helpers/date_range_helper_spec.rb b/spec/helpers/date_range_helper_spec.rb index 2ab6b3ac36..c9b9bf069a 100644 --- a/spec/helpers/date_range_helper_spec.rb +++ b/spec/helpers/date_range_helper_spec.rb @@ -10,9 +10,9 @@ expect(selected_interval).to eq([2.months.ago.to_date, 1.month.from_now.to_date]) end - it "should throw a Date::Error when input is invalid" do + 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(Date::Error, "invalid date") + expect { selected_interval }.to raise_error(Errors::InvalidDateRange, "nov 08 to feb 08") end end end From c5c15e2254926f922985cd0acd14feaf0e41ba63 Mon Sep 17 00:00:00 2001 From: Gabe Date: Sat, 18 Jan 2025 09:01:54 -0600 Subject: [PATCH 7/7] Manually resetting the date_range no longer necessary --- app/controllers/application_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e49e53ca84..29ba2c875e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -118,7 +118,6 @@ def swaddled def invalid_date(date_value) flash[:error] = "Date range '#{date_value}' not properly formatted. Filters reset." - params["filters"]["date_range"] = helpers.default_date redirect_to controller: params["controller"], action: params["action"] end