active_snapshot 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b48af4ad66af3d9fe9110f544df49f29ec6586969cb7e2c7f62431b54163a84
4
- data.tar.gz: fddc58303f066d5b77b56304c4367874e2b3dda0ea2e76e710d81a0f1705861e
3
+ metadata.gz: dcfaf7d239a170431fa82d70ab56b18bfb54a327b46376017357368b38f484b3
4
+ data.tar.gz: 833d8fb9ca610983f9a9e510cd6fc94776f043bf3d35ec7e052a2ade87cf1e5a
5
5
  SHA512:
6
- metadata.gz: 9a769fccb45b11f7163384b6ee79e5ff056b897cb3f382bb48054eaf9e20dba87371ab6b42da4ac117eafd136ebdc5d2dcd3441c7cf3a397ea087ec481dbc61d
7
- data.tar.gz: 2ca41b8874d1511596e25893ddb9b0b05daeead16a12150ff25b1812b1dd3f2901fdcad501ece2681b4a96161e8909e5fd475d52e8c7eda72d74e83804b38574
6
+ metadata.gz: a412ace11cbeee26528d5e3b58116a1b716f415ab4c1aead7978e6c5f2ccd1db49ee7c08ea0120226d1f66d1d099384cd6cadec5353a3f2fa5aee28d32b825e8
7
+ data.tar.gz: 5290672baca7ed50d67e9654acb85851e997f9c601d6fa6aae898cf51ba448686595e30b61c4819eafe6f81608850777a9b75ca5a995c18f9f6b6d8047542bf8
data/CHANGELOG.md CHANGED
@@ -2,12 +2,21 @@ CHANGELOG
2
2
  ---------
3
3
 
4
4
  - **Unreleased**
5
- * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.4.0...master)
5
+ * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.0...master)
6
6
  * Nothing yet
7
7
 
