Skip to content

Commit

Permalink
Resolve error when has_snapshot_children has not been defined
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Feb 25, 2022
1 parent a92d03f commit 8894eeb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGELOG

- **UNRELEASED**
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.2.3...master)
* [PR #20](https://github.com/westonganger/active_snapshot/pull/20) - Resolve error when `has_snapshot_children` has not been defined as it should be optional
* [PR #18](https://github.com/westonganger/active_snapshot/pull/18) - Fix bug where sub-classes of a model would not be assigned correctly as parent when restoring

- **v0.2.3** - Jan 7, 2022
Expand Down
4 changes: 1 addition & 3 deletions lib/active_snapshot/models/concerns/snapshots_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def create_snapshot!(identifier, user: nil, metadata: nil)
class_methods do

def has_snapshot_children(&block)
if !block_given? && !defined?(@snapshot_children_proc)
raise ArgumentError.new("Invalid `has_snapshot_children` requires block to be defined")
elsif block_given?
if block_given?
@snapshot_children_proc = block
else
@snapshot_children_proc
Expand Down
5 changes: 5 additions & 0 deletions test/dummy_app/app/models/parent_without_children.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ParentWithoutChildren < ApplicationRecord
include ActiveSnapshot

self.table_name = "posts"
end
19 changes: 19 additions & 0 deletions test/models/snapshot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,23 @@ def test_fetch_reified_items_with_sti_class
assert reified_items.first.readonly?
assert_equal comment_content, reified_items.second[:comments].first.content
end

def test_single_model_snapshots_without_children
instance = ParentWithoutChildren.create!({a: 1, b: 2})

previous_attributes = instance.attributes

instance.create_snapshot!('v1')

instance.update!(a: 9, b: 9)

snapshot = instance.snapshots.first

reified_items = snapshot.fetch_reified_items

assert_equal [instance, {}], reified_items

assert_equal previous_attributes, reified_items.first.attributes
end

end
4 changes: 1 addition & 3 deletions test/models/snapshots_concern_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def test_create_snapshot!
def test_has_snapshot_children
klass = VolatilePost

assert_raise ArgumentError do
klass.has_snapshot_children
end
assert_nil klass.has_snapshot_children

klass.has_snapshot_children do
{}
Expand Down

0 comments on commit 8894eeb

Please sign in to comment.