diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb new file mode 100644 index 0000000000..d1d367fc47 --- /dev/null +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/spree/admin/payments_controller_decorator.rb b/app/controllers/spree/admin/payments_controller_decorator.rb index 4a58dbfe14..23db5eca09 100644 --- a/app/controllers/spree/admin/payments_controller_decorator.rb +++ b/app/controllers/spree/admin/payments_controller_decorator.rb @@ -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 diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 363a469bd8..7d20e417e8 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -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 diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 6e0c6658bd..a7816a8462 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -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 diff --git a/app/overrides/spree/admin/payment_methods/_form/add_distributor_to_admin_payment_method_edit.html.haml.deface b/app/overrides/spree/admin/payment_methods/_form/add_distributor_to_admin_payment_method_edit.html.haml.deface index 8ac8e7aad9..d15ecec373 100644 --- a/app/overrides/spree/admin/payment_methods/_form/add_distributor_to_admin_payment_method_edit.html.haml.deface +++ b/app/overrides/spree/admin/payment_methods/_form/add_distributor_to_admin_payment_method_edit.html.haml.deface @@ -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 diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 0c397c6325..97685544a8 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -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" diff --git a/spec/factories.rb b/spec/factories.rb index 2ce408807f..7385352ac5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb b/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb index cd95945f8b..b26e275f35 100644 --- a/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb +++ b/spec/features/chili/enterprises_distributor_info_rich_text_feature_spec.rb @@ -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 bisento 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 diff --git a/spec/features/consumer/checkout_spec.rb b/spec/features/consumer/checkout_spec.rb index 882b1f0977..1de98ba573 100644 --- a/spec/features/consumer/checkout_spec.rb +++ b/spec/features/consumer/checkout_spec.rb @@ -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' diff --git a/spec/lib/open_food_web/order_and_distributor_report_spec.rb b/spec/lib/open_food_web/order_and_distributor_report_spec.rb index d003254f7b..63169c976e 100644 --- a/spec/lib/open_food_web/order_and_distributor_report_spec.rb +++ b/spec/lib/open_food_web/order_and_distributor_report_spec.rb @@ -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)