Payment Method must belong to a Distributor

This commit is contained in:
David Cook
2013-08-21 15:14:01 +10:00
parent f5b326ee1a
commit b2878ddc40
10 changed files with 63 additions and 20 deletions

View File

@@ -0,0 +1,14 @@
Spree::Admin::PaymentMethodsController.class_eval do
# Only show payment methods that user has access to.
# ! Redundant 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).
managed_by(spree_current_user) # this line added
else
model_class.scoped.
managed_by(spree_current_user) # this line added
end
end
end

View File

@@ -1,14 +1,21 @@
# 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
# 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) } }
respond_override :fire => { :html => { :success => lambda {
redirect_to request.referer # Keeps any filter and sort prefs
} } }
append_before_filter :filter_payment_methods
# Only show payments for the order's distributor
def filter_payment_methods
@payment_methods = @payment_methods.select{ |pm| pm.has_distributor? @order.distributor}
@payment_method ||= @payment_methods.first
end
end

View File

@@ -30,7 +30,7 @@ class AbilityDecorator
#Enterprise User can only access payment methods for their distributors
can [:index, :create], Spree::PaymentMethod
can [:admin, :read, :update, :fire, :resend ], Spree::PaymentMethod do |payment_method|
can [:admin, :read, :update, :fire, :resend, :destroy ], Spree::PaymentMethod do |payment_method|
user.enterprises.include? payment_method.distributor
end

View File

@@ -1,6 +1,8 @@
Spree::PaymentMethod.class_eval do
belongs_to :distributor, :class_name => 'Enterprise'
validates_presence_of :distributor_id
attr_accessible :distributor_id
# -- Scopes
@@ -13,6 +15,10 @@ Spree::PaymentMethod.class_eval do
where('distributor_id IN (?)', user.enterprises)
end
}
def has_distributor?(distributor)
self.distributor == distributor
end
end
# Ensure that all derived classes also allow distributor_id

View File

@@ -5,5 +5,5 @@
= f.label :distributor
%br
= collection_select(:payment_method, :distributor_id, Enterprise.is_distributor.managed_by(spree_current_user), :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"})
= collection_select(:payment_method, :distributor_id, Enterprise.is_distributor.managed_by(spree_current_user), :id, :name, {:include_blank => false}, {:class => "select2 fullwidth"})
= f.error_message_on :distributor

View File

@@ -16,7 +16,6 @@ namespace :openfoodweb do
country = Spree::Country.find_by_name('Australia')
Spree::ZoneMember.create(:zone => zone, :zoneable => country)
FactoryGirl.create(:shipping_method, :zone => zone)
FactoryGirl.create(:payment_method, :environment => 'development')
end
@@ -96,6 +95,13 @@ namespace :openfoodweb do
end
end
# -- Enterprise Payment Methods
unless Spree::PaymentMethod.count > 1
Enterprise.is_distributor.each do |distributor|
FactoryGirl.create(:payment_method, distributor: distributor, name: "Cheque (#{distributor.name})", :environment => 'development')
end
end
# -- Products
unless Spree::Product.count > 0
puts "[#{task_name}] Seeding products"

View File

@@ -158,6 +158,18 @@ FactoryGirl.modify do
state { Spree::State.find_by_name 'Victoria' }
country { Spree::Country.find_by_name 'Australia' || Spree::Country.first }
end
factory :payment do
ignore do
distributor { order.distributor || Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise) }
end
payment_method { FactoryGirl.create(:payment_method, distributor: distributor) }
end
factory :payment_method do
distributor { Enterprise.is_distributor.first || FactoryGirl.create(:distributor_enterprise) } #Always need a distributor
end
end

View File

@@ -53,11 +53,11 @@ feature "enterprises distributor info as rich text" do
scenario "viewing distributor info", js: true do
ActionMailer::Base.deliveries.clear
setup_shipping_details
d = create(:distributor_enterprise, distributor_info: 'Chu ge sai yubi dan <strong>bisento</strong> tobi ashi yubi ge omote.', next_collection_at: 'Thursday 2nd May')
p = create(:product, :distributors => [d])
setup_shipping_details d
login_to_consumer_section
visit spree.select_distributor_order_path(d)
@@ -86,12 +86,12 @@ feature "enterprises distributor info as rich text" do
private
def setup_shipping_details
def setup_shipping_details(distributor)
zone = create(:zone)
c = Spree::Country.find_by_name('Australia')
Spree::ZoneMember.create(:zoneable => c, :zone => zone)
create(:shipping_method, zone: zone)
create(:payment_method, :description => 'Cheque payment method')
create(:payment_method, :description => 'Cheque payment method', distributor: distributor)
end

View File

@@ -66,7 +66,6 @@ feature %q{
sm = create(:shipping_method, zone: @zone, calculator: Spree::Calculator::FlatRate.new)
sm.calculator.set_preference(:amount, 0); sm.calculator.save!
@payment_method_all = create(:payment_method, :name => 'Cheque payment method', :description => 'Cheque payment method') #valid for any distributor
@payment_method_distributor = create(:payment_method, :name => 'Edible Garden payment method', :distributor => @distributor)
@payment_method_alternative = create(:payment_method, :name => 'Alternative Distributor payment method', :distributor => @distributor_alternative)
end
@@ -319,14 +318,13 @@ feature %q{
# -- Checkout: Payment
# Given the distributor I have selected for my order, I should only see payment methods valid for that distributor
page.should have_selector 'label', :text => @payment_method_all.name
page.should have_selector 'label', :text => @payment_method_distributor.name
page.should_not have_selector 'label', :text => @payment_method_alternative.name
click_checkout_continue_button
# -- Checkout: Order complete
page.should have_content 'Your order has been processed successfully'
page.should have_content @payment_method_all.description
page.should have_content @payment_method_distributor.description
page.should have_selector 'tfoot#order-charges tr.total td', text: 'Distribution'
page.should have_selector 'tfoot#order-charges tr.total td', text: '$3.00'

View File

@@ -14,7 +14,7 @@ module OpenFoodWeb
product_distribution = create(:product_distribution, :product => product, :distributor => @distributor)
@shipping_instructions = "pick up on thursday please!"
@order = create(:order, :distributor => @distributor, :bill_address => @bill_address, :special_instructions => @shipping_instructions)
@payment_method = create(:payment_method)
@payment_method = create(:payment_method, :distributor => @distributor)
payment = create(:payment, :payment_method => @payment_method, :order => @order )
@order.payments << payment
@line_item = create(:line_item, :product => product, :order => @order)