From ef17fd7d8001eeec424e323378082299ed862216 Mon Sep 17 00:00:00 2001 From: Feruz Oripov Date: Mon, 26 Feb 2024 23:41:48 +0500 Subject: [PATCH] Cleanup --- app/controllers/spree/users_controller.rb | 2 +- ....rb => payments_requiring_action_query.rb} | 4 +-- ...complete_orders_with_balance_query_spec.rb | 12 +++---- .../complete_visible_orders_query_spec.rb | 8 ++--- .../customers_with_balance_query_spec.rb | 34 +++++++++---------- ...> payments_requiring_action_query_spec.rb} | 10 +++--- 6 files changed, 35 insertions(+), 35 deletions(-) rename app/queries/{payments_requiring_action.rb => payments_requiring_action_query.rb} (83%) rename spec/queries/{payments_requiring_action_spec.rb => payments_requiring_action_query_spec.rb} (70%) diff --git a/app/controllers/spree/users_controller.rb b/app/controllers/spree/users_controller.rb index 4feff9bf7a..ad4e9082ae 100644 --- a/app/controllers/spree/users_controller.rb +++ b/app/controllers/spree/users_controller.rb @@ -16,7 +16,7 @@ module Spree before_action :set_locale def show - @payments_requiring_action = PaymentsRequiringAction.new(spree_current_user).query + @payments_requiring_action = PaymentsRequiringActionQuery.new(spree_current_user).call @orders = orders_collection.includes(:line_items) customers = spree_current_user.customers diff --git a/app/queries/payments_requiring_action.rb b/app/queries/payments_requiring_action_query.rb similarity index 83% rename from app/queries/payments_requiring_action.rb rename to app/queries/payments_requiring_action_query.rb index ffe705e955..a680b94ddc 100644 --- a/app/queries/payments_requiring_action.rb +++ b/app/queries/payments_requiring_action_query.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -class PaymentsRequiringAction +class PaymentsRequiringActionQuery def initialize(user) @user = user end - def query + def call Spree::Payment.joins(:order).where("spree_orders.user_id = ?", user.id). requires_authorization end diff --git a/spec/queries/complete_orders_with_balance_query_spec.rb b/spec/queries/complete_orders_with_balance_query_spec.rb index ba9fb6e6a2..77deb34bec 100644 --- a/spec/queries/complete_orders_with_balance_query_spec.rb +++ b/spec/queries/complete_orders_with_balance_query_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe CompleteOrdersWithBalanceQuery do - let(:result) { described_class.new(user).call } + subject { described_class.new(user).call } describe '#call' do let(:user) { order.user } @@ -28,16 +28,16 @@ describe CompleteOrdersWithBalanceQuery do allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) expect(outstanding_balance).to receive(:call) - result + subject end it 'returns complete orders including their balance' do - order = result.first + order = subject.first expect(order[:balance_value]).to eq(-1.0) end it 'sorts them by their completed_at with the most recent first' do - expect(result.pluck(:id)).to eq([other_order.id, order.id]) + expect(subject.pluck(:id)).to eq([other_order.id, order.id]) end end @@ -48,11 +48,11 @@ describe CompleteOrdersWithBalanceQuery do allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) expect(outstanding_balance).to receive(:call) - result + subject end it 'returns an empty array' do - expect(result).to be_empty + expect(subject).to be_empty end end end diff --git a/spec/queries/complete_visible_orders_query_spec.rb b/spec/queries/complete_visible_orders_query_spec.rb index 982c567e1f..61ffc5f733 100644 --- a/spec/queries/complete_visible_orders_query_spec.rb +++ b/spec/queries/complete_visible_orders_query_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe CompleteVisibleOrdersQuery do - subject(:result) { described_class.new(order_permissions).call } + subject { described_class.new(order_permissions).call } let(:filter_canceled) { false } describe '#call' do @@ -20,7 +20,7 @@ describe CompleteVisibleOrdersQuery do let(:cart_order) { create(:order, distributor: enterprise) } it 'does not return it' do - expect(result).not_to include(cart_order) + expect(subject).not_to include(cart_order) end end @@ -28,13 +28,13 @@ describe CompleteVisibleOrdersQuery do let(:complete_order) { create(:order, completed_at: 1.day.ago, distributor: enterprise) } it 'does not return it' do - expect(result).to include(complete_order) + expect(subject).to include(complete_order) end end it 'calls #visible_orders' do expect(order_permissions).to receive(:visible_orders).and_call_original - result + subject end end end diff --git a/spec/queries/customers_with_balance_query_spec.rb b/spec/queries/customers_with_balance_query_spec.rb index 76208eb32a..44d24ac14e 100644 --- a/spec/queries/customers_with_balance_query_spec.rb +++ b/spec/queries/customers_with_balance_query_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe CustomersWithBalanceQuery do - subject(:result) { described_class.new(Customer.where(id: customers)).call } + subject { described_class.new(Customer.where(id: customers)).call } describe '#call' do let(:customers) { create(:customer) } @@ -15,7 +15,7 @@ describe CustomersWithBalanceQuery do allow(OutstandingBalanceQuery).to receive(:new).and_return(outstanding_balance) expect(outstanding_balance).to receive(:statement) - result + subject end describe 'arguments' do @@ -23,8 +23,8 @@ describe CustomersWithBalanceQuery do let(:customers) { create_pair(:customer) } it 'returns balance' do - expect(result.pluck(:id).sort).to eq([customers.first.id, customers.second.id].sort) - expect(result.map(&:balance_value)).to eq([0, 0]) + expect(subject.pluck(:id).sort).to eq([customers.first.id, customers.second.id].sort) + expect(subject.map(&:balance_value)).to eq([0, 0]) end end @@ -32,7 +32,7 @@ describe CustomersWithBalanceQuery do let(:customers) { Customer.none } it 'returns empty customers collection' do - expect(result).to eq([]) + expect(subject).to eq([]) end end end @@ -44,7 +44,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(0) end end @@ -56,7 +56,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(0) end end @@ -68,7 +68,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(0) end end @@ -80,7 +80,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(0) end end @@ -94,7 +94,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(-total) end end @@ -110,7 +110,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - total) end end @@ -132,7 +132,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - non_canceled_orders_total) end end @@ -148,7 +148,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - total) end end @@ -164,7 +164,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - total) end end @@ -180,7 +180,7 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - total) end end @@ -197,14 +197,14 @@ describe CustomersWithBalanceQuery do end it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(payment_total - non_returned_orders_total) end end context 'when there are no orders' do it 'returns the customer balance' do - customer = result.first + customer = subject.first expect(customer.balance_value).to eq(0) end end diff --git a/spec/queries/payments_requiring_action_spec.rb b/spec/queries/payments_requiring_action_query_spec.rb similarity index 70% rename from spec/queries/payments_requiring_action_spec.rb rename to spec/queries/payments_requiring_action_query_spec.rb index 77a9ca0404..8fcde56146 100644 --- a/spec/queries/payments_requiring_action_spec.rb +++ b/spec/queries/payments_requiring_action_query_spec.rb @@ -2,12 +2,12 @@ require 'spec_helper' -describe PaymentsRequiringAction do +describe PaymentsRequiringActionQuery do let(:user) { create(:user) } let(:order) { create(:order, user:) } - subject(:payments_requiring_action) { described_class.new(user) } + subject { described_class.new(user).call } - describe '#query' do + describe '#call' do context "payment has a cvv_response_message" do let(:payment) do create(:payment, @@ -17,7 +17,7 @@ describe PaymentsRequiringAction do end it "finds the payment" do - expect(payments_requiring_action.query.all).to include(payment) + expect(subject.all).to include(payment) end end @@ -27,7 +27,7 @@ describe PaymentsRequiringAction do end it "does not find the payment" do - expect(payments_requiring_action.query.all).to_not include(payment) + expect(subject.all).to_not include(payment) end end end