From 77b78f5bbb0f91e9b551fa069c8207ed8f2206b6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 Nov 2012 11:50:37 +1100 Subject: [PATCH] Edit enterprise fee --- .../javascripts/admin/enterprise_fees.js | 26 +++++++++++++++ .../admin/enterprise_fees/index.html.haml | 7 ++-- spec/requests/admin/enterprise_fees_spec.rb | 33 +++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/admin/enterprise_fees.js diff --git a/app/assets/javascripts/admin/enterprise_fees.js b/app/assets/javascripts/admin/enterprise_fees.js new file mode 100644 index 0000000000..e07b0d86ce --- /dev/null +++ b/app/assets/javascripts/admin/enterprise_fees.js @@ -0,0 +1,26 @@ +// Hide calculator preference fields when calculator type changed +// Fixes 'Enterprise fee is not found' error when changing calculator type +// See spree/core/app/assets/javascripts/admin/calculator.js + +$(document).ready(function() { + // Store original value + $("select.calculator_type").each(function(i, ct) { + ct = $(ct); + ct.data('original-value', ct.attr('value')); + }); + + // Hide and disable calculator fields when calculator type is changed + $("select.calculator_type").change(function() { + var ct = $(this); + var cs = ct.parent().parent().find("div.calculator-settings"); + + if(ct.attr('value') == ct.data('original-value')) { + cs.show(); + cs.find("input").prop("disabled", false); + + } else { + cs.hide(); + cs.find("input").prop("disabled", true); + } + }); +}); diff --git a/app/views/admin/enterprise_fees/index.html.haml b/app/views/admin/enterprise_fees/index.html.haml index 4f2fb2ae85..213923160f 100644 --- a/app/views/admin/enterprise_fees/index.html.haml +++ b/app/views/admin/enterprise_fees/index.html.haml @@ -20,10 +20,11 @@ %td= f.collection_select :enterprise_id, Enterprise.all, :id, :name, :include_blank => true %td= f.select :fee_type, enterprise_fee_options %td= f.text_field :name - %td= f.collection_select :calculator_type, @calculators, :name, :description + %td= f.collection_select :calculator_type, @calculators, :name, :description, {}, {:class => 'calculator_type'} %td - if !enterprise_fee.new_record? - = f.fields_for :calculator do |calculator_form| - = preference_fields(enterprise_fee.calculator, calculator_form) + .calculator-settings + = f.fields_for :calculator do |calculator_form| + = preference_fields(enterprise_fee.calculator, calculator_form) %td TODO = enterprise_fee_set_form.submit 'Update' diff --git a/spec/requests/admin/enterprise_fees_spec.rb b/spec/requests/admin/enterprise_fees_spec.rb index d4d583860b..da4fdb6547 100644 --- a/spec/requests/admin/enterprise_fees_spec.rb +++ b/spec/requests/admin/enterprise_fees_spec.rb @@ -14,10 +14,10 @@ feature %q{ click_link 'Configuration' click_link 'Enterprise Fees' - page.should have_selector 'option', text: fee.enterprise.name - page.should have_selector 'option', text: 'Packing' + page.should have_selector "option[selected]", text: fee.enterprise.name + page.should have_selector "option[selected]", text: 'Packing' page.should have_selector "input[value='$0.50 / kg']" - page.should have_selector 'option', text: 'Weight (per kg)' + page.should have_selector "option[selected]", text: 'Weight (per kg)' page.should have_selector "input[value='0.5']" end @@ -49,4 +49,31 @@ feature %q{ page.should have_selector "#enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent[value='12.34']" end + scenario "editing an enterprise fee", js: true do + # Given an enterprise fee + fee = create(:enterprise_fee) + create(:enterprise, name: 'Foo') + + # When I go to the enterprise fees page + login_to_admin_section + click_link 'Configuration' + click_link 'Enterprise Fees' + binding.pry + + # And I update the fields for the enterprise fee and click update + select 'Foo', from: 'enterprise_fee_set_collection_attributes_0_enterprise_id' + select 'Admin', from: 'enterprise_fee_set_collection_attributes_0_fee_type' + fill_in 'enterprise_fee_set_collection_attributes_0_name', with: 'Greetings!' + select 'Flat Percent', from: 'enterprise_fee_set_collection_attributes_0_calculator_type' + binding.pry + click_button 'Update' + + # Then I should see the updated fields for my fee + page.should have_selector "option[selected]", text: 'Foo' + page.should have_selector "option[selected]", text: 'Admin' + page.should have_selector "input[value='Greetings!']" + page.should have_selector "option[selected]", text: 'Flat Percent' + end + + end