class Snapshotable::ModelConfig

Constants

DEFAULT_ATTRIBUTES
DEFAULT_CUSTOM_ATTRIBUTES
DEFAULT_IGNORE_ATTRIBUTES

Public Class Methods

new(model) click to toggle source
# File lib/snapshotable/model_config.rb, line 9
def initialize(model)
  @model = model
end

Public Instance Methods

setup_association(klass) click to toggle source
# File lib/snapshotable/model_config.rb, line 79
def setup_association(klass)
  klass.has_many(
    klass.snapshot_association_name,
    class_name: klass.snapshot_class_name,
    dependent: :destroy
  )
end
setup_attributes_to_ignore_on_diff() click to toggle source
# File lib/snapshotable/model_config.rb, line 56
def setup_attributes_to_ignore_on_diff
  @model.class_attribute :attributes_to_ignore_on_diff, instance_writer: false
  @model.attributes_to_ignore_on_diff = DEFAULT_IGNORE_ATTRIBUTES
  @model.send :attr_accessor, :attributes_to_ignore_on_diff
end
setup_attributes_to_save_on_snapshot() click to toggle source
# File lib/snapshotable/model_config.rb, line 50
def setup_attributes_to_save_on_snapshot
  @model.class_attribute :attributes_to_save_on_snapshot, instance_writer: false
  @model.attributes_to_save_on_snapshot = DEFAULT_ATTRIBUTES
  @model.send :attr_accessor, :attributes_to_save_on_snapshot
end
setup_custom_snapshot_attributes() click to toggle source
# File lib/snapshotable/model_config.rb, line 62
def setup_custom_snapshot_attributes
  @model.class_attribute :custom_snapshot_attributes, instance_writer: false
  @model.custom_snapshot_attributes = DEFAULT_CUSTOM_ATTRIBUTES
  @model.send :attr_accessor, :custom_snapshot_attributes
end
setup_snapshot() click to toggle source
# File lib/snapshotable/model_config.rb, line 13
def setup_snapshot
  setup_snapshot_names
  setup_variables
  setup_association(@model)

  @model.send :include, ::Snapshotable::Model::InstanceMethods
end
setup_snapshot_attributes(attributes) click to toggle source
# File lib/snapshotable/model_config.rb, line 21
def setup_snapshot_attributes(attributes)
  setup_snapshot unless snapshot_configured?
  @model.attributes_to_save_on_snapshot = attributes || DEFAULT_ATTRIBUTES
end
setup_snapshot_custom(attributes) click to toggle source
# File lib/snapshotable/model_config.rb, line 31
def setup_snapshot_custom(attributes)
  setup_snapshot unless snapshot_configured?
  @model.custom_snapshot_attributes = attributes || DEFAULT_CUSTOM_ATTRIBUTES
end
setup_snapshot_ignore(attributes) click to toggle source
# File lib/snapshotable/model_config.rb, line 26
def setup_snapshot_ignore(attributes)
  setup_snapshot unless snapshot_configured?
  @model.attributes_to_ignore_on_diff = attributes || DEFAULT_IGNORE_ATTRIBUTES
end
setup_snapshot_names() click to toggle source
# File lib/snapshotable/model_config.rb, line 68
def setup_snapshot_names
  @model.class_attribute :snapshot_association_name
  @model.snapshot_association_name = snapshot_association_name

  @model.class_attribute :snapshot_class_name
  @model.snapshot_class_name = snapshot_class_name

  @model.class_attribute :snapshot_foreign_key
  @model.snapshot_foreign_key = snapshot_foreign_key
end
setup_variables() click to toggle source
# File lib/snapshotable/model_config.rb, line 40
def setup_variables
  setup_attributes_to_save_on_snapshot
  setup_attributes_to_ignore_on_diff
  setup_custom_snapshot_attributes

  @model.class_attribute :snapshot_configured
  @model.snapshot_configured = true
  @model.send :attr_accessor, :snapshot_configured
end
snapshot_association_name() click to toggle source
# File lib/snapshotable/model_config.rb, line 91
def snapshot_association_name
  snapshot_class_name.pluralize.underscore.to_sym
end
snapshot_class_name() click to toggle source
# File lib/snapshotable/model_config.rb, line 87
def snapshot_class_name
  "#{@model.name}Snapshot"
end
snapshot_configured?() click to toggle source
# File lib/snapshotable/model_config.rb, line 36
def snapshot_configured?
  @model.respond_to?(:snapshot_configured) && @model.snapshot_configured
end
snapshot_foreign_key() click to toggle source
# File lib/snapshotable/model_config.rb, line 95
def snapshot_foreign_key
  "#{@model.name.downcase}_id"
end