module ActiveRecordSnapshot::Snapped::ClassMethods

Public Instance Methods

snapped_attributes() click to toggle source
# File lib/active_record_snapshot/snapped.rb, line 27
def snapped_attributes
  snapshot_config
end
snapshot(attr_name, opts={}) click to toggle source

usage: snapshot :view_count, when: :dirty

# 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_name() click to toggle source
# File lib/active_record_snapshot/snapped.rb, line 18
def snapshot_class_name
  [self.name.singularize, 'Snapshot'].join
end
snapshot_table_name() click to toggle source
# File lib/active_record_snapshot/snapped.rb, line 22
def snapshot_table_name
  snapshot_class_name.tableize
end