diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb index fdb2c0953a..da98806027 100644 --- a/app/controllers/spree/admin/payment_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -1,7 +1,8 @@ module Spree module Admin PaymentMethodsController.class_eval do - before_filter :load_hubs, only: [:new, :edit, :create, :update] + before_filter :load_hubs, only: [:new, :edit, :update] + create.before :load_hubs # Only show payment methods that user has access to and sort by distributor name # ! Redundant code copied from Spree::Admin::ResourceController with modifications marked diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index c7cdf3a7e4..37661463c9 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -4,6 +4,8 @@ Spree::PaymentMethod.class_eval do attr_accessible :distributor_ids + validates :distributors, presence: { message: "^At least one hub must be selected" } + # -- Scopes scope :managed_by, lambda { |user| if user.has_spree_role?('admin') diff --git a/spec/features/admin/payment_method_spec.rb b/spec/features/admin/payment_method_spec.rb index 660a187b3d..a5e8cbccec 100644 --- a/spec/features/admin/payment_method_spec.rb +++ b/spec/features/admin/payment_method_spec.rb @@ -46,14 +46,18 @@ feature %q{ login_to_admin_as enterprise_user end - it "creates payment methods" do + it "I can get to the new enterprise page" do click_link 'Enterprises' within(".enterprise-#{distributor1.id}") { click_link 'Payment Methods' } click_link 'New Payment Method' + current_path.should == spree.new_admin_payment_method_path + end + it "creates payment methods" do + visit spree.new_admin_payment_method_path fill_in 'payment_method_name', :with => 'Cheque payment method' - select distributor1.name, :from => 'payment_method_distributor_ids' + check "payment_method_distributor_ids_#{distributor1.id}" click_button 'Create' flash_message.should == 'Payment Method has been successfully created!' diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index ef70afd426..bed15fdbef 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -9,5 +9,11 @@ module Spree PaymentMethod.by_name.should == [pm2, pm3, pm1] end + + it "raises errors when required fields are missing" do + pm = PaymentMethod.new() + pm.save + pm.errors.to_a.should == ["Name can't be blank", "At least one hub must be selected"] + end end end