Skip to content

Commit

Permalink
Merge pull request #159 from mlitwiniuk/master
Browse files Browse the repository at this point in the history
change conditions into arel expressions
  • Loading branch information
stefankroes committed Dec 5, 2013
2 parents b3da26c + 710dd30 commit a5fdee9
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/ancestry/instance_methods.rb
Expand Up @@ -105,7 +105,8 @@ def ancestor_ids
end

def ancestor_conditions
{primary_key_with_table => ancestor_ids}
t = get_arel_table
t[get_primary_key_column].in(ancestor_ids)
end

def ancestors depth_options = {}
Expand All @@ -125,7 +126,8 @@ def path_ids
end

def path_conditions
{primary_key_with_table => path_ids}
t = get_arel_table
t[get_primary_key_column].in(path_ids)
end

def path depth_options = {}
Expand Down Expand Up @@ -177,7 +179,8 @@ def is_root?

# Children
def child_conditions
{ancestry_column_with_table => child_ancestry}
t = get_arel_table
t[get_ancestry_column].eq(child_ancestry)
end

def children
Expand All @@ -198,7 +201,8 @@ def is_childless?

# Siblings
def sibling_conditions
{ancestry_column_with_table => read_attribute(self.ancestry_base_class.ancestry_column)}
t = get_arel_table
t[get_ancestry_column].eq(read_attribute(self.ancestry_base_class.ancestry_column))
end

def siblings
Expand All @@ -219,7 +223,8 @@ def is_only_child?

# Descendants
def descendant_conditions
["#{ancestry_column_with_table} like ? or #{ancestry_column_with_table} = ?", "#{child_ancestry}/%", child_ancestry]
t = get_arel_table
t[get_ancestry_column].matches("#{child_ancestry}/%").or(t[get_ancestry_column].eq(child_ancestry))
end

def descendants depth_options = {}
Expand All @@ -232,7 +237,8 @@ def descendant_ids depth_options = {}

# Subtree
def subtree_conditions
["#{primary_key_with_table} = ? or #{ancestry_column_with_table} like ? or #{ancestry_column_with_table} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
t = get_arel_table
t[get_primary_key_column].eq(self.id).or(t[get_ancestry_column].matches("#{child_ancestry}/%")).or(t[get_ancestry_column].eq(child_ancestry))
end

def subtree depth_options = {}
Expand Down Expand Up @@ -283,12 +289,16 @@ def unscoped_find id
self.ancestry_base_class.unscoped { self.ancestry_base_class.find(id) }
end

def primary_key_with_table
"#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.primary_key}"
def get_arel_table
self.ancestry_base_class.arel_table
end

def ancestry_column_with_table
"#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column}"
def get_primary_key_column
self.ancestry_base_class.primary_key.to_sym
end

def get_ancestry_column
self.ancestry_base_class.ancestry_column.to_sym
end
end
end

0 comments on commit a5fdee9

Please sign in to comment.