Skip to content

Instantly share code, notes, and snippets.

@remigijusj
Created April 3, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remigijusj/65ce558215292b85948b to your computer and use it in GitHub Desktop.
Save remigijusj/65ce558215292b85948b to your computer and use it in GitHub Desktop.
# 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