Skip to content

Commit

Permalink
Add readonly argument to Shapshot#fetch_reified_items (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrAnderson authored Aug 28, 2024
1 parent a825799 commit 6bf1738
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/active_snapshot/models/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ def restore!
return true
end

def fetch_reified_items
def fetch_reified_items(readonly: true)
reified_children_hash = {}.with_indifferent_access

reified_parent = nil

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

Expand Down
20 changes: 18 additions & 2 deletions test/models/snapshot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6bf1738

Please sign in to comment.