From be37df18d5ee12502f83c9cdebe8a11707c04caf Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Sat, 27 Jan 2024 17:03:15 +0100 Subject: [PATCH] Rubocop -A --- .rubocop_todo.yml | 10 +++++----- core/app/models/spree/simple_order_contents.rb | 2 +- legacy_promotions/app/models/spree/promotion.rb | 2 +- .../spree/promotion/order_adjustments_recalculator.rb | 2 +- .../app/models/spree/promotion/rules/first_order.rb | 6 ++---- .../app/models/spree/promotion/rules/user_logged_in.rb | 2 +- legacy_promotions/app/models/spree/promotion_code.rb | 2 +- .../app/models/spree/promotion_code/batch_builder.rb | 1 + .../app/models/spree/promotion_handler/coupon.rb | 6 ++---- .../app/models/spree/promotion_handler/page.rb | 2 +- .../config/initializers/solidus_legacy_promotions.rb | 1 - .../migrations/promotions_with_code_handlers.rb | 1 + legacy_promotions/solidus_legacy_promotions.gemspec | 2 +- .../models/spree/promotion/rules/option_value_spec.rb | 2 +- legacy_promotions/spec/rails_helper.rb | 2 +- 15 files changed, 20 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a581319b805..6cc05dea150 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -311,7 +311,7 @@ Rails/ApplicationController: # This cop supports unsafe autocorrection (--autocorrect-all). Rails/ApplicationJob: Exclude: - - "core/app/jobs/spree/promotion_code_batch_job.rb" + - "legacy_promotions/app/jobs/spree/promotion_code_batch_job.rb" # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). @@ -330,7 +330,7 @@ Rails/ApplicationRecord: - "core/db/migrate/20170412103617_transform_tax_rate_category_relation.rb" - "core/db/migrate/20180322142651_add_amount_remaining_to_store_credit_events.rb" - "core/db/migrate/20180710170104_create_spree_store_credit_reasons_table.rb" - - "core/db/migrate/20190106184413_remove_code_from_spree_promotions.rb" + - "legacy_promotions/db/migrate/20190106184413_remove_code_from_spree_promotions.rb" - "core/spec/lib/calculated_adjustments_spec.rb" - "core/spec/models/spree/validations/db_maximum_length_validator_spec.rb" - "core/spec/models/spree/wallet_payment_source_spec.rb" @@ -476,8 +476,8 @@ Rails/ReflectionClassName: Exclude: - "core/app/models/spree/credit_card.rb" - "core/app/models/spree/order.rb" - - "core/app/models/spree/promotion/rules/user.rb" - - "core/app/models/spree/promotion_rule_user.rb" + - "legacy_promotions/app/models/spree/promotion/rules/user.rb" + - "legacy_promotions/app/models/spree/promotion_rule_user.rb" - "core/app/models/spree/role_user.rb" - "core/app/models/spree/store_credit.rb" - "core/app/models/spree/user_address.rb" @@ -638,7 +638,7 @@ Style/OptionalBooleanParameter: - "core/app/mailers/spree/reimbursement_mailer.rb" - "core/app/models/concerns/spree/user_address_book.rb" - "core/app/models/spree/order.rb" - - "core/app/models/spree/order_contents.rb" + - "core/app/models/spree/simple_order_contents.rb" - "core/app/models/spree/stock/estimator.rb" # Offense count: 4 diff --git a/core/app/models/spree/simple_order_contents.rb b/core/app/models/spree/simple_order_contents.rb index 6e50556342e..50c5d944150 100644 --- a/core/app/models/spree/simple_order_contents.rb +++ b/core/app/models/spree/simple_order_contents.rb @@ -110,7 +110,7 @@ def remove_from_line_item(variant, quantity, options = {}) def grab_line_item_by_variant(variant, raise_error = false, options = {}) line_item = order.find_line_item_by_variant(variant, options) - if !line_item.present? && raise_error + if line_item.blank? && raise_error raise ActiveRecord::RecordNotFound, "Line item not found for variant #{variant.sku}" end diff --git a/legacy_promotions/app/models/spree/promotion.rb b/legacy_promotions/app/models/spree/promotion.rb index d901fbebc0c..996ccd9365d 100644 --- a/legacy_promotions/app/models/spree/promotion.rb +++ b/legacy_promotions/app/models/spree/promotion.rb @@ -231,7 +231,7 @@ def remove_from(order) actions.each do |action| action.remove_from(order) end - # note: this destroys the join table entry, not the promotion itself + # NOTE: this destroys the join table entry, not the promotion itself order.promotions.destroy(self) order.order_promotions.reset order_promotions.reset diff --git a/legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb b/legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb index ed848a4cdc5..c46a17deb29 100644 --- a/legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb +++ b/legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb @@ -2,7 +2,6 @@ module Spree class Promotion < Spree::Base - # This class encapsulates all the things the promotion system does to # an order. It is called from the `Spree::OrderUpdater` before taxes are # calculated, such that taxes always respect promotions. @@ -43,6 +42,7 @@ def call private attr_reader :order + delegate :line_items, :shipments, to: :order # Recalculate and persist the amount from this adjustment's source based on diff --git a/legacy_promotions/app/models/spree/promotion/rules/first_order.rb b/legacy_promotions/app/models/spree/promotion/rules/first_order.rb index a2a0ec7538f..fa1f7e2bee0 100644 --- a/legacy_promotions/app/models/spree/promotion/rules/first_order.rb +++ b/legacy_promotions/app/models/spree/promotion/rules/first_order.rb @@ -14,10 +14,8 @@ def eligible?(order, options = {}) @user = order.try(:user) || options[:user] @email = order.email - if user || email - if !completed_orders.blank? && completed_orders.first != order - eligibility_errors.add(:base, eligibility_error_message(:not_first_order), error_code: :not_first_order) - end + if (user || email) && (completed_orders.present? && completed_orders.first != order) + eligibility_errors.add(:base, eligibility_error_message(:not_first_order), error_code: :not_first_order) end eligibility_errors.empty? diff --git a/legacy_promotions/app/models/spree/promotion/rules/user_logged_in.rb b/legacy_promotions/app/models/spree/promotion/rules/user_logged_in.rb index 69341dad81c..21af3976e5f 100644 --- a/legacy_promotions/app/models/spree/promotion/rules/user_logged_in.rb +++ b/legacy_promotions/app/models/spree/promotion/rules/user_logged_in.rb @@ -9,7 +9,7 @@ def applicable?(promotable) end def eligible?(order, _options = {}) - unless order.user.present? + if order.user.blank? eligibility_errors.add(:base, eligibility_error_message(:no_user_specified), error_code: :no_user_specified) end eligibility_errors.empty? diff --git a/legacy_promotions/app/models/spree/promotion_code.rb b/legacy_promotions/app/models/spree/promotion_code.rb index bae81171f3d..fc1d9d6d666 100644 --- a/legacy_promotions/app/models/spree/promotion_code.rb +++ b/legacy_promotions/app/models/spree/promotion_code.rb @@ -33,7 +33,7 @@ def usage_count(excluded_orders: []) complete. where.not(spree_orders: { state: :canceled }). joins(:order_promotions). - where(spree_orders_promotions: { promotion_code_id: self.id }). + where(spree_orders_promotions: { promotion_code_id: id }). where.not(id: excluded_orders.map(&:id)). count end diff --git a/legacy_promotions/app/models/spree/promotion_code/batch_builder.rb b/legacy_promotions/app/models/spree/promotion_code/batch_builder.rb index 26fa1d63745..60eb7873bba 100644 --- a/legacy_promotions/app/models/spree/promotion_code/batch_builder.rb +++ b/legacy_promotions/app/models/spree/promotion_code/batch_builder.rb @@ -2,6 +2,7 @@ class ::Spree::PromotionCode::BatchBuilder attr_reader :promotion_code_batch, :options + delegate :promotion, :number_of_codes, :base_code, to: :promotion_code_batch DEFAULT_OPTIONS = { diff --git a/legacy_promotions/app/models/spree/promotion_handler/coupon.rb b/legacy_promotions/app/models/spree/promotion_handler/coupon.rb index b5556601f83..2349c20efc8 100644 --- a/legacy_promotions/app/models/spree/promotion_handler/coupon.rb +++ b/legacy_promotions/app/models/spree/promotion_handler/coupon.rb @@ -50,10 +50,8 @@ def set_error_code(status_code, options = {}) end def promotion - @promotion ||= begin - if promotion_code && promotion_code.promotion.active? - promotion_code.promotion - end + @promotion ||= if promotion_code && promotion_code.promotion.active? + promotion_code.promotion end end diff --git a/legacy_promotions/app/models/spree/promotion_handler/page.rb b/legacy_promotions/app/models/spree/promotion_handler/page.rb index e22450f212d..b96825c41da 100644 --- a/legacy_promotions/app/models/spree/promotion_handler/page.rb +++ b/legacy_promotions/app/models/spree/promotion_handler/page.rb @@ -7,7 +7,7 @@ class Page def initialize(order, path) @order = order - @path = path.gsub(/\A\//, '') + @path = path.delete_prefix('/') end def activate diff --git a/legacy_promotions/config/initializers/solidus_legacy_promotions.rb b/legacy_promotions/config/initializers/solidus_legacy_promotions.rb index a24e9d71935..7f3db3ff353 100644 --- a/legacy_promotions/config/initializers/solidus_legacy_promotions.rb +++ b/legacy_promotions/config/initializers/solidus_legacy_promotions.rb @@ -56,5 +56,4 @@ %w[ Spree::Promotion::Actions::FreeShipping ].each { |klass| config.environment.promotions.shipping_actions << klass } - end diff --git a/legacy_promotions/lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb b/legacy_promotions/lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb index 133dbd05e7c..ea8d492a46b 100644 --- a/legacy_promotions/lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb +++ b/legacy_promotions/lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'spree/core' module SolidusLegacyPromotions diff --git a/legacy_promotions/solidus_legacy_promotions.gemspec b/legacy_promotions/solidus_legacy_promotions.gemspec index 3b39e199e1b..778d9fc367f 100644 --- a/legacy_promotions/solidus_legacy_promotions.gemspec +++ b/legacy_promotions/solidus_legacy_promotions.gemspec @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative '../core/lib/spree/core/version.rb' +require_relative '../core/lib/spree/core/version' Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY diff --git a/legacy_promotions/spec/models/spree/promotion/rules/option_value_spec.rb b/legacy_promotions/spec/models/spree/promotion/rules/option_value_spec.rb index 26eef3780db..706a44280e9 100644 --- a/legacy_promotions/spec/models/spree/promotion/rules/option_value_spec.rb +++ b/legacy_promotions/spec/models/spree/promotion/rules/option_value_spec.rb @@ -33,7 +33,7 @@ context "when there are any applicable line items" do before do rule.preferred_eligible_values = Hash[line_item.product.id => [ - line_item.variant.option_values.pluck(:id).first + line_item.variant.option_values.pick(:id) ]] end it { is_expected.to be true } diff --git a/legacy_promotions/spec/rails_helper.rb b/legacy_promotions/spec/rails_helper.rb index 84e8dc2d7a7..968d9c36d59 100644 --- a/legacy_promotions/spec/rails_helper.rb +++ b/legacy_promotions/spec/rails_helper.rb @@ -41,7 +41,7 @@ config.use_transactional_fixtures = true config.before :suite do - FileUtils.rm_rf(Rails.configuration.active_storage.service_configurations[:test][:root]) unless (ENV['DISABLE_ACTIVE_STORAGE'] == 'true') + FileUtils.rm_rf(Rails.configuration.active_storage.service_configurations[:test][:root]) unless ENV['DISABLE_ACTIVE_STORAGE'] == 'true' DatabaseCleaner.clean_with :truncation end