Sha256: d83318e092bb134f39c6f7e755839bfad0ba20dd0c5d3065c3df504e7b395946
Contents?: true
Size: 1.76 KB
Versions: 32
Compression:
Stored size: 1.76 KB
Contents
module JsonapiCompliable # Apply filtering logic to the scope # # If the user requests to filter a field that has not been whitelisted, # a +JsonapiCompliable::Errors::BadFilter+ error will be raised. # # allow_filter :title # :title now whitelisted # # If the user requests a filter field that has been whitelisted, but # does not pass the associated `+:if+ clause, +BadFilter+ will be raised. # # allow_filter :title, if: :admin? # # This will also honor filter aliases. # # # GET /posts?filter[headline]=foo will filter on title # allow_filter :title, aliases: [:headline] # # @see Adapters::Abstract#filter # @see Adapters::ActiveRecord#filter # @see Resource.allow_filter class Scoping::Filter < Scoping::Base include Scoping::Filterable # Apply the filtering logic. # # Loop and parse all requested filters, taking into account guards and # aliases. If valid, call either the default or custom filtering logic. # @return the scope we are chaining/modifying def apply each_filter do |filter, value| @scope = filter_scope(filter, value) end @scope end private # If there's custom logic, run it, otherwise run the default logic # specified in the adapter. def filter_scope(filter, value) if custom_scope = filter.values.first[:filter] custom_scope.call(@scope, value) else resource.adapter.filter(@scope, filter.keys.first, value) end end def each_filter filter_param.each_pair do |param_name, param_value| filter = find_filter!(param_name.to_sym) value = param_value value = value.split(',') if value.is_a?(String) && value.include?(',') yield filter, value end end end end
Version data entries
32 entries across 32 versions & 1 rubygems