diff --git a/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index 6113291103..891f418eed 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -28,14 +28,12 @@ module Spree scope :production, -> { where(environment: 'production') } scope :managed_by, lambda { |user| - if user.has_spree_role?('admin') - where(nil) - else - joins(:distributors). - where('distributors_payment_methods.distributor_id IN (?)', - user.enterprises.select(&:id)). - select('DISTINCT spree_payment_methods.*') - end + return where(nil) if user.admin? + + joins(:distributors). + where('distributors_payment_methods.distributor_id IN (?)', + user.enterprises.select(&:id)). + select('DISTINCT spree_payment_methods.*') } scope :for_distributors, ->(distributors) { diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index 8951832e56..d69895d934 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -6,6 +6,25 @@ class Spree::Gateway::Test < Spree::Gateway end describe Spree::PaymentMethod do + describe ".managed_by scope" do + subject! { create(:payment_method) } + let(:owner) { subject.distributors.first.owner } + let(:other_user) { create(:user) } + let(:admin) { create(:admin_user) } + + it "returns everything for admins" do + expect(Spree::PaymentMethod.managed_by(admin)).to eq [subject] + end + + it "returns payment methods of managed enterprises" do + expect(Spree::PaymentMethod.managed_by(owner)).to eq [subject] + end + + it "returns nothing for other users" do + expect(Spree::PaymentMethod.managed_by(other_user)).to eq [] + end + end + describe "#available" do let(:enterprise) { create(:enterprise) }