Skip to content

Commit

Permalink
Only show fields for calculators that we have preference form fields for
Browse files Browse the repository at this point in the history
We do not ship preference form fields for Array and Hash preference types. Some shipping or tax rate calculators 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 a04d0d2 commit 79df1c3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<% calculator = f.object.calculator.class == calculator_class ? f.object.calculator : calculator_class.new %>
<div class="js-calculator-preferences" data-calculator-type="<%= calculator_class %>">
<%= f.fields_for :calculator, calculator do |calculator_form| %>
<% calculator.preferences.keys.each do |key| %>
<%= render "spree/admin/shared/preference_fields/#{calculator.preference_type(key)}",
form: calculator_form, attribute: "preferred_#{key}", label: t(key, scope: 'spree') %>
<% calculator.admin_form_preference_names.each do |name| %>
<%= render "spree/admin/shared/preference_fields/#{calculator.preference_type(name)}",
form: calculator_form, attribute: "preferred_#{name}",
label: t(name, scope: 'spree', default: name.to_s.humanize) %>
<% end %>
<% end %>
</div>
Expand Down
40 changes: 40 additions & 0 deletions backend/spec/features/admin/configuration/shipping_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@
click_on "Create"
expect(current_path).to eql(spree.edit_admin_shipping_method_path(Spree::ShippingMethod.last))
end

context 'with shipping method having a calculator with array or hash preference type' do
before do
class ComplexShipments < Spree::ShippingCalculator
preference :amount, :decimal
preference :currency, :string
preference :mapping, :hash
preference :list, :array

def self.description
"Complex Shipments"
end
end
@calculators = Rails.application.config.spree.calculators.shipping_methods
Rails.application.config.spree.calculators.shipping_methods = [ComplexShipments]
end

after do
Rails.application.config.spree.calculators.shipping_methods = @calculators
end

it "does not show array and hash form fields" do
click_link "New Shipping Method"

fill_in "shipping_method_name", with: "bullock cart"

within("#shipping_method_categories_field") do
check first("input[type='checkbox']")["name"]
end

click_on "Create"
select 'Complex Shipments', from: 'Base Calculator'
click_on "Update"

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

# Regression test for https://github.com/spree/spree/issues/1331
Expand Down

0 comments on commit 79df1c3

Please sign in to comment.