When viewing shipping methods, only show those for the distributor I select

This commit is contained in:
Rohan Mitchell
2013-10-18 10:01:11 +11:00
parent c07c52085a
commit 3b5e6f5742
6 changed files with 55 additions and 10 deletions

View File

@@ -3,14 +3,18 @@ Spree::Admin::PaymentMethodsController.class_eval do
# ! Redundant code copied from Spree::Admin::ResourceController with modifications marked
def collection
return parent.send(controller_name) if parent_data.present?
collection = if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class)
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
managed_by(spree_current_user).by_distributor # This line added
else
model_class.scoped.
managed_by(spree_current_user).by_distributor # this line added
managed_by(spree_current_user).by_distributor # This line added
end
# This block added
if params.key? :enterprise_id
distributor = Enterprise.find params[:enterprise_id]
collection = collection.for_distributor(distributor)

View File

@@ -7,13 +7,25 @@ module Spree
# ! Code copied from Spree::Admin::ResourceController with two added lines
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).
by_distributor # this line added
else
model_class.scoped.
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).
by_distributor # This line added
else
model_class.scoped.
by_distributor # This line added
end
# This block added
if params.key? :enterprise_id
distributor = Enterprise.find params[:enterprise_id]
collection = collection.for_distributor(distributor)
end
collection
end
# This method was originally written because ProductDistributions referenced shipping

View File

@@ -2,6 +2,11 @@ Spree::ShippingMethod.class_eval do
has_and_belongs_to_many :distributors, join_table: 'distributors_shipping_methods', :class_name => 'Enterprise', association_foreign_key: 'distributor_id'
attr_accessible :distributor_ids
scope :for_distributor, lambda { |distributor|
joins(:distributors).
where('enterprises.id = ?', distributor)
}
scope :by_distributor, lambda {
joins(:distributors).
order('enterprises.name, spree_shipping_methods.name').

View File

@@ -42,7 +42,7 @@
<%= link_to_delete_enterprise enterprise %><br />
<%= link_to_with_icon 'icon-chevron-right', 'Payment Methods', spree.admin_payment_methods_path(enterprise_id: enterprise.id) %> (<%= enterprise.payment_methods.count %>)<br />
<%= link_to_with_icon 'icon-plane', 'Shipping Methods', spree.admin_shipping_methods_path %> (<%= enterprise.shipping_methods.count %>)<br />
<%= link_to_with_icon 'icon-plane', 'Shipping Methods', spree.admin_shipping_methods_path(enterprise_id: enterprise.id) %> (<%= enterprise.shipping_methods.count %>)<br />
<%#= link_to_with_icon 'icon-money', 'Enterprise Fees', main_app.admin_enterprise_fees_path %>
</td>
</tr>

View File

@@ -83,5 +83,20 @@ feature 'shipping methods' do
shipping_method = Spree::ShippingMethod.find_by_name('Teleport')
shipping_method.distributors.should == [distributor1]
end
it "shows me only payment methods for the enterprise I select" do
sm1
sm2
click_link 'Enterprises'
within(".enterprise-#{distributor1.id}") { click_link 'Shipping Methods' }
page.should have_content sm1.name
page.should_not have_content sm2.name
click_link 'Enterprises'
within(".enterprise-#{distributor2.id}") { click_link 'Shipping Methods' }
page.should_not have_content sm1.name
page.should have_content sm2.name
end
end
end

View File

@@ -18,6 +18,15 @@ module Spree
sm.reload.distributors.should == [d1, d2]
end
it "finds shipping methods for a particular distributor" do
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
sm1 = create(:shipping_method, distributors: [d1, d1])
sm2 = create(:shipping_method, distributors: [d2, d2])
ShippingMethod.for_distributor(d1).should == [sm1]
end
it "can order by distributor" do
d1 = create(:distributor_enterprise, name: '222')
d2 = create(:distributor_enterprise, name: '111')