Create new enterprise fee, display errors when present

This commit is contained in:
Rohan Mitchell
2012-11-21 09:47:51 +11:00
parent ff0d2bc983
commit a4d10ab4e0
7 changed files with 81 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,7 @@
class EnterpriseFeeSet < ModelSet
def initialize(attributes={})
super(EnterpriseFee, EnterpriseFee.all,
proc { |attrs| attrs[:enterprise_id].blank? },
attributes)
end
end

View File

@@ -1,5 +1,5 @@
class EnterpriseSet < ModelSet
def initialize(attributes={})
super(Enterprise.all, attributes)
super(Enterprise, Enterprise.all, nil, attributes)
end
end

View File

@@ -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

View File

@@ -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'

View File

@@ -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