From 98143ffe5b19c3758a1c8cfbc5f450bf792acfe3 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 Nov 2012 16:07:40 +1100 Subject: [PATCH] Add calculator to EnterpriseFee, test passes for listing enterprise fees --- .../admin/enterprise_fees_controller.rb | 6 +++++ app/models/enterprise_fee.rb | 3 +++ .../admin/enterprise_fees/index.html.haml | 7 ++++-- config/application.rb | 7 ++++++ config/initializers/spree.rb | 14 +++++++++++ spec/factories.rb | 9 ++++++++ spec/requests/admin/enterprise_fees_spec.rb | 23 +++++++++++++++++++ 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 spec/requests/admin/enterprise_fees_spec.rb diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index bdb74d59dd..2799aa68be 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -1,10 +1,16 @@ module Admin class EnterpriseFeesController < ResourceController before_filter :load_enterprise_fees_set, :only => :index + before_filter :load_data private def load_enterprise_fees_set @enterprise_fees_set = ModelSet.new EnterpriseFee.all, :collection => collection end + + def load_data + @calculators = EnterpriseFee.calculators.sort_by(&:name) + end + end end diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index ea5ba91446..25b2622e78 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -1,6 +1,9 @@ class EnterpriseFee < ActiveRecord::Base belongs_to :enterprise + calculated_adjustments + has_one :calculator, :as => :calculable, :dependent => :destroy, :class_name => 'Spree::Calculator' + FEE_TYPES = %w(packing transport admin sales) validates_inclusion_of :fee_type, :in => FEE_TYPES diff --git a/app/views/admin/enterprise_fees/index.html.haml b/app/views/admin/enterprise_fees/index.html.haml index bdac2f5ce5..23b2ddddb5 100644 --- a/app/views/admin/enterprise_fees/index.html.haml +++ b/app/views/admin/enterprise_fees/index.html.haml @@ -15,6 +15,9 @@ %td= f.collection_select :enterprise_id, Enterprise.all, :id, :name %td= f.select :fee_type, enterprise_fee_options %td= f.text_field :name - %td TODO - %td TODO + %td= f.collection_select :calculator_type, @calculators, :name, :description + %td + - if !enterprise_fee.new_record? + = f.fields_for :calculator do |calculator_form| + = preference_fields(enterprise_fee.calculator, calculator_form) %td TODO diff --git a/config/application.rb b/config/application.rb index 0e77035c39..24d5009745 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,6 +28,13 @@ module Openfoodweb initializer "spree.register.calculators" do |app| app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Itemwise app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Weight + + app.config.spree.calculators.enterprise_fees = [Spree::Calculator::FlatPercentItemTotal, + Spree::Calculator::FlatRate, + Spree::Calculator::FlexiRate, + Spree::Calculator::PerItem, + Spree::Calculator::PriceSack, + OpenFoodWeb::Calculator::Weight] end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 0a5d763b52..59982c1a9e 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -26,3 +26,17 @@ Spree.config do |config| # Auto-capture payments. Without this option, payments must be manually captured in the paypal interface. config.auto_capture = true end + + +# Add calculators category for enterprise fees +module Spree + module Core + class Environment + class Calculators + include EnvironmentExtension + + attr_accessor :enterprise_fees + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 6b76ab47c7..b643b92f1d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -24,6 +24,9 @@ FactoryGirl.define do enterprise { Enterprise.first || FactoryGirl.create(:supplier_enterprise) } fee_type 'packing' name '$0.50 / kg' + calculator { FactoryGirl.build(:weight_calculator) } + + after_create { |ef| ef.calculator.save! } end factory :product_distribution, :class => ProductDistribution do @@ -39,6 +42,12 @@ FactoryGirl.define do factory :itemwise_calculator, :class => OpenFoodWeb::Calculator::Itemwise do end + + factory :weight_calculator, :class => OpenFoodWeb::Calculator::Weight do + after_build { |c| c.set_preference(:per_kg, 0.5) } + after_create { |c| c.set_preference(:per_kg, 0.5); c.save! } + end + end diff --git a/spec/requests/admin/enterprise_fees_spec.rb b/spec/requests/admin/enterprise_fees_spec.rb new file mode 100644 index 0000000000..08fc134ceb --- /dev/null +++ b/spec/requests/admin/enterprise_fees_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +feature %q{ + As an administrator + I want to manage enterprise fees +} do + include AuthenticationWorkflow + include WebHelper + + scenario "listing enterprise fees" do + fee = create(:enterprise_fee) + + login_to_admin_section + 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 "input[value='$0.50 / kg']" + page.should have_selector 'option', :text => 'Weight (per kg)' + page.should have_selector "input[value='0.5']" + end +end