From a200168917ce07d95a6bbb202e85304cb51f2e6d Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 18 Dec 2024 19:25:05 +0100 Subject: [PATCH 1/2] Make Spree::Money autoloadable Spree::Money is a model that should be able to be autoloaded. --- .rubocop_todo.yml | 4 +- core/app/models/spree/money.rb | 120 +++++++++++++++++ core/lib/spree/core.rb | 1 - core/lib/spree/money.rb | 123 +----------------- core/spec/{lib => models}/spree/money_spec.rb | 0 5 files changed, 127 insertions(+), 121 deletions(-) create mode 100644 core/app/models/spree/money.rb rename core/spec/{lib => models}/spree/money_spec.rb (100%) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5bd856f79d9..e3d9e323b52 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -166,7 +166,7 @@ Layout/SpaceAroundOperators: - "backend/spec/features/admin/orders/order_details_spec.rb" - "bin/__rspec" - "bin/rspec" - - "core/lib/spree/money.rb" + - "core/app/models/spree/money.rb" - "core/spec/models/spree/order/number_generator_spec.rb" # Offense count: 8 @@ -413,7 +413,7 @@ Rails/OutputSafety: - "core/app/helpers/spree/base_helper.rb" - "core/app/helpers/spree/checkout_helper.rb" - "core/app/helpers/spree/products_helper.rb" - - "core/lib/spree/money.rb" + - "core/app/models/spree/money.rb" # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). diff --git a/core/app/models/spree/money.rb b/core/app/models/spree/money.rb new file mode 100644 index 00000000000..435bc80429f --- /dev/null +++ b/core/app/models/spree/money.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +module Spree + # Spree::Money is a relatively thin wrapper around Monetize which handles + # formatting via Spree::Config. + class Money + include Comparable + DifferentCurrencyError = Class.new(StandardError) + + class <(other) + if !other.respond_to?(:money) + raise TypeError, "Can't compare #{other.class} to Spree::Money" + end + if currency != other.currency + # By default, ::Money will try to run a conversion on `other.money` and + # try a comparison on that. We do not want any currency conversion to + # take place so we'll catch this here and raise an error. + raise( + DifferentCurrencyError, + "Can't compare #{currency} with #{other.currency}" + ) + end + @money <=> other.money + end + + # Delegates comparison to the internal ruby money instance. + # + # @see http://www.rubydoc.info/gems/money/Money/Arithmetic#%3D%3D-instance_method + def ==(other) + raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) + @money == other.money + end + + def -(other) + raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) + self.class.new(@money - other.money) + end + + def +(other) + raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) + self.class.new(@money + other.money) + end + end +end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index e5c3fe42794..95475bf1eda 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -105,7 +105,6 @@ class GatewayError < RuntimeError; end require 'spree/i18n' require 'spree/localized_number' -require 'spree/money' require 'spree/permitted_attributes' require 'spree/core/importer' diff --git a/core/lib/spree/money.rb b/core/lib/spree/money.rb index 435bc80429f..72f6898b795 100644 --- a/core/lib/spree/money.rb +++ b/core/lib/spree/money.rb @@ -1,120 +1,7 @@ # frozen_string_literal: true -module Spree - # Spree::Money is a relatively thin wrapper around Monetize which handles - # formatting via Spree::Config. - class Money - include Comparable - DifferentCurrencyError = Class.new(StandardError) - - class <(other) - if !other.respond_to?(:money) - raise TypeError, "Can't compare #{other.class} to Spree::Money" - end - if currency != other.currency - # By default, ::Money will try to run a conversion on `other.money` and - # try a comparison on that. We do not want any currency conversion to - # take place so we'll catch this here and raise an error. - raise( - DifferentCurrencyError, - "Can't compare #{currency} with #{other.currency}" - ) - end - @money <=> other.money - end - - # Delegates comparison to the internal ruby money instance. - # - # @see http://www.rubydoc.info/gems/money/Money/Arithmetic#%3D%3D-instance_method - def ==(other) - raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) - @money == other.money - end - - def -(other) - raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) - self.class.new(@money - other.money) - end - - def +(other) - raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) - self.class.new(@money + other.money) - end - end -end +Spree.deprecator.warn( + <<~MSG + The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. + MSG +) diff --git a/core/spec/lib/spree/money_spec.rb b/core/spec/models/spree/money_spec.rb similarity index 100% rename from core/spec/lib/spree/money_spec.rb rename to core/spec/models/spree/money_spec.rb From 95c39b208fcd04fb98a258cd6c475441da8a4462 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 9 Jan 2025 18:11:17 +0100 Subject: [PATCH 2/2] chore(spree/money): Fix rubocop style errors --- .rubocop_todo.yml | 1 - core/app/models/spree/money.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e3d9e323b52..9f6844d4994 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -166,7 +166,6 @@ Layout/SpaceAroundOperators: - "backend/spec/features/admin/orders/order_details_spec.rb" - "bin/__rspec" - "bin/rspec" - - "core/app/models/spree/money.rb" - "core/spec/models/spree/order/number_generator_spec.rb" # Offense count: 8 diff --git a/core/app/models/spree/money.rb b/core/app/models/spree/money.rb index 435bc80429f..046a4aa2e0b 100644 --- a/core/app/models/spree/money.rb +++ b/core/app/models/spree/money.rb @@ -7,7 +7,7 @@ class Money include Comparable DifferentCurrencyError = Class.new(StandardError) - class <