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.store_credit_category_proc as a simple way
to redefine how the reimbursement category should be fetched.

Add Spree::Reimbursement#store_credit_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 23, 2020
1 parent 6a21551 commit eeff1f0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
19 changes: 19 additions & 0 deletions core/app/models/spree/reimbursement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ 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.
#
# @param proc A proc that will accept a single "reimbursement" argument
class_attribute :store_credit_category_proc
self.store_credit_category_proc = ->(_reimbursement) {
name = Spree::Deprecation.silence { Spree::StoreCreditCategory.reimbursement_category_name }
name ||= I18n.t('spree.store_credit_category.default')

Spree::StoreCreditCategory.find_by(name: name) ||
Spree::StoreCreditCategory.first
}

include ::Spree::Config.state_machines.reimbursement

class << self
Expand Down Expand Up @@ -153,6 +167,11 @@ def return_all(created_by: nil)
perform!(created_by: created_by)
end

# @return [Spree::StoreCreditCategory]
def store_credit_category
store_credit_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.store_credit_category,
created_by: created_by,
memo: "Refund for uncreditable payments on order #{reimbursement.order.number}",
currency: reimbursement.order.currency
Expand Down
19 changes: 15 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,23 @@ 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.store_credit_category_proc= instead.
class_attribute :reimbursement_category_name
self.reimbursement_category_name = I18n.t('spree.store_credit_category.default')

def self.reimbursement_category(_reimbursement)
Spree::StoreCreditCategory.find_by(name: reimbursement_category_name) ||
Spree::StoreCreditCategory.first
# @deprecated
# Please use Spree::Reimbursement#store_credit_category instead.
def self.reimbursement_category(reimbursement)
reimbursement.store_credit_category
end

deprecate :reimbursement_category_name, deprecator: Spree::Deprecation
deprecate :reimbursement_category_name=, deprecator: Spree::Deprecation
deprecate :reimbursement_category_name?, deprecator: Spree::Deprecation
class << self
deprecate :reimbursement_category_name, deprecator: Spree::Deprecation
deprecate :reimbursement_category_name=, deprecator: Spree::Deprecation
deprecate :reimbursement_category, deprecator: Spree::Deprecation
end

def non_expiring?
Expand Down

0 comments on commit eeff1f0

Please sign in to comment.