Sha256: 64948cf9ba6f89e439d3a8268cc7a0ba1fe99a4c893042bb8db0121bc8e81419

Contents?: true

Size: 1.62 KB

Versions: 41

Compression:

Stored size: 1.62 KB

Contents

module Spree
  class StoreCreditEvent < Spree::Base
    acts_as_paranoid

    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_event_date
      I18n.l(created_at.to_date, format: :long)
    end

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

    def order
      Spree::Payment.find_by_response_code(authorization_code).try(:order)
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
solidus_core-1.4.0.rc1 app/models/spree/store_credit_event.rb
solidus_core-2.0.0.beta1 app/models/spree/store_credit_event.rb
solidus_core-1.4.0.beta1 app/models/spree/store_credit_event.rb
solidus_core-1.3.1 app/models/spree/store_credit_event.rb
solidus_core-1.3.0 app/models/spree/store_credit_event.rb
solidus_core-1.3.0.rc2 app/models/spree/store_credit_event.rb
solidus_core-1.3.0.rc1 app/models/spree/store_credit_event.rb
solidus_core-1.3.0.beta1 app/models/spree/store_credit_event.rb
solidus_core-1.2.2 app/models/spree/store_credit_event.rb
solidus_core-1.2.1 app/models/spree/store_credit_event.rb
solidus_core-1.1.3 app/models/spree/store_credit_event.rb
solidus_core-1.2.0 app/models/spree/store_credit_event.rb
solidus_core-1.2.0.rc2 app/models/spree/store_credit_event.rb
solidus_core-1.1.2 app/models/spree/store_credit_event.rb
solidus_core-1.2.0.rc1 app/models/spree/store_credit_event.rb
solidus_core-1.2.0.beta1 app/models/spree/store_credit_event.rb
solidus_core-1.1.1 app/models/spree/store_credit_event.rb
solidus_core-1.1.0 app/models/spree/store_credit_event.rb
solidus_core-1.1.0.pre2 app/models/spree/store_credit_event.rb
solidus_core-1.1.0.pre1 app/models/spree/store_credit_event.rb