diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb index db2579e054..443e50a6d1 100644 --- a/app/controllers/spree/admin/payment_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -1,14 +1,21 @@ Spree::Admin::PaymentMethodsController.class_eval do # Only show payment methods that user has access to and sort by distributor name - # ! Redundant code copied from Spree::Admin::ResourceController with two added lines + # ! Redundant code copied from Spree::Admin::ResourceController with modifications marked def collection return parent.send(controller_name) if parent_data.present? - if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class) - model_class.accessible_by(current_ability, action). - managed_by(spree_current_user).by_distributor # this line added - else - model_class.scoped. - managed_by(spree_current_user).by_distributor # this line added + collection = if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class) + model_class.accessible_by(current_ability, action). + managed_by(spree_current_user).by_distributor # this line added + else + model_class.scoped. + managed_by(spree_current_user).by_distributor # this line added + end + + if params.key? :enterprise_id + distributor = Enterprise.find params[:enterprise_id] + collection = collection.for_distributor(distributor) end + + collection end -end \ No newline at end of file +end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 865ec5bcd5..a74a023ca8 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -14,6 +14,11 @@ Spree::PaymentMethod.class_eval do end } + scope :for_distributor, lambda { |distributor| + joins(:distributors). + where('enterprises.id = ?', distributor) + } + scope :by_distributor, lambda { joins(:distributors). order('enterprises.name, spree_payment_methods.name'). diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb index 8be8afdc37..c9332020f4 100644 --- a/app/views/admin/enterprises/index.html.erb +++ b/app/views/admin/enterprises/index.html.erb @@ -11,6 +11,14 @@ <%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %> + + + + + + + + @@ -23,14 +31,19 @@ <%= f.fields_for :collection do |enterprise_form| %> <% enterprise = enterprise_form.object %> - + <% end %> diff --git a/spec/features/admin/payment_method_spec.rb b/spec/features/admin/payment_method_spec.rb index e5bcadc821..948ea6f058 100644 --- a/spec/features/admin/payment_method_spec.rb +++ b/spec/features/admin/payment_method_spec.rb @@ -11,8 +11,7 @@ feature %q{ @distributors = (1..3).map { create(:distributor_enterprise) } end - #Create and Edit uses same partial form - context "creating a payment method", js: true do + describe "creating a payment method", js: true do scenario "assigning a distributor to the payment method" do login_to_admin_section @@ -28,7 +27,52 @@ feature %q{ flash_message.should == 'Payment Method has been successfully created!' payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method') - payment_method.distributors.should include(@distributors[0]) + payment_method.distributors.should == [@distributors[0]] + end + end + + context "as an enterprise user" do + let(:enterprise_user) { create_enterprise_user } + let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') } + let(:distributor2) { create(:distributor_enterprise, name: 'Second Distributor') } + let(:pm1) { create(:payment_method, name: 'One', distributors: [distributor1]) } + let(:pm2) { create(:payment_method, name: 'Two', distributors: [distributor2]) } + + before(:each) do + enterprise_user.enterprise_roles.build(enterprise: distributor1).save + enterprise_user.enterprise_roles.build(enterprise: distributor2).save + login_to_admin_as enterprise_user + end + + it "creates payment methods" do + click_link 'Enterprises' + within(".enterprise-#{distributor1.id}") { click_link 'Payment Methods' } + click_link 'New Payment Method' + + fill_in 'payment_method_name', :with => 'Cheque payment method' + + select distributor1.name, :from => 'payment_method_distributor_ids' + click_button 'Create' + + flash_message.should == 'Payment Method has been successfully created!' + + payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method') + payment_method.distributors.should == [distributor1] + end + + it "shows me only payment methods for the enterprise I select" do + pm1 + pm2 + + click_link 'Enterprises' + within(".enterprise-#{distributor1.id}") { click_link 'Payment Methods' } + page.should have_content pm1.name + page.should_not have_content pm2.name + + click_link 'Enterprises' + within(".enterprise-#{distributor2.id}") { click_link 'Payment Methods' } + page.should_not have_content pm1.name + page.should have_content pm2.name end end end diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb new file mode 100644 index 0000000000..0193aa1425 --- /dev/null +++ b/spec/models/spree/payment_method_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +module Spree + describe PaymentMethod do + it "finds payment methods for a particular distributor" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + pm1 = create(:payment_method, distributors: [d1, d1]) + pm2 = create(:payment_method, distributors: [d2, d2]) + + PaymentMethod.for_distributor(d1).should == [pm1] + end + end +end
Name
<%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %> <%= 'PP ' if enterprise.is_primary_producer %><%= 'D' if enterprise.is_distributor %> <%= enterprise_form.text_field :next_collection_at %> <%= enterprise.description %> - <%= link_to_edit enterprise, :class => 'edit' %>   - <%= link_to_delete_enterprise enterprise %> + + <%= link_to_with_icon('icon-edit', 'Edit Profile', main_app.admin_enterprise_path(enterprise), class: 'edit') %>
+ <%= link_to_delete_enterprise enterprise %>
+ + <%= link_to_with_icon 'icon-chevron-right', 'Payment Methods', spree.admin_payment_methods_path(enterprise_id: enterprise.id) %> (<%= enterprise.payment_methods.count %>)
+ <%#= link_to_with_icon 'icon-plane', 'Shipping Methods', spree.admin_shipping_methods_path %>
+ <%#= link_to_with_icon 'icon-money', 'Enterprise Fees', main_app.admin_enterprise_fees_path %>