module GraphQR::ApplyScopes
The ApplyScopes
module defines a way of applying model scopes in the GraphQL universe it is based on the [has_scope](github.com/plataformatec/has_scope/) gem, but simplified for more basic usage
Public Instance Methods
This method is a parallel to the one offerend by the `has_scope` gem. A big difference in this case is the necessity of the second parameter (normally it only takes one).
### Params:
target
: the ActiveRecordRelation that will be filtered using scopes
scopes
: a hash of scopes and their values, those only accept Array, String, Integer or Boolean types. **Hash scopes are not yet supported**
### Example:
“` apply_scopes
(User, { with_id: [1,2,3], order_by_name: true} ) “`
# File lib/graphqr/apply_scopes.rb, line 24 def apply_scopes(target, scopes) parsed_scopes = parse_scopes(scopes.to_h) parsed_scopes.each do |scope, value| target = call_scope(target, scope, value) end target end
Private Instance Methods
Checks whether the value is a Boolean checking its class
# File lib/graphqr/apply_scopes.rb, line 56 def boolean?(value) value.class == TrueClass || value.class == FalseClass end
Calls the scope with the correct parametes (none if its a boolean type)
# File lib/graphqr/apply_scopes.rb, line 46 def call_scope(target, scope, value) if boolean?(value) target.send(scope) else target.send(scope, value) end end
Parses the scope hash, removing scopes with a `nil` or `false` value
# File lib/graphqr/apply_scopes.rb, line 38 def parse_scopes(scopes) scopes.inject({}) do |acc, option| option.last.present? ? acc.merge(option.first => option.last) : acc end end