Skip to content

Commit

Permalink
Move the reimbursement category logic to Spree::Reimbursement
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elia committed Mar 13, 2020
1 parent c26de62 commit fb587a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 15 additions & 0 deletions core/app/models/spree/reimbursement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions core/app/models/spree/store_credit_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit fb587a6

Please sign in to comment.