8
+ - **v0.5.0** - Nov 8, 2024
9
+ * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.4.0...v0.5.0)
10
+ * [#61](https://github.com/westonganger/active_snapshot/pull/61) - Ensure snapshot record returned by `create_snapshot!` is `valid?`
11
+ * [#60](https://github.com/westonganger/active_snapshot/pull/60) - Store enum value as integer
12
+ * [#56](https://github.com/westonganger/active_snapshot/pull/56) - Add presence validation for object in SnapshotItem model
13
+ * [#57](https://github.com/westonganger/active_snapshot/pull/57) - Add readonly argument to `Shapshot#fetch_reified_items`
14
+ * [#53](https://github.com/westonganger/active_snapshot/pull/53) - Allow `ActiveSnapshot.config` to be called before ActiveRecord `on_load` hook has occurred
15
+ * [#52](https://github.com/westonganger/active_snapshot/pull/52) - Remove deprecated positional argument on `create_snapshot!`
16
+
8
17
  - **v0.4.0** - July 23, 2024
9
18
  * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.2...v0.4.0)
10
- * [#44](https://github.com/westonganger/active_snapshot/pull/44) - Remove dependency on activerecord-import with vanilla ActiveRecord upsert_all
19
+ * [#44](https://github.com/westonganger/active_snapshot/pull/44) - Remove dependency on `activerecord-import` with vanilla ActiveRecord `upsert_all`
11
20
 
12
21
  - **v0.3.2** - Oct 17, 2023
13
22
  * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.1...v0.3.2)
data/README.md CHANGED
@@ -9,15 +9,15 @@ Simplified snapshots and restoration for ActiveRecord models and associations wi
9
9
  Key Features:
10
10
 
11
11
  - Create and Restore snapshots of a parent record and any specified child records
12
- - Predictible and explicit behaviour provides much needed clarity to your restore logic
12
+ - Predictable and explicit behaviour provides much needed clarity to your restore logic
13
13
  - Snapshots are created upon request only, we do not use any callbacks
14
14
  - Tiny method footprint so its easy to completely override the logic later
15
15
 
16
16
  Why This Library:
17
17
 
18
- Model Versioning and Restoration require concious thought, design, and understanding. You should understand your versioning and restoration process completely. This gem's small API and fully understandable design fully supports this.
18
+ Model Versioning and Restoration require conscious thought, design, and understanding. You should understand your versioning and restoration process completely. This gem's small API and fully understandable design fully supports this.
19
19
 
20
- I do not recommend using paper_trail-association_tracking because it is mostly a blackbox solution which encourages you to set it up and then assume its Just Working<sup>TM</sup>. This makes for major data problems later. Dont fall into this trap. Instead read this gems brief source code completely before use OR copy the code straight into your codebase. Once you know it, then you are free.
20
+ I do not recommend using [paper_trail-association_tracking](https://github.com/westonganger/paper_trail-association_tracking) because it is mostly a blackbox solution which encourages you to set it up and then assume its Just Working<sup>TM</sup>. This makes for major data problems later. Dont fall into this trap. Instead read this gems brief source code completely before use OR copy the code straight into your codebase. Once you know it, then you are free.
21
21
 
22
22
 
23
23
 
@@ -139,12 +139,14 @@ You can view all of the reified snapshot items by calling the following method.
139
139
  reified_parent, reified_children_hash = snapshot.fetch_reified_items
140
140
  ```
141
141
 
142
- As a safety these records have the `@readonly = true` attribute set on them. If you want to perform any write actions on the returned instances you will have to set `@readonly = nil`.
142
+ As a safety these records have the `readonly` attribute set on them.
143
+ If you want to perform any write actions on the returned instances you will have to set the `readonly` attribute to `false`
143
144
 
144
145
  ```ruby
146
+ reified_parent, reified_children_hash = snapshot.fetch_reified_items(readonly: false)
147
+ # or
145
148
  reified_parent, reified_children_hash = snapshot.fetch_reified_items
146
-
147
- reified_parent.instance_variable_set("@readonly", false)
149
+ reified_children_hash.first.instance_variable_set("@readonly", false)
148
150
  ```
149
151
 
150
152
  # Key Models Provided & Additional Customizations
@@ -8,13 +8,7 @@ module ActiveSnapshot
8
8
  has_many :snapshot_items, as: :item, class_name: 'ActiveSnapshot::SnapshotItem'
9
9
  end
10
10
 
11
- def create_snapshot!(legacy_identifier=nil, identifier: nil, user: nil, metadata: nil)
12
- if identifier.nil? && legacy_identifier
13
- identifier = legacy_identifier
14
-
15
- ActiveSupport::Deprecation.warn(LEGACY_POSITIONAL_ARGUMENT_WARNING)
16
- end
17
-
11
+ def create_snapshot!(identifier: nil, user: nil, metadata: nil)
18
12
  snapshot = snapshots.create!({
19
13
  identifier: identifier,
20
14
  user_id: (user.id if user),
@@ -40,6 +34,8 @@ module ActiveSnapshot
40
34
 
41
35
  SnapshotItem.upsert_all(new_entries.map{|x| x.delete("id"); x }, returning: false)
42
36
 
37
+ snapshot.snapshot_items.reset # clear the association cache otherwise snapshot.valid? returns false
38
+
43
39
  snapshot
44
40
  end
45
41
 
@@ -127,7 +123,5 @@ module ActiveSnapshot
127
123
  end
128
124
  end
129
125
 
130
- LEGACY_POSITIONAL_ARGUMENT_WARNING = "Supplying the snapshots :identifier as a positional argument is now deprecated and will be removed in upcoming versions. Please supply the snapshot identifier using the :identifier keyword argument instead.".freeze
131
-
132
126
  end
133
127
  end
@@ -46,8 +46,15 @@ module ActiveSnapshot
46
46
  end
47
47
 
48
48
  def build_snapshot_item(instance, child_group_name: nil)
49
+ attributes = instance.attributes
50
+ attributes.each do |k, v|
51
+ if instance.class.defined_enums.key?(k)
52
+ attributes[k] = instance.class.defined_enums.fetch(k).fetch(v)
53
+ end
54
+ end
55
+
49
56
  self.snapshot_items.new({
50
- object: instance.attributes,
57
+ object: attributes,
51
58
  item_id: instance.id,
52
59
  item_type: instance.class.name,
53
60
  child_group_name: child_group_name,
@@ -96,7 +103,7 @@ module ActiveSnapshot
96
103
  return true
97
104
  end
98
105
 
99
- def fetch_reified_items
106
+ def fetch_reified_items(readonly: true)
100
107
  reified_children_hash = {}.with_indifferent_access
101
108
 
102
109
  reified_parent = nil
@@ -104,7 +111,9 @@ module ActiveSnapshot
104
111
  snapshot_items.each do |si|
105
112
  reified_item = si.item_type.constantize.new(si.object)
106
113
 
107
- reified_item.readonly!
114
+ if readonly
115
+ reified_item.readonly!
116
+ end
108
117
 
109
118
  key = si.child_group_name
110
119
 
@@ -12,22 +12,21 @@ module ActiveSnapshot
12
12
  validates :snapshot_id, presence: true
13
13
  validates :item_id, presence: true, uniqueness: { scope: [:snapshot_id, :item_type] }
14
14
  validates :item_type, presence: true
15
+ validates :object, presence: true
15
16
 
16
17
  def object
17
18
  return @object if @object
18
19
 
19
20
  if ActiveSnapshot.config.storage_method_json?
20
- @object = JSON.parse(self[:object])
21
+ @object = self[:object] ? JSON.parse(self[:object]) : {}
21
22
  elsif ActiveSnapshot.config.storage_method_yaml?
22
- yaml_method = "unsafe_load"
23
+ yaml_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
23
24
 
24
- if !YAML.respond_to?("unsafe_load")
25
- yaml_method = "load"
26
- end
27
-
28
- @object = YAML.send(yaml_method, self[:object])
25
+ @object = self[:object] ? YAML.public_send(yaml_method, self[:object]) : {}
29
26
  elsif ActiveSnapshot.config.storage_method_native_json?
30
27
  @object = self[:object]
28
+ else
29
+ raise StandardError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`"
31
30
  end
32
31
  end
33
32
 
@@ -1,3 +1,3 @@
1
1
  module ActiveSnapshot
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
@@ -3,27 +3,29 @@ require "active_snapshot/config"
3
3
 
4
4
  require 'active_support/lazy_load_hooks'
5
5
 
6
+ module ActiveSnapshot
7
+ @@config = ActiveSnapshot::Config.new
8
+
9
+ def self.config(&block)
10
+ if block_given?
11
+ block.call(@@config)
12
+ else
13
+ return @@config
14
+ end
15
+ end
16
+ end
17
+
6
18
  ActiveSupport.on_load(:active_record) do
7
19
  require "active_snapshot/models/snapshot"
8
20
  require "active_snapshot/models/snapshot_item"
9
21
 
10
22
  require "active_snapshot/models/concerns/snapshots_concern"
11
23
 
12
- module ActiveSnapshot
24
+ ActiveSnapshot.module_eval do
13
25
  extend ActiveSupport::Concern
14
26
 
15
27
  included do
16
28
  include ActiveSnapshot::SnapshotsConcern
17
29
  end
18
-
19
- @@config = ActiveSnapshot::Config.new
20
-
21
- def self.config(&block)
22
- if block_given?
23
- block.call(@@config)
24
- else
25
- return @@config
26
- end
27
- end
28
30
  end
29
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-23 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord