Skip to content

Commit

Permalink
Only show fields for promotion action calculators that we have prefer…
Browse files Browse the repository at this point in the history
…ence 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.
  • Loading branch information
tvdeyen committed Nov 25, 2017
1 parent be51a30 commit a04d0d2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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 %>
43 changes: 43 additions & 0 deletions backend/spec/features/admin/promotion_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a04d0d2

Please sign in to comment.