Reduce line length of PaymentMethod class

Rubocop was complaining. So I found code that could be simplified and
specced it before refactoring.
This commit is contained in:
Maikel Linke
2023-07-31 17:10:54 +10:00
parent faeb8ae8f8
commit 1b6eeb0928
2 changed files with 25 additions and 8 deletions

View File

@@ -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) {

View File

@@ -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) }