Skip to content

Instantly share code, notes, and snippets.

@remigijusj
Created April 3, 2015 08:49
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/e3c540dcdaf114985c65 to your computer and use it in GitHub Desktop.
Save remigijusj/e3c540dcdaf114985c65 to your computer and use it in GitHub Desktop.
Databound patch
module Databound
class Manager
def find_scoped_records(only_extra_scopes: false)
if only_extra_scopes
rel = model.where(@scope.to_h)
else
rel = model.where(params.to_h)
check_permit!(:read, rel) # <<< this is out of place, because SRP!
end
extra_rels = @extra_where_scopes.map do |scope|
model.where(scope.to_h)
end
or_query(rel, *extra_rels)
end
def or_query(*relations)
nodes = relations.map do |rel|
rel.where_values.reduce(:and)
end
bind_values = relations.flat_map(&:bind_values)
model.where(nodes.reduce(:or)).tap do |rel|
rel.bind_values = bind_values
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment