Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added in the association callbacks to the children :has_many association

  • Loading branch information...
commit f957fa687d037f4e9badd4afdd1c9e2f3da4d6c0 1 parent 627e0fc
@mdeering mdeering authored
View
6 lib/awesome_nested_set/awesome_nested_set.rb
@@ -64,7 +64,11 @@ def acts_as_nested_set(options = {})
:inverse_of => :children
has_many :children, :class_name => self.base_class.to_s,
:foreign_key => parent_column_name, :order => quoted_left_column_name,
- :inverse_of => :parent
+ :inverse_of => :parent,
+ :before_add => options[:before_add] || nil,
+ :after_add => options[:after_add] || nil,
+ :before_remove => options[:before_remove] || nil,
+ :after_remove => options[:after_remove] || nil
attr_accessor :skip_before_destroy
View
22 spec/awesome_nested_set_spec.rb
@@ -798,7 +798,6 @@ def check_structure(entries, structure)
end
it "should increment the counter cache on create" do
- ActiveRecord::Base.logger.info 'FOO'
note1 = things(:parent1)
note1.children.count.should == 2
note1[:children_count].should == 2
@@ -818,4 +817,25 @@ def check_structure(entries, structure)
note1[:children_count].should == 1
end
end
+
+ describe "association callbacks on children" do
+ it "should call the appropriate callbacks on the children :has_many association " do
+ root = DefaultWithCallbacks.create
+ root.should_not be_new_record
+
+ child = root.children.build
+
+ root.before_add.should == child
+ root.after_add.should == child
+
+ root.before_remove.should_not == child
+ root.after_remove.should_not == child
+
+ child.save.should be_true
+ root.children.delete(child).should be_true
+
+ root.before_remove.should == child
+ root.after_remove.should == child
+ end
+ end
end
View
21 spec/support/models.rb
@@ -44,4 +44,25 @@ def recurse &block
class Thing < ActiveRecord::Base
acts_as_nested_set :counter_cache => 'children_count'
+end
+
+class DefaultWithCallbacks < ActiveRecord::Base
+
+ set_table_name 'categories'
+
+ attr_accessor :before_add, :after_add, :before_remove, :after_remove
+
+ acts_as_nested_set :before_add => :do_before_add_stuff,
+ :after_add => :do_after_add_stuff,
+ :before_remove => :do_before_remove_stuff,
+ :after_remove => :do_after_remove_stuff
+
+ private
+
+ [ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name|
+ define_method "do_#{hook_name}_stuff" do |child_node|
+ self.send("#{hook_name}=", child_node)
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.