Class: Snapshotable::ModelConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshotable/model_config.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =
[].freeze
DEFAULT_IGNORE_ATTRIBUTES =
%w[id created_at updated_at].freeze
DEFAULT_CUSTOM_ATTRIBUTES =
{}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ModelConfig

Returns a new instance of ModelConfig.



9
10
11
# File 'lib/snapshotable/model_config.rb', line 9

def initialize(model)
  @model = model
end

Instance Method Details

#setup_association(klass) ⇒ Object



79
80
81
82
83
84
85
# 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_diffObject



56
57
58
59
60
# 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_snapshotObject



50
51
52
53
54
# 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_attributesObject



62
63
64
65
66
# 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_snapshotObject



13
14
15
16
17
18
19
# 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) ⇒ Object



21
22
23
24
# 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) ⇒ Object



31
32
33
34
# 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) ⇒ Object



26
27
28
29
# 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_namesObject



68
69
70
71
72
73
74
75
76
77
# 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_variablesObject



40
41
42
43
44
45
46
47
48
# 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_nameObject



91
92
93
# File 'lib/snapshotable/model_config.rb', line 91

def snapshot_association_name
  snapshot_class_name.pluralize.underscore.to_sym
end

#snapshot_class_nameObject



87
88
89
# File 'lib/snapshotable/model_config.rb', line 87

def snapshot_class_name
  "#{@model.name}Snapshot"
end

#snapshot_configured?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/snapshotable/model_config.rb', line 36

def snapshot_configured?
  @model.respond_to?(:snapshot_configured) && @model.snapshot_configured
end

#snapshot_foreign_keyObject



95
96
97
# File 'lib/snapshotable/model_config.rb', line 95

def snapshot_foreign_key
  "#{@model.name.downcase}_id"
end