Module: ActiveSnapshot::SnapshotsConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_snapshot/models/concerns/snapshots_concern.rb

Instance Method Summary collapse

Instance Method Details

#children_to_snapshotObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_snapshot/models/concerns/snapshots_concern.rb', line 38

def children_to_snapshot
  snapshot_children_proc = self.class.has_snapshot_children

  if !snapshot_children_proc
    return {}
  else
    records = self.instance_exec(&snapshot_children_proc)

    if records.is_a?(Hash)
      records = records.with_indifferent_access
    else
      raise ArgumentError.new("Invalid `has_snapshot_children` definition. Must return a Hash")
    end

    snapshot_children = {}.with_indifferent_access

    records.each do |assoc_name, opts|
      snapshot_children[assoc_name] = {}

      if opts.nil?
        ### nil is allowed value in case has_one/belongs_to is nil, etc.
        snapshot_children[assoc_name][:records] = []

      elsif opts.is_a?(ActiveRecord::Base)
        ### Support belongs_to / has_one
        snapshot_children[assoc_name][:records] = [opts]

      elsif opts.is_a?(ActiveRecord::Relation) || opts.is_a?(Array)
        snapshot_children[assoc_name][:records] = opts

      elsif opts.is_a?(Hash)
        opts = opts.with_indifferent_access

        if opts.has_key?(:records)
          records = opts[:records]
        elsif opts.has_key?(:record)
          records = opts[:record]
        end

        if records.nil?
          # Do nothing, allow nil value in case a has_one/belong_to returns nil, etc.
        elsif records
          if records.respond_to?(:to_a)
            records = records.to_a
          else
            records = [records]
          end

          snapshot_children[assoc_name][:records] = records
        else
          raise ArgumentError.new("Invalid `has_snapshot_children` definition. Must define a :records key for each child association.")
        end

        delete_method = opts[:delete_method]

        if delete_method.present? && delete_method.to_s != "default"
          if delete_method.respond_to?(:call)
            snapshot_children[assoc_name][:delete_method] = delete_method
          else
            raise ArgumentError.new("Invalid `has_snapshot_children` definition. Invalid :delete_method argument. Must be a Lambda / Proc")
          end
        end

      else
        raise ArgumentError.new("Invalid `has_snapshot_children` definition. Invalid :records argument. Must be a Hash or Array")
      end
    end

    return snapshot_children
  end
end

#create_snapshot!(identifier: nil, user: nil, metadata: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_snapshot/models/concerns/snapshots_concern.rb', line 13

def create_snapshot!(identifier: nil, user: nil, metadata: nil)
  snapshot = Snapshot.build_snapshot(self, identifier: identifier, user: user, metadata: )
  new_entries = snapshot.snapshot_items.map(&:attributes)
  snapshot.snapshot_items.reset # clear the association cache otherwise snapshot.valid? returns false
  snapshot.save!

  new_entries = new_entries.map { |item| item.except("id").merge(created_at: snapshot.created_at, snapshot_id: snapshot.id) }

  SnapshotItem.upsert_all(new_entries, returning: false)

  snapshot
end