From 1b6eeb0928d53c28bdc4dc032cb83bcdc5837780 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 31 Jul 2023 17:10:54 +1000 Subject: [PATCH] Reduce line length of PaymentMethod class Rubocop was complaining. So I found code that could be simplified and specced it before refactoring. --- app/models/spree/payment_method.rb | 14 ++++++-------- spec/models/spree/payment_method_spec.rb | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) 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) }