Skip to content

Commit f6baa82

Browse files
committed
writing specs
1 parent 6abe5ea commit f6baa82

2 files changed

Lines changed: 94 additions & 8 deletions

File tree

lib/paper_trail/reifier.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def prepare_array_for_has_many(array, options, versions)
113113
elsif version.event == "create"
114114
options[:mark_for_destruction] ? record.tap(&:mark_for_destruction) : nil
115115
else
116-
version.reify(options.merge(has_many: false, has_one: false))
116+
version.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
117117
end
118118
end
119119

@@ -122,7 +122,7 @@ def prepare_array_for_has_many(array, options, versions)
122122
# associations.
123123
array.concat(
124124
versions.values.map { |v|
125-
v.reify(options.merge(has_many: false, has_one: false))
125+
v.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
126126
}
127127
)
128128

@@ -155,7 +155,7 @@ def reify_has_ones(transaction_id, model, options = {})
155155
end
156156
end
157157
else
158-
child = version.reify(options.merge(has_many: false, has_one: false))
158+
child = version.reify(options.merge(has_many: false, has_one: false, belongs_to: false))
159159
model.appear_as_new_record do
160160
without_persisting(child) do
161161
model.send "#{assoc.name}=", child
@@ -166,8 +166,6 @@ def reify_has_ones(transaction_id, model, options = {})
166166
end
167167

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

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

183181
collection = if version.nil?
184182
assoc.klass.where(assoc.klass.primary_key => collection_key).first
185-
elsif version.event == 'create'
183+
elsif version.event == "create"
186184
options[:mark_for_destruction] ? collection.mark_for_destruction : nil
187185
else
188-
version.reify(options.merge(:has_many => false, :has_one => false))
186+
version.reify(options.merge(has_many: false, has_one: false,
187+
belongs_to: false))
189188
end
190189

191-
model.send("#{assoc.name}=".to_sym,collection)
190+
model.send("#{assoc.name}=".to_sym, collection)
192191
end
193192
end
194193

test/unit/associations_test.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,91 @@ class AssociationsTest < ActiveSupport::TestCase
769769
end
770770
end
771771
end
772+
773+
context "belongs_to associations" do
774+
context "Wotsit and Widget" do
775+
setup { @widget = Widget.create(name: "widget_0") }
776+
777+
context "where the association is created between model versions" do
778+
setup do
779+
@wotsit = @widget.create_wotsit(name: "wotsit_0")
780+
Timecop.travel 1.second.since
781+
@wotsit.update_attributes name: "wotsit_1"
782+
end
783+
784+
context "when reified" do
785+
setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) }
786+
787+
should "see the associated as it was at the time" do
788+
assert_equal "widget_0", @wotsit_0.widget.name
789+
end
790+
791+
should "not persist changes to the live association" do
792+
assert_equal @widget, @wotsit.reload.widget
793+
end
794+
end
795+
796+
context "and then the associated is updated between model versions" do
797+
setup do
798+
@widget.update_attributes name: "widget_1"
799+
@widget.update_attributes name: "widget_2"
800+
Timecop.travel 1.second.since
801+
@wotsit.update_attributes name: "wotsit_2"
802+
@widget.update_attributes name: "widget_3"
803+
end
804+
805+
context "when reified" do
806+
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) }
807+
808+
should "see the associated as it was at the time" do
809+
assert_equal "widget_2", @wotsit_1.widget.name
810+
end
811+
812+
should "not persist changes to the live association" do
813+
assert_equal "widget_3", @wotsit.reload.widget.name
814+
end
815+
end
816+
817+
context "when reified opting out of belongs_to reification" do
818+
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: false) }
819+
820+
should "see the associated as it is live" do
821+
assert_equal "widget_3", @wotsit_1.widget.name
822+
end
823+
end
824+
end
825+
826+
context "and then the associated is destroyed" do
827+
setup { @widget.destroy }
828+
829+
context "when reify" do
830+
setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) }
831+
832+
should "see the associated as it was at the time" do
833+
assert_equal @widget, @wotsit_1.widget
834+
end
835+
836+
should "not persist changes to the live association" do
837+
assert_nil @wotsit.reload.widget
838+
end
839+
end
840+
841+
context "and then the model is updated" do
842+
setup do
843+
Timecop.travel 1.second.since
844+
@wotsit.update_attributes name: "wotsit_3"
845+
end
846+
847+
context "when reified" do
848+
setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) }
849+
850+
should "see the associated as it was the time" do
851+
assert_nil @wotsit_2.widget
852+
end
853+
end
854+
end
855+
end
856+
end
857+
end
858+
end
772859
end

0 commit comments

Comments
 (0)