Ticket #36: fix_root_method.diff

File fix_root_method.diff, 2.1 kB (added by rails, 1 year ago)
  • lib/better_nested_set.rb

    old new  
    208208          end                                    
    209209        end 
    210210         
    211         # Returns roo
     211        # Returns the root for the current se
    212212        def root 
    213             self.class.find(:first, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)"
     213            ancestors(:first
    214214        end 
    215215                 
    216         # Returns roots when multiple roots (or virtual root, which is the same) 
    217         def roots 
    218             self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)", :order => "#{acts_as_nested_set_options[:left_column]}") 
    219         end 
    220                  
    221216        # Returns the parent 
    222217        def parent 
    223218            self.class.find(self[acts_as_nested_set_options[:parent_column]]) if self[acts_as_nested_set_options[:parent_column]] 
     
    225220         
    226221        # Returns an array of all parents  
    227222        # Maybe 'full_outline' would be a better name, but we prefer to mimic the Tree class 
    228         def ancestors 
    229             self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column] ) 
     223        def ancestors(multiplicity = :all) 
     224            self.class.find(multiplicity, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column] ) 
    230225        end 
    231226         
    232227        # Returns the array of all parents and self