From a04d0d26719a6d5ed9d7957a65291c383f015a48 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 22 Nov 2017 17:17:49 +0100 Subject: [PATCH] Only show fields for promotion action calculators that we have preference form fields for We do not ship preference form fields for Array and Hash preference types. Some promotion actions might use them to store very specific settings. As arrays and hashes won't represent well in a form field, we should not render them. --- .../calculators/_default_fields.html.erb | 9 ++-- .../admin/promotion_adjustments_spec.rb | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/backend/app/views/spree/admin/promotions/calculators/_default_fields.html.erb b/backend/app/views/spree/admin/promotions/calculators/_default_fields.html.erb index 78273ae01cf..0bdcfa9286e 100644 --- a/backend/app/views/spree/admin/promotions/calculators/_default_fields.html.erb +++ b/backend/app/views/spree/admin/promotions/calculators/_default_fields.html.erb @@ -1,5 +1,6 @@ -<% calculator.preferences.keys.map do |key| %> - <%= render "spree/admin/shared/preference_fields/#{calculator.preference_type(key)}", - name: "#{prefix}[calculator_attributes][preferred_#{key}]", - value: calculator.get_preference(key), label: t(key.to_s, scope: 'spree') %> +<% calculator.admin_form_preference_names.map do |name| %> + <%= render "spree/admin/shared/preference_fields/#{calculator.preference_type(name)}", + name: "#{prefix}[calculator_attributes][preferred_#{name}]", + value: calculator.get_preference(name), + label: t(name.to_s, scope: 'spree', default: name.to_s.humanize) %> <% end %> diff --git a/backend/spec/features/admin/promotion_adjustments_spec.rb b/backend/spec/features/admin/promotion_adjustments_spec.rb index e44949eccb9..793053e4e5e 100644 --- a/backend/spec/features/admin/promotion_adjustments_spec.rb +++ b/backend/spec/features/admin/promotion_adjustments_spec.rb @@ -242,5 +242,48 @@ expect(first_action.calculator.class).to eq(Spree::Calculator::FlatRate) expect(first_action.calculator.preferred_amount).to eq(5) end + + context 'creating a promotion with promotion action that has a calculator with complex preferences' do + before do + class ComplexCalculator < Spree::Calculator + preference :amount, :decimal + preference :currency, :string + preference :mapping, :hash + preference :list, :array + + def self.description + "Complex Calculator" + end + end + @calculators = Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments + Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments = [ComplexCalculator] + end + + after do + Rails.application.config.spree.calculators.promotion_actions_create_item_adjustments = @calculators + end + + it "does not show array and hash form fields" do + fill_in "Name", with: "SAVE SAVE SAVE" + choose "Apply to all orders" + click_button "Create" + expect(page).to have_title("SAVE SAVE SAVE - Promotions") + + select "Create per-line-item adjustment", from: "Add action of type" + within('#action_fields') do + click_button "Add" + select "Complex Calculator", from: "Base Calculator" + end + within('#actions_container') { click_button "Update" } + expect(page).to have_text 'successfully updated' + + within('#action_fields') do + expect(page).to have_field('Amount') + expect(page).to have_field('Currency') + expect(page).to_not have_field('Mapping') + expect(page).to_not have_field('List') + end + end + end end end