-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from kbrock/base_conditions
sql conditions to class level
- Loading branch information
Showing
5 changed files
with
62 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Ancestry | ||
module MaterializedPath | ||
def ancestor_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
t[primary_key].in(node.ancestor_ids) | ||
end | ||
|
||
def path_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
t[primary_key].in(node.path_ids) | ||
end | ||
|
||
def child_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
t[ancestry_column].eq(node.child_ancestry) | ||
end | ||
|
||
def descendant_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
# rails has case sensitive matching. | ||
if ActiveRecord::VERSION::MAJOR >= 5 | ||
t[ancestry_column].matches("#{node.child_ancestry}/%", nil, true).or(t[ancestry_column].eq(node.child_ancestry)) | ||
else | ||
t[ancestry_column].matches("#{node.child_ancestry}/%").or(t[ancestry_column].eq(node.child_ancestry)) | ||
end | ||
end | ||
|
||
def subtree_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
descendant_conditions(object).or(t[primary_key].eq(node.id)) | ||
end | ||
|
||
def sibling_conditions(object) | ||
t = arel_table | ||
node = to_node(object) | ||
t[ancestry_column].eq(node[ancestry_column]) | ||
end | ||
end | ||
end |