Created
August 18, 2011 10:20
-
-
Save philsmy/1153792 to your computer and use it in GitHub Desktop.
or scopes - add this to your model to be able to or scopes together (eg: Model.or(Model.scope1, Model.scope2). Stolen and adapted from fake_arel
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
__or_fn = lambda do |*scopes| | |
where = [] | |
joins = [] | |
includes = [] | |
# for some reason, flatten is actually executing the scope | |
scopes = scopes[0] if scopes.size == 1 | |
scopes.each do |s| | |
w = [] | |
s.where_clauses.each do |where_clause| | |
w << where_clause | |
end | |
where << w.join(" AND ") | |
joins << s.joins_values if s.joins_values.any? | |
includes << s.includes_values if s.includes_values.any? | |
end | |
scoped = self | |
p self | |
scoped = scoped.includes(includes.uniq.flatten) if includes.any? | |
scoped = scoped.joins(joins.uniq.flatten) if joins.any? | |
p where.join(" OR ") | |
scoped.where(where.join(" OR ")) | |
end | |
scope :or, __or_fn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment