diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index a0bed17042..e5c61ada05 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -7,6 +7,9 @@ Spree::Admin::ProductsController.class_eval do #respond_override :clone => { :json => {:success => lambda { redirect_to bulk_index_admin_products_url+"?q[id_eq]=#{@new.id}" } } } + def product_distributions + end + def bulk_update collection_hash = Hash[params[:_json].each_with_index.map { |p,i| [i,p] }] product_set = Spree::ProductSet.new({:collection_attributes => collection_hash}) diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 9bac09aa06..9ef4d30a13 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -10,7 +10,7 @@ class AbilityDecorator # Enterprise User can only access products that they are a supplier for can [:create], Spree::Product - can [:admin, :read, :update, :bulk_edit, :bulk_update, :clone, :destroy], Spree::Product do |product| + can [:admin, :read, :update, :product_distributions, :bulk_edit, :bulk_update, :clone, :destroy], Spree::Product do |product| user.enterprises.include? product.supplier end diff --git a/app/overrides/add_distributor_to_admin_product_edit.rb b/app/overrides/add_distributor_to_admin_product_edit.rb deleted file mode 100644 index de56bb11b6..0000000000 --- a/app/overrides/add_distributor_to_admin_product_edit.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/_form", - :insert_bottom => "[data-hook='admin_product_form_additional_fields']", - :partial => "spree/admin/products/distributors_form", - :name => "add_distributor_to_admin_product_edit", - :original => '5a6c66358efbce3e73eb60a168ac4914a6bcc27f') diff --git a/app/overrides/add_distributor_to_admin_product_new.rb b/app/overrides/add_distributor_to_admin_product_new.rb deleted file mode 100644 index d5a16476f8..0000000000 --- a/app/overrides/add_distributor_to_admin_product_new.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/new", - :insert_after => "[data-hook='new_product_attrs']", - :partial => "spree/admin/products/distributors_form", - :name => "add_distributor_to_admin_product_new", - :original => '59b7ed369769267bdedb596768fcfcc2cb94f122') \ No newline at end of file diff --git a/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface b/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface new file mode 100644 index 0000000000..3877d49345 --- /dev/null +++ b/app/overrides/spree/admin/shared/_product_tabs/add_distributions.html.haml.deface @@ -0,0 +1,5 @@ +/ insert_bottom "[data-hook='admin_product_tabs']" + +- klass = current == 'Product Distributions' ? 'active' : '' +%li{:class => klass} + = link_to_with_icon 'icon-tasks', 'Product Distributions', product_distributions_admin_product_url(@product) diff --git a/app/views/spree/admin/products/product_distributions.html.haml b/app/views/spree/admin/products/product_distributions.html.haml new file mode 100644 index 0000000000..0181a6b870 --- /dev/null +++ b/app/views/spree/admin/products/product_distributions.html.haml @@ -0,0 +1,8 @@ += render :partial => 'spree/admin/shared/product_sub_menu' += render :partial => 'spree/admin/shared/product_tabs', :locals => { :current => 'Product Distributions' } += render :partial => 'spree/shared/error_messages', :locals => { :target => @product } + += form_for [:admin, @product], :method => :put, :html => { :multipart => true } do |f| + %fieldset.no-border-top + = render :partial => 'distributors_form', :locals => { :f => f } + = render :partial => 'spree/admin/shared/edit_resource_links' diff --git a/config/routes.rb b/config/routes.rb index f74a92c144..8de04e4519 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,6 +75,8 @@ Spree::Core::Engine.routes.prepend do namespace :admin do resources :products do + get :product_distributions, on: :member + post :bulk_update, :on => :collection, :as => :bulk_update end end diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index f7245c1243..646ed70346 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -24,19 +24,26 @@ feature %q{ fill_in 'product_price', :with => '19.99' select 'New supplier', :from => 'product_supplier_id' - check @distributors[0].name - select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' - check @distributors[2].name - select @enterprise_fees[2].name, :from => 'product_product_distributions_attributes_2_enterprise_fee_id' - click_button 'Create' flash_message.should == 'Product "A new product !!!" has been successfully created!' product = Spree::Product.find_by_name('A new product !!!') product.supplier.should == @supplier + product.group_buy.should be_false + + # Distributors + within('#sidebar') { click_link 'Product Distributions' } + + check @distributors[0].name + select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' + check @distributors[2].name + select @enterprise_fees[2].name, :from => 'product_product_distributions_attributes_2_enterprise_fee_id' + + click_button 'Update' + + product.reload product.distributors.should == [@distributors[0], @distributors[2]] product.product_distributions.map { |pd| pd.enterprise_fee }.should == [@enterprise_fees[0], @enterprise_fees[2]] - product.group_buy.should be_false end scenario "making a group buy product" do @@ -86,6 +93,20 @@ feature %q{ page.should_not have_content @supplier.name end + click_button 'Create' + + flash_message.should == 'Product "A new product !!!" has been successfully created!' + product = Spree::Product.find_by_name('A new product !!!') + product.supplier.should == @supplier2 + end + + scenario "editing product distributions" do + product = create(:simple_product, supplier: @supplier2) + + click_link 'Products' + click_link product.name + within('#sidebar') { click_link 'Product Distributions' } + check @distributors[0].name select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id' @@ -95,14 +116,11 @@ feature %q{ page.should_not have_content @distributors[2].name end - click_button 'Create' + click_button 'Update' + flash_message.should == "Product \"#{product.name}\" has been successfully updated!" - flash_message.should == 'Product "A new product !!!" has been successfully created!' - product = Spree::Product.find_by_name('A new product !!!') - product.supplier.should == @supplier2 product.distributors.should == [@distributors[0]] end - end end end diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index d1604c12e1..53d755a4c2 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -32,11 +32,11 @@ module Spree let(:order) {create(:order)} it "should be able to read/write their enterprises' products" do - should have_ability([:admin, :read, :update, :bulk_edit, :bulk_update, :clone, :destroy], for: p1) + should have_ability([:admin, :read, :update, :product_distributions, :bulk_edit, :bulk_update, :clone, :destroy], for: p1) end it "should not be able to read/write other enterprises' products" do - should_not have_ability([:admin, :read, :update, :bulk_edit, :bulk_update, :clone, :destroy], for: p2) + should_not have_ability([:admin, :read, :update, :product_distributions, :bulk_edit, :bulk_update, :clone, :destroy], for: p2) end it "should be able to create a new product" do