Class: Accountability::OrderItem

Inherits:
ApplicationRecord show all
Defined in:
app/models/accountability/order_item.rb

Overview

An OrderItem represents a Product that has been (or is being) purchased They are stored in an OrderGroup, which acts like a shopping cart

Instance Method Summary collapse

Methods inherited from ApplicationRecord

validates_attributes

Instance Method Details

#accruable?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'app/models/accountability/order_item.rb', line 42

def accruable?
  return false if terminated?
  return false if .nil?

  order_group.complete?
end

#accrue_credit!Object



15
16
17
18
19
20
21
# File 'app/models/accountability/order_item.rb', line 15

def accrue_credit!
  return unless accruing?

  credit = credits.new account: 
  discounts.each { |discount| discount.apply(credit) }
  credit.save!
end

#accruing?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'app/models/accountability/order_item.rb', line 33

def accruing?
  return false unless accruable?
  return true if credits.none?
  return false if product.accrues_one_time?

  billing_cycle_threshold = product.billing_cycle_length.ago
  last_accruement_date.before? billing_cycle_threshold
end

#default_priceObject



53
54
55
# File 'app/models/accountability/order_item.rb', line 53

def default_price
  product.price
end

#last_accruement_dateObject



49
50
51
# File 'app/models/accountability/order_item.rb', line 49

def last_accruement_date
  credits.maximum(:created_at)
end

#source_recordsObject



78
79
80
81
82
83
# File 'app/models/accountability/order_item.rb', line 78

def source_records
  return [] if source_scope.empty?
  return [] if product.source_class.nil?

  product.source_class.where(**source_scope)
end

#terminate!(date: Time.current) ⇒ Object



23
24
25
# File 'app/models/accountability/order_item.rb', line 23

def terminate!(date: Time.current)
  update termination_date: date
end

#terminated?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'app/models/accountability/order_item.rb', line 27

def terminated?
  return false if termination_date.nil?

  termination_date.past?
end

#trigger_callback(trigger) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/accountability/order_item.rb', line 57

def trigger_callback(trigger)
  source_records.each do |record|
    next unless product.callbacks.has_key? trigger

    product.callbacks[trigger].each do |callback|
      data = { billable: .billable, offerable_category: product.offerable_template }

      arguments = callback[:params]
      keyword_arguments = arguments.extract_options!

      arguments = data.values_at(*arguments)
      keyword_arguments = keyword_arguments.to_h { |keyword, data_type| [keyword, data[data_type]] }

      params = arguments
      params << keyword_arguments if keyword_arguments.present?

      record.public_send(callback[:method_name], *params)
    end
  end
end