Skip to content

Commit

Permalink
support for reify of only belongs_to relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
theRealNG committed Mar 14, 2016
1 parent 2bf0520 commit 39df4e3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
52 changes: 49 additions & 3 deletions lib/paper_trail/reifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def reify(version, options)
mark_for_destruction: false,
has_one: false,
has_many: false,
belongs_to: false,
unversioned_attributes: :nil
)

Expand Down Expand Up @@ -91,6 +92,25 @@ def reify_attributes(model, version, attrs)
)
end
end
<<<<<<< HEAD
=======

model.send "#{model.class.version_association_name}=", version

unless options[:has_one] == false
reify_has_ones version.transaction_id, model, options
end

unless options[:belongs_to] == false
reify_belongs_tos version.transaction_id, model, options
end

unless options[:has_many] == false
reify_has_manys version.transaction_id, model, options
end

model
>>>>>>> support for reify of only belongs_to relationships
end

# Replaces each record in `array` with its reified version, if present
Expand All @@ -114,7 +134,7 @@ def prepare_array_for_has_many(array, options, versions)
elsif version.event == "create"
options[:mark_for_destruction] ? record.tap(&:mark_for_destruction) : nil
else
version.reify(options.merge(has_many: false, has_one: false))
version.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
end
end

Expand All @@ -123,7 +143,7 @@ def prepare_array_for_has_many(array, options, versions)
# associations.
array.concat(
versions.values.map { |v|
v.reify(options.merge(has_many: false, has_one: false))
v.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
}
)

Expand Down Expand Up @@ -156,7 +176,7 @@ def reify_has_ones(transaction_id, model, options = {})
end
end
else
child = version.reify(options.merge(has_many: false, has_one: false))
child = version.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
model.appear_as_new_record do
without_persisting(child) do
model.send "#{assoc.name}=", child
Expand All @@ -166,6 +186,32 @@ def reify_has_ones(transaction_id, model, options = {})
end
end

def reify_belongs_tos(transaction_id, model, options = {})
associations = model.class.reflect_on_all_associations(:belongs_to)

associations.each do |assoc|
next unless assoc.klass.paper_trail_enabled_for_model?
collection_key = model.send(assoc.association_foreign_key)

version = assoc.klass.paper_trail_version_class.
where("item_type = ?", assoc.class_name).
where("item_id = ?", collection_key).
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
order("id").limit(1).first

collection = if version.nil?
assoc.klass.where(assoc.klass.primary_key => collection_key).first
elsif version.event == "create"
options[:mark_for_destruction] ? collection.mark_for_destruction : nil
else
version.reify(options.merge(has_many: false, has_one: false,
belongs_to: false))
end

model.send("#{assoc.name}=".to_sym, collection)
end
end

# Restore the `model`'s has_many associations as they were at version_at
# timestamp We lookup the first child versions after version_at timestamp or
# in same transaction.
Expand Down
87 changes: 87 additions & 0 deletions test/unit/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,91 @@ class AssociationsTest < ActiveSupport::TestCase
end
end
end

context "belongs_to associations" do
context "Wotsit and Widget" do
setup { @widget = Widget.create(name: "widget_0") }

context "where the association is created between model versions" do
setup do
@wotsit = @widget.create_wotsit(name: "wotsit_0")
Timecop.travel 1.second.since
@wotsit.update_attributes name: "wotsit_1"
end

context "when reified" do
setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was at the time" do
assert_equal "widget_0", @wotsit_0.widget.name
end

should "not persist changes to the live association" do
assert_equal @widget, @wotsit.reload.widget
end
end

context "and then the associated is updated between model versions" do
setup do
@widget.update_attributes name: "widget_1"
@widget.update_attributes name: "widget_2"
Timecop.travel 1.second.since
@wotsit.update_attributes name: "wotsit_2"
@widget.update_attributes name: "widget_3"
end

context "when reified" do
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was at the time" do
assert_equal "widget_2", @wotsit_1.widget.name
end

should "not persist changes to the live association" do
assert_equal "widget_3", @wotsit.reload.widget.name
end
end

context "when reified opting out of belongs_to reification" do
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: false) }

should "see the associated as it is live" do
assert_equal "widget_3", @wotsit_1.widget.name
end
end
end

context "and then the associated is destroyed" do
setup { @widget.destroy }

context "when reify" do
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was at the time" do
assert_equal @widget, @wotsit_1.widget
end

should "not persist changes to the live association" do
assert_nil @wotsit.reload.widget
end
end

context "and then the model is updated" do
setup do
Timecop.travel 1.second.since
@wotsit.update_attributes name: "wotsit_3"
end

context "when reified" do
setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) }

should "see the associated as it was the time" do
assert_nil @wotsit_2.widget
end
end
end
end
end
end
end
end

0 comments on commit 39df4e3

Please sign in to comment.