Class: Accountability::OrderItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Accountability::OrderItem
- 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
- #accruable? ⇒ Boolean
- #accrue_credit! ⇒ Object
- #accruing? ⇒ Boolean
- #default_price ⇒ Object
- #last_accruement_date ⇒ Object
- #price_override ⇒ Object
- #source_records ⇒ Object
- #terminate!(date: Time.current) ⇒ Object
- #terminated? ⇒ Boolean
- #trigger_callback(trigger) ⇒ Object
Methods inherited from ApplicationRecord
Instance Method Details
#accruable? ⇒ 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 account.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: account discounts.each { |discount| discount.apply(credit) } credit.save! end |
#accruing? ⇒ 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_price ⇒ Object
53 54 55 56 57 |
# File 'app/models/accountability/order_item.rb', line 53 def default_price return price_override.price if price_override.present? product.price end |
#last_accruement_date ⇒ Object
49 50 51 |
# File 'app/models/accountability/order_item.rb', line 49 def last_accruement_date credits.maximum(:created_at) end |
#price_override ⇒ Object
59 60 61 62 63 64 65 |
# File 'app/models/accountability/order_item.rb', line 59 def price_override return @price_override unless @price_override.nil? return unless source_records.one? # Memoize as `false` if nil to avoid re-running query @price_override = product.price_overrides.find_by(offerable_source: source_records) || false end |
#source_records ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/accountability/order_item.rb', line 88 def source_records return @source_records unless @source_records.nil? return [] if source_scope.empty? return [] if product.source_class.nil? @source_records = 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
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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/accountability/order_item.rb', line 67 def trigger_callback(trigger) source_records.each do |record| next unless product.callbacks.has_key? trigger product.callbacks[trigger].each do |callback| data = { billable: account.billable, offerable_category: product.offerable_template } arguments = callback[:params] keyword_arguments = arguments. 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 |