Module: Historyable::ClassMethods

Defined in:
lib/historyable.rb

Instance Method Summary collapse

Instance Method Details

#has_history(*attributes) ⇒ Object

Parameters:

  • attributes (Array, Symbol)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/historyable.rb', line 19

def has_history(*attributes)
  self.historyable_items = attributes.map do |attribute|
    attribute_name   = attribute.to_sym
    association_name = attribute.to_s.insert(-1, '_changes').to_sym

    Historyable::Item.new(attribute_name, association_name)
  end

  # Associations
  historyable_items.each do |historyable|
    has_many historyable.association_name,
             as:         :item,
             class_name: '::Change',
             dependent:  :destroy
  end

  # Instance methods
  historyable_items.each do |historyable|
    define_historyable_attribute_history_raw(historyable)
    define_historyable_attribute_history(historyable)
    define_historyable_attribute_history?(historyable)
  end

  # Callbacks
  around_save :save_changes
end