From a4d10ab4e08956ecbf1734f04199148c66639522 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 21 Nov 2012 09:47:51 +1100 Subject: [PATCH] Create new enterprise fee, display errors when present --- .../admin/enterprise_fees_controller.rb | 20 +++++++++-- app/models/enterprise_fee.rb | 2 ++ app/models/enterprise_fee_set.rb | 7 ++++ app/models/enterprise_set.rb | 2 +- app/models/model_set.rb | 17 ++++++--- .../admin/enterprise_fees/index.html.haml | 12 +++++-- spec/requests/admin/enterprise_fees_spec.rb | 35 +++++++++++++++++-- 7 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 app/models/enterprise_fee_set.rb diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 2799aa68be..9b6966c40e 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -1,16 +1,30 @@ module Admin class EnterpriseFeesController < ResourceController - before_filter :load_enterprise_fees_set, :only => :index + before_filter :load_enterprise_fee_set, :only => :index before_filter :load_data + def bulk_update + @enterprise_fee_set = EnterpriseFeeSet.new(params[:enterprise_fee_set]) + if @enterprise_fee_set.save + redirect_to main_app.admin_enterprise_fees_path, :notice => 'Your enterprise fees have been updated.' + else + render :index + end + end + + private - def load_enterprise_fees_set - @enterprise_fees_set = ModelSet.new EnterpriseFee.all, :collection => collection + def load_enterprise_fee_set + @enterprise_fee_set = EnterpriseFeeSet.new :collection => collection end def load_data @calculators = EnterpriseFee.calculators.sort_by(&:name) end + def collection + super + (1..3).map { EnterpriseFee.new } + end + end end diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 25b2622e78..8107f1fdf7 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -4,6 +4,8 @@ class EnterpriseFee < ActiveRecord::Base calculated_adjustments has_one :calculator, :as => :calculable, :dependent => :destroy, :class_name => 'Spree::Calculator' + attr_accessible :enterprise_id, :fee_type, :name, :calculator_type + FEE_TYPES = %w(packing transport admin sales) validates_inclusion_of :fee_type, :in => FEE_TYPES diff --git a/app/models/enterprise_fee_set.rb b/app/models/enterprise_fee_set.rb new file mode 100644 index 0000000000..4ae02f51d8 --- /dev/null +++ b/app/models/enterprise_fee_set.rb @@ -0,0 +1,7 @@ +class EnterpriseFeeSet < ModelSet + def initialize(attributes={}) + super(EnterpriseFee, EnterpriseFee.all, + proc { |attrs| attrs[:enterprise_id].blank? }, + attributes) + end +end diff --git a/app/models/enterprise_set.rb b/app/models/enterprise_set.rb index f267810c62..81591b6dad 100644 --- a/app/models/enterprise_set.rb +++ b/app/models/enterprise_set.rb @@ -1,5 +1,5 @@ class EnterpriseSet < ModelSet def initialize(attributes={}) - super(Enterprise.all, attributes) + super(Enterprise, Enterprise.all, nil, attributes) end end diff --git a/app/models/model_set.rb b/app/models/model_set.rb index 737fb9d625..778a148b82 100644 --- a/app/models/model_set.rb +++ b/app/models/model_set.rb @@ -5,8 +5,9 @@ class ModelSet attr_accessor :collection - def initialize(collection, attributes={}) - @collection = collection + + def initialize(klass, collection, reject_if, attributes={}) + @klass, @collection, @reject_if = klass, collection, reject_if attributes.each do |name, value| send("#{name}=", value) @@ -16,11 +17,19 @@ class ModelSet def collection_attributes=(attributes) attributes.each do |k, attributes| # attributes == {:id => 123, :next_collection_at => '...'} - e = @collection.detect { |e| e.id.to_s == attributes[:id].to_s } - e.assign_attributes(attributes.except(:id)) + e = @collection.detect { |e| e.id.to_s == attributes[:id].to_s && !e.id.nil? } + if e.nil? + @collection << @klass.new(attributes) unless @reject_if.andand.call(attributes) + else + e.assign_attributes(attributes.except(:id)) + end end end + def errors + @collection.map { |ef| ef.errors.full_messages }.flatten + end + def save collection.all?(&:save) end diff --git a/app/views/admin/enterprise_fees/index.html.haml b/app/views/admin/enterprise_fees/index.html.haml index 23b2ddddb5..4f2fb2ae85 100644 --- a/app/views/admin/enterprise_fees/index.html.haml +++ b/app/views/admin/enterprise_fees/index.html.haml @@ -1,4 +1,9 @@ -= form_for @enterprise_fees_set, :url => main_app.bulk_update_admin_enterprise_fees_path do |enterprise_fees_set_form| += form_for @enterprise_fee_set, :url => main_app.bulk_update_admin_enterprise_fees_path do |enterprise_fee_set_form| + - if @enterprise_fee_set.errors.present? + %h2 Errors + %ul + - @enterprise_fee_set.errors.each do |error| + %li= error %table.index#listing_enterprise_fees %thead %tr @@ -9,10 +14,10 @@ %th Calculator values %th %tbody - = enterprise_fees_set_form.fields_for :collection do |f| + = enterprise_fee_set_form.fields_for :collection do |f| - enterprise_fee = f.object %tr - %td= f.collection_select :enterprise_id, Enterprise.all, :id, :name + %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 @@ -21,3 +26,4 @@ = 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 08fc134ceb..d4d583860b 100644 --- a/spec/requests/admin/enterprise_fees_spec.rb +++ b/spec/requests/admin/enterprise_fees_spec.rb @@ -14,10 +14,39 @@ 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', text: fee.enterprise.name + page.should have_selector 'option', 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', text: 'Weight (per kg)' page.should have_selector "input[value='0.5']" end + + scenario "creating an enterprise fee" do + # Given an enterprise + e = create(:supplier_enterprise, name: 'Feedme') + + # When I go to the enterprise fees page + login_to_admin_section + click_link 'Configuration' + click_link 'Enterprise Fees' + + # And I fill in the fields for a new enterprise fee and click update + select 'Feedme', 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: 'Hello!' + select 'Flat Percent', from: 'enterprise_fee_set_collection_attributes_0_calculator_type' + click_button 'Update' + + # Then I should see my fee and fields for the calculator + page.should have_content "Your enterprise fees have been updated." + page.should have_selector "input[value='Hello!']" + + # When I fill in the calculator fields and click update + fill_in 'enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent', with: '12.34' + click_button 'Update' + + # Then I should see the correct values in my calculator fields + page.should have_selector "#enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent[value='12.34']" + end + end