Skip to content

Commit

Permalink
Merge branch 'join_fixes' of https://github.com/raelik/ancestry into …
Browse files Browse the repository at this point in the history
…raelik-join_fixes
  • Loading branch information
Stefan Kroes committed Apr 22, 2011
2 parents 62f2679 + 5e0757a commit aee269e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/ancestry/has_ancestry.rb
Expand Up @@ -51,8 +51,8 @@ def has_ancestry options = {}
send scope_method, :descendants_of, lambda { |object| {:conditions => to_node(object).descendant_conditions} }
send scope_method, :subtree_of, lambda { |object| {:conditions => to_node(object).subtree_conditions} }
send scope_method, :siblings_of, lambda { |object| {:conditions => to_node(object).sibling_conditions} }
send scope_method, :ordered_by_ancestry, :order => "(case when #{ancestry_column} is null then 0 else 1 end), #{ancestry_column}"
send scope_method, :ordered_by_ancestry_and, lambda { |order| {:order => "(case when #{ancestry_column} is null then 0 else 1 end), #{ancestry_column}, #{order}"} }
send scope_method, :ordered_by_ancestry, :order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}"
send scope_method, :ordered_by_ancestry_and, lambda { |order| {:order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}, #{order}"} }

# Update descendants with new ancestry before save
before_save :update_descendants_with_new_ancestry
Expand Down Expand Up @@ -86,4 +86,4 @@ def has_ancestry options = {}
if !defined?(ActsAsTree)
alias_method :acts_as_tree, :has_ancestry
end
end
end
6 changes: 3 additions & 3 deletions lib/ancestry/instance_methods.rb
Expand Up @@ -171,7 +171,7 @@ def is_only_child?

# Descendants
def descendant_conditions
["#{self.base_class.ancestry_column} like ? or #{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
["#{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
end

def descendants depth_options = {}
Expand All @@ -184,7 +184,7 @@ def descendant_ids depth_options = {}

# Subtree
def subtree_conditions
["#{self.base_class.primary_key} = ? or #{self.base_class.ancestry_column} like ? or #{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
["#{self.base_class.table_name}.#{self.base_class.primary_key} = ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
end

def subtree depth_options = {}
Expand Down Expand Up @@ -229,4 +229,4 @@ def primary_key_type
@primary_key_type ||= column_for_attribute(self.class.primary_key).type
end
end
end
end

0 comments on commit aee269e

Please sign in to comment.