Skip to content

Commit 1e099fd

Browse files
foobearwestonganger
authored andcommitted
Add full STI support (inherit snapshot children definition from base class, and allow overriding in STI child classes)
1 parent 946d22e commit 1e099fd

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

lib/active_snapshot/models/concerns/snapshots_concern.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module SnapshotsConcern
66
### We do NOT mark these as dependent: :destroy, the developer must manually destroy the snapshots or individual snapshot items
77
has_many :snapshots, as: :item, class_name: 'ActiveSnapshot::Snapshot'
88
has_many :snapshot_items, as: :item, class_name: 'ActiveSnapshot::SnapshotItem'
9+
10+
class_attribute :snapshot_children_proc
911
end
1012

1113
def create_snapshot!(identifier: nil, user: nil, metadata: nil)
@@ -25,9 +27,9 @@ def create_snapshot!(identifier: nil, user: nil, metadata: nil)
2527

2628
def has_snapshot_children(&block)
2729
if block_given?
28-
@snapshot_children_proc = block
30+
self.snapshot_children_proc = block
2931
else
30-
@snapshot_children_proc
32+
self.snapshot_children_proc
3133
end
3234
end
3335

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
class SubPost < Post
2-
has_snapshot_children do
3-
instance = self.class.includes(:comments, :notes).find(id)
4-
5-
{
6-
comments: instance.comments,
7-
notes: instance.notes,
8-
nil_assoc: nil,
9-
}
10-
end
2+
# Inherits snapshot children definition from base class
113
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SubPostWithOwnDefinition < Post
2+
# Ignores snapshot children definition from base class, and defines its own
3+
has_snapshot_children do
4+
{ comments: comments }
5+
end
6+
end

test/models/snapshot_test.rb

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,68 @@ def test_fetch_reified_items_without_readonly
181181
assert children_hash.values.all?(&:readonly?)
182182
end
183183

184+
def test_fetch_reified_items_with_base_class
185+
post = Post.create!(a: 1, b: 2)
186+
187+
comment_content = 'Example comment'
188+
post.comments.create!(content: comment_content)
189+
190+
note_body = 'Example note'
191+
post.notes.create!(body: note_body)
192+
193+
post.create_snapshot!(identifier: 'v1')
194+
snapshot = post.snapshots.first
195+
196+
reified_post, reified_children = snapshot.fetch_reified_items
197+
198+
assert_equal post, reified_post
199+
assert reified_post.readonly?
200+
assert_equal ['comments', 'notes'], reified_children.keys.sort
201+
assert_equal comment_content, reified_children['comments'].first.content
202+
assert_equal note_body, reified_children['notes'].first.body
203+
end
204+
184205
def test_fetch_reified_items_with_sti_class
206+
# Inherits snapshot children definition from base class
185207
post = SubPost.create!(a: 1, b: 2)
208+
186209
comment_content = 'Example comment'
187210
post.comments.create!(content: comment_content)
211+
212+
note_body = 'Example note'
213+
post.notes.create!(body: note_body)
214+
188215
post.create_snapshot!(identifier: 'v1')
189216
snapshot = post.snapshots.first
190-
reified_items = snapshot.fetch_reified_items
191217

192-
assert_equal post, reified_items.first
193-
assert reified_items.first.readonly?
194-
assert_equal comment_content, reified_items.second[:comments].first.content
218+
reified_post, reified_children = snapshot.fetch_reified_items
219+
220+
assert_equal post, reified_post
221+
assert reified_post.readonly?
222+
assert_equal ['comments', 'notes'], reified_children.keys.sort
223+
assert_equal comment_content, reified_children['comments'].first.content
224+
assert_equal note_body, reified_children['notes'].first.body
225+
end
226+
227+
def test_fetch_reified_items_with_sti_class_having_own_definition
228+
# Includes "comments" children, but no "notes"
229+
post = SubPostWithOwnDefinition.create!(a: 1, b: 2)
230+
231+
comment_content = 'Example comment'
232+
post.comments.create!(content: comment_content)
233+
234+
note_body = 'Example note'
235+
post.notes.create!(body: note_body)
236+
237+
post.create_snapshot!(identifier: 'v1')
238+
snapshot = post.snapshots.first
239+
240+
reified_post, reified_children = snapshot.fetch_reified_items
241+
242+
assert_equal post, reified_post
243+
assert reified_post.readonly?
244+
assert_equal ['comments'], reified_children.keys
245+
assert_equal comment_content, reified_children['comments'].first.content
195246
end
196247

197248
def test_fetch_reified_items_handles_dropped_columns!

test/models/snapshots_concern_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_has_snapshot_children
5858
{}
5959
end
6060

61-
assert klass.instance_variable_get(:@snapshot_children_proc).is_a?(Proc)
61+
assert klass.has_snapshot_children.is_a?(Proc)
6262

6363
klass.new.children_to_snapshot
6464

0 commit comments

Comments
 (0)