Class: Accountability::Product

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

Constant Summary collapse

RECURRING_SCHEDULES =
%i[weekly monthly annually].freeze

Instance Method Summary collapse

Methods inherited from ApplicationRecord

validates_attributes

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'app/models/accountability/product.rb', line 22

def active?
  return false unless activation_date.present?

  activation_date.past?
end

#available_inventoryObject



48
49
50
51
52
# File 'app/models/accountability/product.rb', line 48

def available_inventory
  return [] if source_class.nil?

  source_class.where(**source_scope).public_send(offerable_template.whitelist)
end

#billing_cycle_lengthObject



28
29
30
31
32
33
34
# File 'app/models/accountability/product.rb', line 28

def billing_cycle_length
  case schedule
  when 'weekly' then 1.week
  when 'monthly' then 1.month
  when 'annually' then 1.year
  end
end

#inventoryObject



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

def inventory
  return [] if source_class.nil?

  source_class.where(**source_scope)
end

#inventory_itemsObject

Returns an Inventory object containing each record within the product’s scope. This method will eventually phase out the ‘inventory` and `available_inventory` methods.



38
39
40
# File 'app/models/accountability/product.rb', line 38

def inventory_items
  Inventory.new(self)
end

#offerable_templateObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/accountability/product.rb', line 64

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

#scopesObject

TODO: Update offerable_template.scopes to return an array of Scope objects and delegate to that instead



55
56
57
58
59
60
61
62
# File 'app/models/accountability/product.rb', line 55

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_classObject



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

def source_class
  return if offerable_template.nil?

  offerable_template.class_name.constantize
end