Created
April 3, 2015 08:49
-
-
Save remigijusj/e3c540dcdaf114985c65 to your computer and use it in GitHub Desktop.
Databound patch
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
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