diff --git a/lib/closure_tree/acts_as_tree.rb b/lib/closure_tree/acts_as_tree.rb index 7fc0829c..eeb50d4f 100644 --- a/lib/closure_tree/acts_as_tree.rb +++ b/lib/closure_tree/acts_as_tree.rb @@ -40,6 +40,9 @@ def ==(comparison_object) self.attributes == comparison_object.attributes end alias :eql? :== + def hash + attributes.hash + end RUBY self.hierarchy_class.table_name = hierarchy_table_name diff --git a/spec/tag_spec.rb b/spec/tag_spec.rb index ec5f8679..dfc54582 100644 --- a/spec/tag_spec.rb +++ b/spec/tag_spec.rb @@ -130,6 +130,15 @@ def nuke_db TagHierarchy.find_all_by_ancestor_id(@root.id).should == root_hiers TagHierarchy.find_all_by_descendant_id(@root.id).should == root_hiers end + + it "should have different hash codes for each hierarchy model" do + hashes = TagHierarchy.all.map(&:hash) + hashes.should =~ hashes.uniq + end + + it "should return the same hash code for equal hierarchy models" do + TagHierarchy.first.hash.should == TagHierarchy.first.hash + end end it "performs as the readme says it does" do