Skip to content

Commit

Permalink
writing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
theRealNG committed Mar 11, 2016
1 parent 6abe5ea commit f6baa82
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/paper_trail/reifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 @@ -122,7 +122,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 @@ -155,7 +155,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,8 +166,6 @@ def reify_has_ones(transaction_id, model, options = {})
end

def reify_belongs_tos(transaction_id, model, options = {})
#TODO write comments and spec
version_table_name = model.class.paper_trail_version_class.table_name
associations = model.class.reflect_on_all_associations(:belongs_to)

associations.each do |assoc|
Expand All @@ -182,13 +180,14 @@ def reify_belongs_tos(transaction_id, model, options = {})

collection = if version.nil?
assoc.klass.where(assoc.klass.primary_key => collection_key).first
elsif version.event == 'create'
elsif version.event == "create"
options[:mark_for_destruction] ? collection.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

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

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 f6baa82

Please sign in to comment.