Sha256: 64ba5c3eea0826e946d1f374c3859e06b31020994d1ad0ebc6fdf9d60c2c82e6

Contents?: true

Size: 1.92 KB

Versions: 15

Compression:

Stored size: 1.92 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 :store_credit_reason, class_name: 'Spree::StoreCreditReason', inverse_of: :store_credit_events

    validates_presence_of :store_credit_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

15 entries across 15 versions & 1 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/store_credit_event.rb
solidus_core-2.8.6 app/models/spree/store_credit_event.rb
solidus_core-2.9.5 app/models/spree/store_credit_event.rb
solidus_core-2.9.4 app/models/spree/store_credit_event.rb
solidus_core-2.8.5 app/models/spree/store_credit_event.rb
solidus_core-2.9.3 app/models/spree/store_credit_event.rb
solidus_core-2.9.2 app/models/spree/store_credit_event.rb
solidus_core-2.9.1 app/models/spree/store_credit_event.rb
solidus_core-2.9.0 app/models/spree/store_credit_event.rb
solidus_core-2.9.0.rc.1 app/models/spree/store_credit_event.rb
solidus_core-2.8.4 app/models/spree/store_credit_event.rb
solidus_core-2.8.3 app/models/spree/store_credit_event.rb
solidus_core-2.8.2 app/models/spree/store_credit_event.rb
solidus_core-2.8.1 app/models/spree/store_credit_event.rb
solidus_core-2.8.0 app/models/spree/store_credit_event.rb