Module: DataSnapshots::ActiveRecordExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/data_snapshots/active_record_extension.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/data_snapshots/active_record_extension.rb', line 9

def method_missing(method, *args, &block)
  return super(method, *args, &block) unless method.to_s.end_with?('_snapshots')
  match_data = method.to_s.match(/(?<snapshot_name>\w+)_snapshots/)
  name = match_data[:snapshot_name]
  fetch_snapshots(name)
end

Instance Method Details

#generate_snapshot(name:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/data_snapshots/active_record_extension.rb', line 16

def generate_snapshot(name:)
  snapshot = DataSnapshots.configuration.snapshots[name]

  unless snapshot
    raise UnregisteredSnapshotError.new("Snapshot: #{name} has not been registered")
  end

  data = {}
  snapshot[:methods].each do |name, method|
    data[name] = method.call(self)
  end

  DataSnapshots::Snapshot.create!(
    name: name,
    model_id: self.id,
    model_type: self.class.to_s,
    data: data
  )
end