diff --git a/README.md b/README.md index aece418..b18b342 100644 --- a/README.md +++ b/README.md @@ -139,12 +139,14 @@ You can view all of the reified snapshot items by calling the following method. reified_parent, reified_children_hash = snapshot.fetch_reified_items ``` -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`. +As a safety these records have the `readonly` attribute set on them. +If you want to perform any write actions on the returned instances you will have to set the `readonly` attribute to `false` ```ruby +reified_parent, reified_children_hash = snapshot.fetch_reified_items(readonly: false) +# or reified_parent, reified_children_hash = snapshot.fetch_reified_items - -reified_parent.instance_variable_set("@readonly", false) +reified_children_hash.first.instance_variable_set("@readonly", false) ``` # Key Models Provided & Additional Customizations diff --git a/lib/active_snapshot/models/snapshot.rb b/lib/active_snapshot/models/snapshot.rb index 4b83ae9..2e30249 100644 --- a/lib/active_snapshot/models/snapshot.rb +++ b/lib/active_snapshot/models/snapshot.rb @@ -96,7 +96,7 @@ def restore! return true end - def fetch_reified_items + def fetch_reified_items(readonly: true) reified_children_hash = {}.with_indifferent_access reified_parent = nil @@ -104,7 +104,9 @@ def fetch_reified_items snapshot_items.each do |si| reified_item = si.item_type.constantize.new(si.object) - reified_item.readonly! + if readonly + reified_item.readonly! + end key = si.child_group_name diff --git a/test/models/snapshot_test.rb b/test/models/snapshot_test.rb index c98a052..6fc2795 100644 --- a/test/models/snapshot_test.rb +++ b/test/models/snapshot_test.rb @@ -94,7 +94,7 @@ def test_restore @snapshot.restore! end - def test_fetch_reified_items + def test_fetch_reified_items_with_readonly @snapshot = @snapshot_klass.first reified_items = @snapshot.fetch_reified_items @@ -107,7 +107,23 @@ def test_fetch_reified_items assert children_hash.is_a?(Hash) - assert children_hash.all?{|k,v| v.all?{|x| x.readonly?} } + assert children_hash.values.all?(&:readonly?) + end + + def test_fetch_reified_items_without_readonly + @snapshot = @snapshot_klass.first + + reified_items = @snapshot.fetch_reified_items(readonly: false) + + assert reified_items.is_a?(Array) + + assert_not reified_items.first.readonly? + + children_hash = reified_items.last + + assert children_hash.is_a?(Hash) + + assert children_hash.values.all?(&:readonly?) end def test_fetch_reified_items_with_sti_class