From d73df78f4349761dde74b7b4b00b17bdc26187c5 Mon Sep 17 00:00:00 2001 From: Meron Ogbai Date: Fri, 17 Sep 2021 16:48:53 +0300 Subject: [PATCH 1/3] Extract gateway_params to a private method --- app/controllers/spree/admin/payment_methods_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 94d95b6a6c..5b74fe5588 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -157,11 +157,14 @@ module Spree call.to_h.with_indifferent_access end + def gateway_params + raw_params[ActiveModel::Naming.param_key(@payment_method)] || {} + end + # Merge payment method params with gateway params like :gateway_stripe_connect # Also, remove password if present and blank def update_params @update_params ||= begin - gateway_params = raw_params[ActiveModel::Naming.param_key(@payment_method)] || {} params_for_update = base_params.merge(gateway_params) params_for_update.each do |key, value| From 957f834694b704f07b662e3aa08ef7c5b5f909be Mon Sep 17 00:00:00 2001 From: Meron Ogbai Date: Fri, 17 Sep 2021 18:49:57 +0300 Subject: [PATCH 2/3] Rename preferred amount to preferred value --- .../spree/admin/payment_methods_controller.rb | 10 +++------- config/locales/en.yml | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 5b74fe5588..0aaee82b4c 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -7,7 +7,7 @@ module Spree before_action :load_data before_action :validate_payment_method_provider, only: [:create] before_action :load_hubs, only: [:new, :edit, :update] - before_action :validate_calculator_preferred_amount, only: [:update] + before_action :validate_calculator_preferred_value, only: [:update] respond_to :html @@ -177,13 +177,9 @@ module Spree end end - def validate_calculator_preferred_amount - preferred_amount = params.dig(:payment_method_check, :calculator_attributes, - :preferred_amount) - return if preferred_amount.nil? || Float(preferred_amount, - exception: false) + def validate_calculator_preferred_value - flash[:error] = I18n.t(:calculator_preferred_amount_error) + flash[:error] = I18n.t(:calculator_preferred_value_error) redirect_to spree.edit_admin_payment_method_path(@payment_method) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 623aba949a..d9e0955a23 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2322,7 +2322,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using calculator_values: "Calculator values" calculator_settings_warning: "If you are changing the calculator type, you must save first before you can edit the calculator settings" calculator_preferred_unit_error: "must be kg or lb" - calculator_preferred_amount_error: "Invalid input. Please use only numbers. For example: 10, 5.5, -20" + calculator_preferred_value_error: "Invalid input. Please use only numbers. For example: 10, 5.5, -20" flat_percent_per_item: "Flat Percent (per item)" flat_rate_per_item: "Flat Rate (per item)" flat_rate_per_order: "Flat Rate (per order)" From cd992ee866d18758985384214d6dfac44c768713 Mon Sep 17 00:00:00 2001 From: Meron Ogbai Date: Fri, 17 Sep 2021 18:50:29 +0300 Subject: [PATCH 3/3] Validate preferred values all calculators --- .../spree/admin/payment_methods_controller.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 0aaee82b4c..9f88727cf2 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -178,10 +178,29 @@ module Spree end def validate_calculator_preferred_value + return if calculator_preferred_values.all? do |value| + preferred_value_from_params = gateway_params.dig(:calculator_attributes, value) + preferred_value_from_params.nil? || Float(preferred_value_from_params, + exception: false) + end flash[:error] = I18n.t(:calculator_preferred_value_error) redirect_to spree.edit_admin_payment_method_path(@payment_method) end + + def calculator_preferred_values + [ + :preferred_amount, + :preferred_flat_percent, + :preferred_flat_percent, + :preferred_first_item, + :preferred_additional_item, + :preferred_max_items, + :preferred_normal_amount, + :preferred_discount_amount, + :preferred_minimal_amount + ] + end end end end