From fb587a61b9a69bb261905ea530f42c4bfb113e2e Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 5 Feb 2020 16:17:24 +0100 Subject: [PATCH] Move the reimbursement category logic to Spree::Reimbursement Add Spree::Reimbursement.reimbursement_category_proc as a simple way to redefine how the reimbursement category should be fetched. Add Spree::Reimbursement#reimbursement_category as the most natural way to fetch a given category for a reimbursement. This fixes reimbursement_category_name ending up in a translation missing error. We need to lazily load the translation because, when the file is loaded, Rails will still have to load translations from the main application. --- core/app/models/spree/reimbursement.rb | 15 +++++++++++++++ .../reimbursement_type/reimbursement_helpers.rb | 2 +- core/app/models/spree/store_credit_category.rb | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/core/app/models/spree/reimbursement.rb b/core/app/models/spree/reimbursement.rb index 3e91130bbc2..6c9bd699b8e 100644 --- a/core/app/models/spree/reimbursement.rb +++ b/core/app/models/spree/reimbursement.rb @@ -59,6 +59,17 @@ class IncompleteReimbursementError < StandardError; end class_attribute :reimbursement_failure_hooks self.reimbursement_failure_hooks = [] + # The reimbursement_category_proc allows to fetch a specific reimbursement + # category for a given reimbursement, the fetched category is used as the + # category passed to Spree::Reimbursement::Credit.default_creditable_class. + class_attribute :reimbursement_category_proc + self.reimbursement_category_proc = -> reimbursement { + legacy_name = Spree::Deprecation.silence { Spree::StoreCreditCategory.reimbursement_category_name } + name = legacy_name || I18n.t('spree.store_credit_category.default') + + find_by(name: name) || first + } + include ::Spree::Config.state_machines.reimbursement class << self @@ -153,6 +164,10 @@ def return_all(created_by: nil) perform!(created_by: created_by) end + def reimbursement_category + reimbursement_category_proc.call(self) + end + private def calculate_total diff --git a/core/app/models/spree/reimbursement_type/reimbursement_helpers.rb b/core/app/models/spree/reimbursement_type/reimbursement_helpers.rb index cb14862ab23..3aad21664db 100644 --- a/core/app/models/spree/reimbursement_type/reimbursement_helpers.rb +++ b/core/app/models/spree/reimbursement_type/reimbursement_helpers.rb @@ -54,7 +54,7 @@ def create_creditable(reimbursement, unpaid_amount, created_by:) Spree::Reimbursement::Credit.default_creditable_class.new( user: reimbursement.order.user, amount: unpaid_amount, - category: Spree::StoreCreditCategory.reimbursement_category(reimbursement), + category: reimbursement.reimbursement_category, created_by: created_by, memo: "Refund for uncreditable payments on order #{reimbursement.order.number}", currency: reimbursement.order.currency diff --git a/core/app/models/spree/store_credit_category.rb b/core/app/models/spree/store_credit_category.rb index cc0aa8e1773..ecd4af79ae3 100644 --- a/core/app/models/spree/store_credit_category.rb +++ b/core/app/models/spree/store_credit_category.rb @@ -4,12 +4,21 @@ class Spree::StoreCreditCategory < Spree::Base class_attribute :non_expiring_credit_types self.non_expiring_credit_types = [Spree::StoreCreditType::NON_EXPIRING] + # @deprecated + # Please use Spree::Reimbursement.reimbursement_category_proc instead. class_attribute :reimbursement_category_name - self.reimbursement_category_name = I18n.t('spree.store_credit_category.default') + deprecate :reimbursement_category_name, deprecator: Spree::Deprecation + deprecate :reimbursement_category_name=, deprecator: Spree::Deprecation + deprecate :reimbursement_category_name?, deprecator: Spree::Deprecation - def self.reimbursement_category(_reimbursement) - Spree::StoreCreditCategory.find_by(name: reimbursement_category_name) || - Spree::StoreCreditCategory.first + class << self + deprecate :reimbursement_category_name, deprecator: Spree::Deprecation + deprecate :reimbursement_category_name=, deprecator: Spree::Deprecation + deprecate :reimbursement_category, deprecator: Spree::Deprecation + end + + def self.reimbursement_category(reimbursement) + Spree::Reimbursement.reimbursement_category_proc.call(reimbursement) end def non_expiring?