Class: Accountability::Product
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Accountability::Product
- Defined in:
- app/models/accountability/product.rb
Constant Summary collapse
- RECURRING_SCHEDULES =
i[weekly monthly annually].freeze
Instance Method Summary collapse
- #active? ⇒ Boolean
- #available_inventory ⇒ Object
- #billing_cycle_length ⇒ Object
- #inventory ⇒ Object
-
#inventory_items ⇒ Object
Returns an Inventory object containing each record within the product’s scope.
- #offerable_template ⇒ Object
-
#scopes ⇒ Object
TODO: Update offerable_template.scopes to return an array of Scope objects and delegate to that instead.
- #source_class ⇒ Object
Methods inherited from ApplicationRecord
Instance Method Details
#active? ⇒ Boolean
23 24 25 26 27 |
# File 'app/models/accountability/product.rb', line 23 def active? return false unless activation_date.present? activation_date.past? end |
#available_inventory ⇒ Object
49 50 51 52 53 |
# File 'app/models/accountability/product.rb', line 49 def available_inventory return [] if source_class.nil? source_class.where(**source_scope).public_send(offerable_template.whitelist) end |
#billing_cycle_length ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/models/accountability/product.rb', line 29 def billing_cycle_length case schedule when 'weekly' then 1.week when 'monthly' then 1.month when 'annually' then 1.year end end |
#inventory ⇒ Object
43 44 45 46 47 |
# File 'app/models/accountability/product.rb', line 43 def inventory return [] if source_class.nil? source_class.where(**source_scope) end |
#inventory_items ⇒ Object
Returns an Inventory object containing each record within the product’s scope. This method will eventually phase out the ‘inventory` and `available_inventory` methods.
39 40 41 |
# File 'app/models/accountability/product.rb', line 39 def inventory_items Inventory.new(self) end |
#offerable_template ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/accountability/product.rb', line 65 def offerable_template return if offerable_category.nil? return @offerable if @offerable.present? offerable = Offerable.collection[offerable_category.to_sym] return offerable if new_record? if offerable.present? @offerable = offerable else raise_offerable_not_found end end |
#scopes ⇒ Object
TODO: Update offerable_template.scopes to return an array of Scope objects and delegate to that instead
56 57 58 59 60 61 62 63 |
# File 'app/models/accountability/product.rb', line 56 def scopes return @scopes if @scopes.present? @scopes = offerable_template.scopes.map do |attribute, params| params.merge! source_class: source_class, attribute: attribute Offerable::Scope.new(**params) end end |
#source_class ⇒ Object
80 81 82 83 84 |
# File 'app/models/accountability/product.rb', line 80 def source_class return if offerable_template.nil? offerable_template.class_name.constantize end |