Created
April 3, 2015 06:09
-
-
Save remigijusj/65ce558215292b85948b to your computer and use it in GitHub Desktop.
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
# a monkey-patch fix for OR-query building, needed for Rails 4.2 | |
module Databound | |
class Manager | |
def or_query(root, *scopes) | |
relations = scopes.map do |scope| | |
root.where(scope.to_h) | |
end | |
nodes = relations.map do |rel| | |
rel.where_values.reduce(:and) | |
end | |
bind_values = relations.flat_map(&:bind_values) | |
res = root.where(nodes.reduce(:or)).tap { |rel| rel.bind_values = bind_values } | |
res | |
end | |
def find_scoped_records(only_extra_scopes: false) | |
records = or_query(model, @scope, *@extra_where_scopes) | |
unless only_extra_scopes | |
records = filter_by_params!(records) | |
check_permit!(:read, records) | |
end | |
records | |
end | |
# THIS WAS: or_query(records, params, *@extra_where_scopes) | |
# simplified the condition, because repeated or-query joining by and doesn't quite work | |
def filter_by_params!(records) | |
records.where(params.to_h) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment