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



80
81
82
83
84
85
86
# File 'lib/snapshotable/model_config.rb', line 80

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



57
58
59
60
61
# File 'lib/snapshotable/model_config.rb', line 57

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



51
52
53
54
55
# File 'lib/snapshotable/model_config.rb', line 51

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_callback(klass) ⇒ Object



88
89
90
91
92
# File 'lib/snapshotable/model_config.rb', line 88

def setup_callback(klass)
  klass.after_create(
    :take_snapshot!
  )
end

#setup_custom_snapshot_attributesObject



63
64
65
66
67
# File 'lib/snapshotable/model_config.rb', line 63

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
20
# File 'lib/snapshotable/model_config.rb', line 13

def setup_snapshot
  setup_snapshot_names
  setup_variables
  setup_association(@model)
  setup_callback(@model)

  @model.send :include, ::Snapshotable::Model::InstanceMethods
end

#setup_snapshot_attributes(attributes) ⇒ Object



22
23
24
25
# File 'lib/snapshotable/model_config.rb', line 22

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



32
33
34
35
# File 'lib/snapshotable/model_config.rb', line 32

def setup_snapshot_custom(attributes)
  setup_snapshot unless snapshot_configured?
  @model.custom_snapshot_attributes = attributes || DEFAULT_CUSTOM_ATTRIBUTES
end

#setup_snapshot_ignore(attributes) ⇒ Object



27
28
29
30
# File 'lib/snapshotable/model_config.rb', line 27

def setup_snapshot_ignore(attributes)
  setup_snapshot unless snapshot_configured?
  @model.attributes_to_ignore_on_diff = attributes || DEFAULT_IGNORE_ATTRIBUTES
end

#setup_snapshot_namesObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/snapshotable/model_config.rb', line 69

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



41
42
43
44
45
46
47
48
49
# File 'lib/snapshotable/model_config.rb', line 41

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



98
99
100
# File 'lib/snapshotable/model_config.rb', line 98

def snapshot_association_name
  snapshot_class_name.pluralize.underscore.to_sym
end

#snapshot_class_nameObject



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

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

#snapshot_configured?Boolean

Returns:

  • (Boolean)


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

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

#snapshot_foreign_keyObject



102
103
104
# File 'lib/snapshotable/model_config.rb', line 102

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