Skip to content

Commit

Permalink
Merge pull request #189 from brocktimus/master
Browse files Browse the repository at this point in the history
Only propagating updated_at if records have been changed with touch option
kbrock authored Jun 12, 2016
2 parents f8a7522 + 4a62f13 commit 68d6c05
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
@@ -74,9 +74,9 @@ def has_ancestry options = {}
}
end

after_save :touch_ancestors_callback
after_touch :touch_ancestors_callback
after_destroy :touch_ancestors_callback
after_save :touch_ancestors_callback, if: :changed?
end
end

@@ -86,4 +86,4 @@ class << ActiveRecord::Base
alias_method :acts_as_tree, :has_ancestry
end
end
end
end
25 changes: 23 additions & 2 deletions test/concerns/touching_test.rb
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ def test_touch_option_disabled
end
end

def test_touch_option_enabled
def test_touch_option_enabled_propagates_with_modification
AncestryTestDatabase.with_model(
:extra_columns => {:updated_at => :datetime},
:touch => true
@@ -44,4 +44,25 @@ def test_touch_option_enabled
assert_equal way_back, child_1_2.reload.updated_at, "unrelated record was touched"
end
end
end

def test_touch_option_enabled_doesnt_propagate_without_modification
AncestryTestDatabase.with_model(
:extra_columns => {:updated_at => :datetime},
:touch => true
) do |model|

way_back = Time.new(1984)
recently = Time.now - 1.minute

parent = model.create!
child = model.create!(:parent => parent)
grandchild = model.create!(:parent => child)
model.update_all(updated_at: way_back)
grandchild.save

assert_equal way_back, grandchild.reload.updated_at, "main record updated_at timestamp was touched"
assert_equal way_back, child.reload.updated_at, "parent record was touched"
assert_equal way_back, parent.reload.updated_at, "grandparent record was touched"
end
end
end

0 comments on commit 68d6c05

Please sign in to comment.