From ba0b3bad8562f2287df436849ce6f4a6d9fc6841 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 9 Aug 2013 17:35:34 +1000 Subject: [PATCH 01/13] Convert product distribution shipping methods to enterprise fees --- ...e_fee_records_for_product_distributions.rb | 19 +++++++++++++++++++ db/schema.rb | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20130809045637_create_enterprise_fee_records_for_product_distributions.rb diff --git a/db/migrate/20130809045637_create_enterprise_fee_records_for_product_distributions.rb b/db/migrate/20130809045637_create_enterprise_fee_records_for_product_distributions.rb new file mode 100644 index 0000000000..2cb18b82b7 --- /dev/null +++ b/db/migrate/20130809045637_create_enterprise_fee_records_for_product_distributions.rb @@ -0,0 +1,19 @@ +class CreateEnterpriseFeeRecordsForProductDistributions < ActiveRecord::Migration + def up + ProductDistribution.all.each do |pd| + calculator = pd.shipping_method.calculator.dup + calculator.save! + + ef = EnterpriseFee.new enterprise_id: pd.distributor.id, fee_type: 'packing', name: pd.shipping_method.name + ef.calculator = calculator + ef.save! + + pd.enterprise_fee = ef + pd.save! + end + end + + def down + ProductDistribution.update_all :enterprise_fee_id => nil + end +end diff --git a/db/schema.rb b/db/schema.rb index ac8c7f6946..1ad64fed61 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130807230834) do +ActiveRecord::Schema.define(:version => 20130809045637) do create_table "carts", :force => true do |t| t.integer "user_id" From 149d48ff5ab9fe4bedb03508694807fe43674060 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 09:59:44 +1000 Subject: [PATCH 02/13] Add AdjustmentMetadata, for holding info about enterprise fee adjustments at time of creation --- app/models/adjustment_metadata.rb | 4 ++++ app/models/spree/adjustment_decorator.rb | 2 ++ .../20130809075103_create_adjustment_metadata.rb | 13 +++++++++++++ db/schema.rb | 12 +++++++++++- spec/factories.rb | 8 ++++++++ spec/models/adjustment_metadata_spec.rb | 6 ++++++ spec/models/adjustment_spec.rb | 8 ++++++++ 7 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 app/models/adjustment_metadata.rb create mode 100644 db/migrate/20130809075103_create_adjustment_metadata.rb create mode 100644 spec/models/adjustment_metadata_spec.rb create mode 100644 spec/models/adjustment_spec.rb diff --git a/app/models/adjustment_metadata.rb b/app/models/adjustment_metadata.rb new file mode 100644 index 0000000000..d8315a3c71 --- /dev/null +++ b/app/models/adjustment_metadata.rb @@ -0,0 +1,4 @@ +class AdjustmentMetadata < ActiveRecord::Base + belongs_to :adjustment, class_name: 'Spree::Adjustment' + belongs_to :enterprise +end diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index 6d089ee3b3..5c4748b227 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -1,5 +1,7 @@ module Spree Adjustment.class_eval do + has_one :metadata, class_name: 'AdjustmentMetadata' + scope :enterprise_fee, where(originator_type: 'EnterpriseFee') end end diff --git a/db/migrate/20130809075103_create_adjustment_metadata.rb b/db/migrate/20130809075103_create_adjustment_metadata.rb new file mode 100644 index 0000000000..4d1c6579eb --- /dev/null +++ b/db/migrate/20130809075103_create_adjustment_metadata.rb @@ -0,0 +1,13 @@ +class CreateAdjustmentMetadata < ActiveRecord::Migration + def change + create_table :adjustment_metadata do |t| + t.integer :adjustment_id + t.integer :enterprise_id + t.string :fee_name + t.string :fee_type + t.string :enterprise_role + end + + add_index :adjustment_metadata, :adjustment_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 1ad64fed61..ac3280a415 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,17 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130809045637) do +ActiveRecord::Schema.define(:version => 20130809075103) do + + create_table "adjustment_metadata", :force => true do |t| + t.integer "adjustment_id" + t.integer "enterprise_id" + t.string "fee_name" + t.string "fee_type" + t.string "enterprise_role" + end + + add_index "adjustment_metadata", ["adjustment_id"], :name => "index_adjustment_metadata_on_adjustment_id" create_table "carts", :force => true do |t| t.integer "user_id" diff --git a/spec/factories.rb b/spec/factories.rb index 32a9cd770a..c6462d8f28 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -97,6 +97,14 @@ FactoryGirl.define do calculator { FactoryGirl.build(:itemwise_calculator) } end + factory :adjustment_metadata, :class => AdjustmentMetadata do + adjustment { FactoryGirl.create(:adjustment) } + enterprise { FactoryGirl.create(:distributor_enterprise) } + fee_name 'fee' + fee_type 'packing' + enterprise_role 'distributor' + end + factory :itemwise_calculator, :class => OpenFoodWeb::Calculator::Itemwise do end diff --git a/spec/models/adjustment_metadata_spec.rb b/spec/models/adjustment_metadata_spec.rb new file mode 100644 index 0000000000..6ead53ce30 --- /dev/null +++ b/spec/models/adjustment_metadata_spec.rb @@ -0,0 +1,6 @@ +describe AdjustmentMetadata do + it "is valid when build from factory" do + adjustment = create(:adjustment) + adjustment.should be_valid + end +end diff --git a/spec/models/adjustment_spec.rb b/spec/models/adjustment_spec.rb new file mode 100644 index 0000000000..60dfd631ce --- /dev/null +++ b/spec/models/adjustment_spec.rb @@ -0,0 +1,8 @@ +module Spree + describe Adjustment do + it "has metadata" do + adjustment = create(:adjustment, metadata: create(:adjustment_metadata)) + adjustment.metadata.should be + end + end +end From 14268b7be0ae34a97d472918bc9bf53cb83f4e77 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 10:00:10 +1000 Subject: [PATCH 03/13] Create adjustment metadata when creating adjustment --- app/models/product_distribution.rb | 3 ++- spec/models/product_distribution_spec.rb | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/models/product_distribution.rb b/app/models/product_distribution.rb index 7ef2a90cdf..a5c1438605 100644 --- a/app/models/product_distribution.rb +++ b/app/models/product_distribution.rb @@ -25,7 +25,8 @@ class ProductDistribution < ActiveRecord::Base end def create_adjustment_for(line_item) - enterprise_fee.create_adjustment(adjustment_label_for(line_item), line_item.order, line_item, true) + a = enterprise_fee.create_adjustment(adjustment_label_for(line_item), line_item.order, line_item, true) + AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: 'distributor' end def clear_all_enterprise_fee_adjustments_for(line_item) diff --git a/spec/models/product_distribution_spec.rb b/spec/models/product_distribution_spec.rb index a1f92d2cb3..ad18e3f8ad 100644 --- a/spec/models/product_distribution_spec.rb +++ b/spec/models/product_distribution_spec.rb @@ -57,7 +57,11 @@ describe ProductDistribution do adjustment.amount.should == 1.23 # And it should have some associated metadata - pending 'Needs metadata spec' + md = adjustment.metadata + md.enterprise.should == distributor + md.fee_name.should == enterprise_fee.name + md.fee_type.should == enterprise_fee.fee_type + md.enterprise_role.should == 'distributor' end end @@ -152,6 +156,12 @@ describe ProductDistribution do adjustment.source.should == line_item adjustment.originator.should == pd.enterprise_fee adjustment.should be_mandatory + + md = adjustment.metadata + md.enterprise.should == pd.distributor + md.fee_name.should == pd.enterprise_fee.name + md.fee_type.should == pd.enterprise_fee.fee_type + md.enterprise_role.should == 'distributor' end end From 7ea112283339af9180a7d6145fd4489ede44d6db Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 10:13:31 +1000 Subject: [PATCH 04/13] Make spec naming consistent --- spec/features/admin/{order_spec.rb => orders_spec.rb} | 0 spec/features/admin/{product_spec.rb => products_spec.rb} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename spec/features/admin/{order_spec.rb => orders_spec.rb} (100%) rename spec/features/admin/{product_spec.rb => products_spec.rb} (100%) diff --git a/spec/features/admin/order_spec.rb b/spec/features/admin/orders_spec.rb similarity index 100% rename from spec/features/admin/order_spec.rb rename to spec/features/admin/orders_spec.rb diff --git a/spec/features/admin/product_spec.rb b/spec/features/admin/products_spec.rb similarity index 100% rename from spec/features/admin/product_spec.rb rename to spec/features/admin/products_spec.rb From af6b16ecc45b1b84c18604dda07cfcf915b98f4f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 10:40:54 +1000 Subject: [PATCH 05/13] Admin can assign enterprise fees to product distributions --- app/models/enterprise_fee.rb | 3 +++ app/models/product_distribution.rb | 2 +- .../admin/products/_distributors_form.html.haml | 2 +- spec/factories.rb | 3 ++- spec/features/admin/products_spec.rb | 12 ++++++------ 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 8edd76e60b..96b062cdd0 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -9,4 +9,7 @@ class EnterpriseFee < ActiveRecord::Base validates_inclusion_of :fee_type, :in => FEE_TYPES validates_presence_of :name + + + scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) } end diff --git a/app/models/product_distribution.rb b/app/models/product_distribution.rb index a5c1438605..44ce0d494b 100644 --- a/app/models/product_distribution.rb +++ b/app/models/product_distribution.rb @@ -5,7 +5,7 @@ class ProductDistribution < ActiveRecord::Base belongs_to :enterprise_fee validates_presence_of :product_id, :on => :update - validates_presence_of :distributor_id, :shipping_method_id + validates_presence_of :distributor_id, :enterprise_fee_id validates_uniqueness_of :product_id, :scope => :distributor_id diff --git a/app/views/spree/admin/products/_distributors_form.html.haml b/app/views/spree/admin/products/_distributors_form.html.haml index e5c867c6e8..d4b655dd59 100644 --- a/app/views/spree/admin/products/_distributors_form.html.haml +++ b/app/views/spree/admin/products/_distributors_form.html.haml @@ -11,4 +11,4 @@ = label_tag "#{pd_form.object_name}[_destroy]", pd_form.object.distributor.name = pd_form.hidden_field :distributor_id %td - = pd_form.collection_select :shipping_method_id, Spree::ShippingMethod.all, :id, :name, {}, :class => "select2" + = pd_form.collection_select :enterprise_fee_id, EnterpriseFee.for_enterprise(pd_form.object.distributor), :id, :name, {}, :class => "select2" diff --git a/spec/factories.rb b/spec/factories.rb index c6462d8f28..5d288a1993 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -77,9 +77,10 @@ FactoryGirl.define do end factory :enterprise_fee, :class => EnterpriseFee do + sequence(:name) { |n| "Enterprise fee #{n}" } + 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! } diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 908e5528e3..f7245c1243 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -10,7 +10,7 @@ feature %q{ background do @supplier = create(:supplier_enterprise, :name => 'New supplier') @distributors = (1..3).map { create(:distributor_enterprise) } - @shipping_method = create(:shipping_method, :name => 'My shipping method') + @enterprise_fees = (0..2).map { |i| create(:enterprise_fee, enterprise: @distributors[i]) } end context "creating a product" do @@ -25,9 +25,9 @@ feature %q{ select 'New supplier', :from => 'product_supplier_id' check @distributors[0].name - select 'My shipping method', :from => 'product_product_distributions_attributes_0_shipping_method_id' + select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' check @distributors[2].name - select 'My shipping method', :from => 'product_product_distributions_attributes_2_shipping_method_id' + select @enterprise_fees[2].name, :from => 'product_product_distributions_attributes_2_enterprise_fee_id' click_button 'Create' @@ -35,7 +35,7 @@ feature %q{ product = Spree::Product.find_by_name('A new product !!!') product.supplier.should == @supplier product.distributors.should == [@distributors[0], @distributors[2]] - product.product_distributions.map { |pd| pd.shipping_method }.should == [@shipping_method, @shipping_method] + product.product_distributions.map { |pd| pd.enterprise_fee }.should == [@enterprise_fees[0], @enterprise_fees[2]] product.group_buy.should be_false end @@ -60,7 +60,7 @@ feature %q{ end - describe 'As an enterprise user' do + context "as an enterprise user" do before(:each) do @new_user = create_enterprise_user @@ -87,7 +87,7 @@ feature %q{ end check @distributors[0].name - select 'My shipping method', :from => 'product_product_distributions_attributes_0_shipping_method_id' + select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' # Should only have distributors listed which the user can manage within "#product_product_distributions_field" do From f1485bf9c5402fa3e2b9705cbac1cf9aaf31b5f6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 14:21:39 +1000 Subject: [PATCH 06/13] Remove shipping method from product distribution in factory. --- spec/factories.rb | 14 -------------- spec/features/admin/enterprise_fees_spec.rb | 2 +- spec/features/admin/reports_spec.rb | 2 +- spec/support/spree/init.rb | 10 +++++----- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 5d288a1993..b652afd596 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -89,7 +89,6 @@ FactoryGirl.define do factory :product_distribution, :class => ProductDistribution do product { |pd| Spree::Product.first || FactoryGirl.create(:product) } distributor { |pd| Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise) } - shipping_method { |pd| Spree::ShippingMethod.where("name != 'Delivery'").first || FactoryGirl.create(:shipping_method) } enterprise_fee { |pd| FactoryGirl.create(:enterprise_fee, enterprise: pd.distributor) } end @@ -135,19 +134,6 @@ FactoryGirl.modify do supplier { Enterprise.is_primary_producer.first || FactoryGirl.create(:supplier_enterprise) } on_hand 3 - - # before(:create) do |product, evaluator| - # product.product_distributions = [FactoryGirl.create(:product_distribution, :product => product)] - # end - - # Do not create products distributed via the 'Delivery' shipping method - after(:create) do |product, evaluator| - pd = product.product_distributions.first - if pd.andand.shipping_method.andand.name == 'Delivery' - pd.shipping_method = Spree::ShippingMethod.where("name != 'Delivery'").first || FactoryGirl.create(:shipping_method) - pd.save! - end - end end factory :shipping_method do diff --git a/spec/features/admin/enterprise_fees_spec.rb b/spec/features/admin/enterprise_fees_spec.rb index 745f953bca..2f3c9e64fd 100644 --- a/spec/features/admin/enterprise_fees_spec.rb +++ b/spec/features/admin/enterprise_fees_spec.rb @@ -17,7 +17,7 @@ feature %q{ end scenario "listing enterprise fees" do - fee = create(:enterprise_fee) + fee = create(:enterprise_fee, name: '$0.50 / kg') login_to_admin_section click_link 'Configuration' diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 1202f7da45..7345a527b0 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -54,7 +54,7 @@ feature %q{ @distributor_address = create(:address, :address1 => "distributor address", :city => 'The Shire', :zipcode => "1234") @distributor = create(:distributor_enterprise, :address => @distributor_address) product = create(:product) - product_distribution = create(:product_distribution, :product => product, :distributor => @distributor, :shipping_method => create(:shipping_method)) + product_distribution = create(:product_distribution, :product => product, :distributor => @distributor) @shipping_instructions = "pick up on thursday please!" @order1 = create(:order, :distributor => @distributor, :bill_address => @bill_address, :special_instructions => @shipping_instructions) @order2 = create(:order, :distributor => @distributor, :bill_address => @bill_address, :special_instructions => @shipping_instructions) diff --git a/spec/support/spree/init.rb b/spec/support/spree/init.rb index 471b7cf2c9..c60c7bc0d4 100644 --- a/spec/support/spree/init.rb +++ b/spec/support/spree/init.rb @@ -1,11 +1,11 @@ -# Initialise shipping method when created without one, like this: +# Initialise enterprise fee when created without one, like this: # create(:product, :distributors => [...]) -# In this case, we don't care what the shipping method is, but we need one for validations to pass. +# In this case, we don't care what the fee is, but we need one for validations to pass. ProductDistribution.class_eval do - before_validation :init_shipping_method + before_validation :init_enterprise_fee - def init_shipping_method - self.shipping_method ||= Spree::ShippingMethod.first || FactoryGirl.create(:shipping_method) + def init_enterprise_fee + self.enterprise_fee ||= EnterpriseFee.where(enterprise_id: distributor).first || FactoryGirl.create(:enterprise_fee, enterprise_id: distributor) end end From d7cce918f5265eb0da976e530baa82c1132fc3c6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 14:22:11 +1000 Subject: [PATCH 07/13] Instead of protecting shipping methods from destruction when referenced by product distributions, protect enterprise fees --- .../admin/enterprise_fees_controller.rb | 17 +++++++++++++- spec/features/admin/enterprise_fees_spec.rb | 23 +++++++++++++++++++ spec/features/admin/shipping_methods_spec.rb | 11 --------- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 890cc5b08f..41450c82c9 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -2,6 +2,8 @@ module Admin class EnterpriseFeesController < ResourceController before_filter :load_enterprise_fee_set, :only => :index before_filter :load_data + before_filter :do_not_destroy_referenced_fees, :only => :destroy + def index respond_to do |format| @@ -21,6 +23,20 @@ module Admin private + + def do_not_destroy_referenced_fees + product_distribution = ProductDistribution.where(:enterprise_fee_id => @object).first + if product_distribution + p = product_distribution.product + flash[:error] = "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." + + respond_with(@object) do |format| + format.html { redirect_to collection_url } + format.js { render text: flash[:error], status: 403 } + end + end + end + def load_enterprise_fee_set @enterprise_fee_set = EnterpriseFeeSet.new :collection => collection end @@ -32,6 +48,5 @@ module Admin def collection super.order('enterprise_id', 'fee_type', 'name') end - end end diff --git a/spec/features/admin/enterprise_fees_spec.rb b/spec/features/admin/enterprise_fees_spec.rb index 2f3c9e64fd..e2af5bb65e 100644 --- a/spec/features/admin/enterprise_fees_spec.rb +++ b/spec/features/admin/enterprise_fees_spec.rb @@ -99,4 +99,27 @@ feature %q{ page.should_not have_selector "input[value='#{fee.name}']" end + scenario "deleting a shipping method referenced by a product distribution" do + # Given an enterprise fee referenced by a product distribution + fee = create(:enterprise_fee) + p = create(:product) + d = create(:distributor_enterprise) + create(:product_distribution, product: p, distributor: d, enterprise_fee: fee) + + # When I go to the enterprise fees page + login_to_admin_section + click_link 'Configuration' + click_link 'Enterprise Fees' + + # And I click delete + find("a.delete-resource").click + + # Then I should see an error + page.should have_content "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." + + # And my enterprise fee should not have been deleted + visit admin_enterprise_fees_path + page.should have_selector "input[value='#{fee.name}']" + EnterpriseFee.find(fee.id).should_not be_nil + end end diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index 9f4f1f9490..fc36554af3 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -26,15 +26,4 @@ feature 'shipping methods' do page.should have_content "That shipping method cannot be deleted as it is referenced by an order: #{o.number}." Spree::ShippingMethod.find(@sm.id).should_not be_nil end - - scenario "deleting a shipping method referenced by a product distribution" do - p = create(:product) - d = create(:distributor_enterprise) - create(:product_distribution, product: p, distributor: d, shipping_method: @sm) - - visit_delete spree.admin_shipping_method_path(@sm) - - page.should have_content "That shipping method cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." - Spree::ShippingMethod.find(@sm.id).should_not be_nil - end end From c645ad25a789609d240a75e34399373e86d58f21 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 7 Aug 2013 15:06:12 +1000 Subject: [PATCH 08/13] Add more order privileges for distributor enterprise users --- app/models/spree/ability_decorator.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 540f052168..f5879a0a13 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -19,13 +19,14 @@ class AbilityDecorator #User can only access orders that they are a distributor for can [:index, :create], Spree::Order - can [:admin, :read, :update, :fire, :resend ], Spree::Order do |order| # :customer, :return_authorizations + can [:admin, :read, :update, :fire, :resend ], Spree::Order do |order| user.enterprises.include? order.distributor end - can [:admin, :index, :read, :create, :edit], Spree::Payment # , :fire, :capture, - can [:admin, :index, :read, :create, :edit], Spree::Shipment #edit order shipment doesn't work - can [:admin, :index, :read, :create, :edit], Spree::Adjustment + can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::Payment + can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::Shipment + can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::Adjustment + can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::ReturnAuthorization end end From 2ae4efbebaad79104ef94884cd29bb1bd738d7b6 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 7 Aug 2013 15:06:36 +1000 Subject: [PATCH 09/13] Add more order privileges for distributor enterprise users --- .../admin/orders/customer_details_controller_decorator.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 app/controllers/spree/admin/orders/customer_details_controller_decorator.rb diff --git a/app/controllers/spree/admin/orders/customer_details_controller_decorator.rb b/app/controllers/spree/admin/orders/customer_details_controller_decorator.rb new file mode 100644 index 0000000000..fe98451154 --- /dev/null +++ b/app/controllers/spree/admin/orders/customer_details_controller_decorator.rb @@ -0,0 +1,8 @@ +Spree::Admin::Orders::CustomerDetailsController.class_eval do + #Override BaseController.authorize_admin to inherit CanCan permissions for the current order + def authorize_admin + load_order unless @order + authorize! :admin, @order + authorize! params[:action].to_sym, @order + end +end \ No newline at end of file From c915bb91cfd86b958dd6907f82cef78a6e005065 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 7 Aug 2013 16:03:00 +1000 Subject: [PATCH 10/13] More tests for distributor enterprise user roles --- spec/models/ability_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 077147d514..b757eb744e 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -92,6 +92,26 @@ module Spree it "should not be able to read/write other enterprises' orders" do should_not have_ability([:admin, :index, :read, :edit], for: o2) end + + it "should be able to create a new order" do + should have_ability(:create, for: Spree::Order) + end + + it "should be able to read/write Payments on a product" do + should have_ability([:admin, :index, :read, :create, :edit, :update, :fire], for: Spree::Payment) + end + + it "should be able to read/write Shipments on a product" do + should have_ability([:admin, :index, :read, :create, :edit, :update, :fire], for: Spree::Shipment) + end + + it "should be able to read/write Adjustments on a product" do + should have_ability([:admin, :index, :read, :create, :edit, :update, :fire], for: Spree::Adjustment) + end + + it "should be able to read/write ReturnAuthorizations on a product" do + should have_ability([:admin, :index, :read, :create, :edit, :update, :fire], for: Spree::ReturnAuthorization) + end end end From 5baf9938e12533bd1b6983d67717bf8edeb907e7 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 9 Aug 2013 14:10:08 +1000 Subject: [PATCH 11/13] Ensure capture shortcut button shows for the correct payment --- app/views/spree/admin/orders/_capture.html.haml | 10 ++++++---- libpeerconnection.log | 0 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 libpeerconnection.log diff --git a/app/views/spree/admin/orders/_capture.html.haml b/app/views/spree/admin/orders/_capture.html.haml index 80a7481066..327c5cf320 100644 --- a/app/views/spree/admin/orders/_capture.html.haml +++ b/app/views/spree/admin/orders/_capture.html.haml @@ -1,5 +1,7 @@ --# copied from backend/app/views/spree/admin/payments/_list.html.erb - +- # Get the payment in 'checkout' state if any, and show capture button - if order.payments.present? - - order.payments.last.actions.grep(/^capture$/).each do |action| - = link_to_with_icon "icon-#{action}", t(action), fire_admin_order_payment_path(order, order.payments.last, :e => action), :method => :put, :no_text => true, :data => {:action => action} + - payment = order.payments.select{|p| p if p.state == 'checkout'}.first + - if !payment.nil? + - payment.actions.grep(/^capture$/).each do |action| + - # copied from backend/app/views/spree/admin/payments/_list.html.erb + = link_to_with_icon "icon-#{action}", t(action), fire_admin_order_payment_path(order, payment, :e => action), :method => :put, :no_text => true, :data => {:action => action} diff --git a/libpeerconnection.log b/libpeerconnection.log new file mode 100644 index 0000000000..e69de29bb2 From 03b8b042ba677a6fcd767ad30c23cf99742a6f04 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 9 Aug 2013 14:11:04 +1000 Subject: [PATCH 12/13] Deleted: libpeerconnection.log --- libpeerconnection.log | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 libpeerconnection.log diff --git a/libpeerconnection.log b/libpeerconnection.log deleted file mode 100644 index e69de29bb2..0000000000 From c5f0848a56f77d62e3671ff196b786037bcc9fb3 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 9 Aug 2013 15:42:11 +1000 Subject: [PATCH 13/13] More comments --- .../spree/admin/payments_controller_decorator.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/spree/admin/payments_controller_decorator.rb b/app/controllers/spree/admin/payments_controller_decorator.rb index df9e77f154..4a58dbfe14 100644 --- a/app/controllers/spree/admin/payments_controller_decorator.rb +++ b/app/controllers/spree/admin/payments_controller_decorator.rb @@ -1,6 +1,12 @@ # When a user fires an event, take them back to where they came from # Responder: http://guides.spreecommerce.com/developer/logic.html#overriding-controller-action-responses +# For some strange reason, adding PaymentsController.class_eval will cause gems/spree/app/controllers/spree/admin/payments_controller.rb:37 to error: +# payments_url not defined. +# This could be fixed by replacing line 37 with: +# respond_with(@payment, location: admin_order_payments_url) { |format| format.html { redirect_to admin_order_payments_path(@order) } } + + Spree::Admin::PaymentsController.class_eval do respond_override :fire => { :html => { :success => lambda { redirect_to request.referer # Keeps any filter and sort prefs