Sha256: 415abb1b935b27d02831fe623fcc02d9dabe1f9fba84621376864842c01eff41

Contents?: true

Size: 1.88 KB

Versions: 27

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'discard'

module Spree
  class StoreCreditEvent < Spree::Base
    acts_as_paranoid
    include Spree::ParanoiaDeprecations

    include Discard::Model
    self.discard_column = :deleted_at

    belongs_to :store_credit
    belongs_to :originator, polymorphic: true
    belongs_to :update_reason, class_name: "Spree::StoreCreditUpdateReason"

    validates_presence_of :update_reason, if: :action_requires_reason?

    NON_EXPOSED_ACTIONS = [Spree::StoreCredit::ELIGIBLE_ACTION, Spree::StoreCredit::AUTHORIZE_ACTION]

    scope :exposed_events, -> { exposable_actions.not_invalidated }
    scope :exposable_actions, -> { where.not(action: NON_EXPOSED_ACTIONS) }
    scope :not_invalidated, -> { joins(:store_credit).where(spree_store_credits: { invalidated_at: nil }) }
    scope :chronological, -> { order(:created_at) }
    scope :reverse_chronological, -> { order(created_at: :desc) }

    delegate :currency, to: :store_credit

    def capture_action?
      action == Spree::StoreCredit::CAPTURE_ACTION
    end

    def authorization_action?
      action == Spree::StoreCredit::AUTHORIZE_ACTION
    end

    def action_requires_reason?
      [Spree::StoreCredit::ADJUSTMENT_ACTION, Spree::StoreCredit::INVALIDATE_ACTION].include?(action)
    end

    def display_amount
      Spree::Money.new(amount, { currency: currency })
    end

    def display_user_total_amount
      Spree::Money.new(user_total_amount, { currency: currency })
    end

    def display_remaining_amount
      Spree::Money.new(amount_remaining, { currency: currency })
    end

    def display_event_date
      I18n.l(created_at.to_date, format: :long)
    end

    def display_action
      return if NON_EXPOSED_ACTIONS.include?(action)
      I18n.t("spree.store_credit.display_action.#{action}")
    end

    def order
      Spree::Payment.find_by(response_code: authorization_code).try(:order)
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
solidus_core-2.6.6 app/models/spree/store_credit_event.rb
solidus_core-2.7.4 app/models/spree/store_credit_event.rb
solidus_core-2.7.3 app/models/spree/store_credit_event.rb
solidus_core-2.6.5 app/models/spree/store_credit_event.rb
solidus_core-2.7.2 app/models/spree/store_credit_event.rb
solidus_core-2.6.4 app/models/spree/store_credit_event.rb
solidus_core-2.6.3 app/models/spree/store_credit_event.rb
solidus_core-2.7.1 app/models/spree/store_credit_event.rb
solidus_core-2.6.2 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/store_credit_event.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/store_credit_event.rb