Module: ActiveRecordSnapshot::Snapped::ClassMethods

Defined in:
lib/active_record_snapshot/snapped.rb

Instance Method Summary collapse

Instance Method Details

#snapped_attributesObject



27
28
29
# File 'lib/active_record_snapshot/snapped.rb', line 27

def snapped_attributes
  snapshot_config
end

#snapshot(attr_name, opts = {}) ⇒ Object

usage: snapshot :view_count, when: :dirty



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record_snapshot/snapped.rb', line 33

def snapshot(attr_name, opts={})
  mash_opts = Hashie::Mash.new(opts)

  if !self.attribute_names.include?(attr_name.to_s)
    raise ActiveRecord::UnknownAttributeError, "#{attr_name} is not an attribute of #{self.name}"
  end

  att_sym = attr_name.to_sym

  # code smell TODO, make SnapshotConfig its own class, rather than a managed Mash
  config_mash = Hashie::Mash.new


  val_when = case mash_opts[:when]
  when :always
    :always
  when :dirty
    :dirty
  when nil
    :dirty
  else
    raise ArgumentError, "#{mash_opts[:when]} is not a valid :when option. Only :dirty and :always allowed"
  end

  config_mash[:when] = val_when

  self.snapshot_config[att_sym] = config_mash
end

#snapshot_class_nameObject



18
19
20
# File 'lib/active_record_snapshot/snapped.rb', line 18

def snapshot_class_name
  [self.name.singularize, 'Snapshot'].join
end

#snapshot_table_nameObject



22
23
24
# File 'lib/active_record_snapshot/snapped.rb', line 22

def snapshot_table_name
  snapshot_class_name.tableize
end