Module: SpreeLoyaltyPoints::OrderDecorator

Included in:
Spree::Order
Defined in:
app/models/spree_loyalty_points/order_decorator.rb

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/spree_loyalty_points/order_decorator.rb', line 3

def self.prepended(base)
  base.include Spree::LoyaltyPoints
  base.include Spree::Order::LoyaltyPoints

  base.has_many :loyalty_points_transactions, as: :source
  base.has_many :loyalty_points_credit_transactions, as: :source
  base.has_many :loyalty_points_debit_transactions, as: :source

  base.scope :loyalty_points_not_awarded, -> {
    includes(:loyalty_points_credit_transactions).where(spree_loyalty_points_transactions: { source_id: nil })
  }

  base.scope :with_hours_since_payment, ->(num) {
    where('`spree_orders`.`paid_at` < ? ', num.hours.ago)
  }

  base.scope :with_uncredited_loyalty_points, ->(num) {
    with_hours_since_payment(num).loyalty_points_not_awarded
  }

  # FSM transitions
  base.state_machines[:state].after_transition(
    from: base.state_machines[:state].states.map(&:name) - [:complete],
    to: [:complete],
    do: :complete_loyalty_points_payments
  )
end