class Snapshotable::SnapshotCreator
Attributes
record[R]
Public Class Methods
new(record)
click to toggle source
# File lib/services/snapshot_creator.rb, line 5 def initialize(record) @record = record end
Public Instance Methods
call()
click to toggle source
# File lib/services/snapshot_creator.rb, line 9 def call snapshot_attrs end
Private Instance Methods
add_custom_attributes(snapshot)
click to toggle source
# File lib/services/snapshot_creator.rb, line 35 def add_custom_attributes(snapshot) return false unless custom_snapshot_attributes&.any? custom_snapshot_attributes.each do |key, attribute| snapshot[key] = record.send(attribute) end end
add_deep_snapshot_objects(snapshot)
click to toggle source
# File lib/services/snapshot_creator.rb, line 43 def add_deep_snapshot_objects(snapshot) deep_snapshot_attrs&.each do |association_name, attributes| association = record.send(association_name) snapshot["#{association_name}_object"] = if association.class.name == 'ActiveRecord::Associations::CollectionProxy' association.order(:id).map { |model| extract_attributes(attributes, model) } else extract_attributes(attributes, association) end end end
add_foreign_key(snapshot)
click to toggle source
# File lib/services/snapshot_creator.rb, line 29 def add_foreign_key(snapshot) return false if record.snapshot_foreign_key.nil? snapshot[record.snapshot_foreign_key.to_s] = record.id end
custom_snapshot_attributes()
click to toggle source
# File lib/services/snapshot_creator.rb, line 71 def custom_snapshot_attributes @custom_snapshot_attributes ||= record.class.custom_snapshot_attributes.first end
deep_snapshot_attrs()
click to toggle source
# File lib/services/snapshot_creator.rb, line 67 def deep_snapshot_attrs @deep_snapshot_attrs ||= record.class.attributes_to_save_on_snapshot.select { |attr| attr.is_a? Hash }.first end
extract_attributes(attributes, model)
click to toggle source
# File lib/services/snapshot_creator.rb, line 55 def extract_attributes(attributes, model) return {} if model.blank? attributes.inject({}) do |collected_attrs, attr| collected_attrs.merge(attr => model.send(attr)) end end
record_snapshot_attrs()
click to toggle source
# File lib/services/snapshot_creator.rb, line 63 def record_snapshot_attrs @record_snapshot_attrs ||= record.class.attributes_to_save_on_snapshot.select { |attr| attr.is_a? Symbol } end
snapshot_attrs()
click to toggle source
# File lib/services/snapshot_creator.rb, line 17 def snapshot_attrs snapshot = {} snapshot[:object] = extract_attributes(record_snapshot_attrs, record) if record_snapshot_attrs.any? add_foreign_key(snapshot) add_custom_attributes(snapshot) add_deep_snapshot_objects(snapshot) snapshot end