Module: Amountable::ActAsMethod

Defined in:
lib/amountable.rb

Instance Method Summary collapse

Instance Method Details

#act_as_amountable(options = {}) ⇒ Object

Possible storage values: [:table, :jsonb]



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/amountable.rb', line 59

def act_as_amountable(options = {})
  self.extend Amountable::ClassMethod
  class_attribute :amount_names
  class_attribute :amount_sets
  class_attribute :amounts_column_name
  class_attribute :storage
  self.amount_sets = Hash.new { |h, k| h[k] = Set.new }
  self.amount_names = Set.new
  self.amounts_column_name = 'amounts'
  self.storage = (options[:storage] || :table).to_sym
  case self.storage
  when :table
    has_many :amounts, class_name: 'Amountable::Amount', as: :amountable, dependent: :destroy, autosave: false
    include Amountable::TableMethods
  when :jsonb
    self.amounts_column_name = options[:column].to_s if options[:column]
    include Amountable::JsonbMethods
  else
    raise ArgumentError.new("Please specify a storage: #{ALLOWED_STORAGE}")
  end
  validate :validate_amount_names
  include Amountable::InstanceMethods
end