Class: JsonapiCompliable::Query
- Inherits:
-
Object
- Object
- JsonapiCompliable::Query
- Defined in:
- lib/jsonapi_compliable/query.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
TODO: This class could use some refactoring love!.
-
#resource ⇒ Object
readonly
TODO: This class could use some refactoring love!.
Class Method Summary collapse
-
.default_hash ⇒ Hash
private
This is the structure of Query#to_hash used elsewhere in the library.
Instance Method Summary collapse
-
#association_names ⇒ Array<Symbol>
All the keys of the #include_hash.
-
#include_directive ⇒ JSONAPI::IncludeDirective
The relevant include directive.
-
#include_hash ⇒ Hash
The include, directive, as a hash.
-
#initialize(resource, params) ⇒ Query
constructor
A new instance of Query.
-
#to_hash ⇒ Hash
A flat hash of sanitized query parameters.
-
#zero_results? ⇒ Boolean
Check if the user has requested 0 actual results They may have done this to get, say, the total count without the overhead of fetching actual records.
Constructor Details
#initialize(resource, params) ⇒ Query
Returns a new instance of Query
24 25 26 27 28 |
# File 'lib/jsonapi_compliable/query.rb', line 24 def initialize(resource, params) @resource = resource @params = params @params = @params.permit! if @params.respond_to?(:permit!) end |
Instance Attribute Details
#params ⇒ Object (readonly)
TODO: This class could use some refactoring love!
4 5 6 |
# File 'lib/jsonapi_compliable/query.rb', line 4 def params @params end |
#resource ⇒ Object (readonly)
TODO: This class could use some refactoring love!
4 5 6 |
# File 'lib/jsonapi_compliable/query.rb', line 4 def resource @resource end |
Class Method Details
.default_hash ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is the structure of Query#to_hash used elsewhere in the library
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jsonapi_compliable/query.rb', line 12 def self.default_hash { filter: {}, sort: [], page: {}, include: {}, stats: {}, fields: {}, extra_fields: {} } end |
Instance Method Details
#association_names ⇒ Array<Symbol>
All the keys of the #include_hash
For example, let's say we had
{ posts: { comments: {} }
#association_names
would return
[:posts, :comments]
75 76 77 |
# File 'lib/jsonapi_compliable/query.rb', line 75 def association_names @association_names ||= Util::Hash.keys(include_hash) end |
#include_directive ⇒ JSONAPI::IncludeDirective
The relevant include directive
33 34 35 |
# File 'lib/jsonapi_compliable/query.rb', line 33 def include_directive @include_directive ||= JSONAPI::IncludeDirective.new(params[:include]) end |
#include_hash ⇒ Hash
The include, directive, as a hash. For instance
{ posts: { comments: {} } }
This will only include relationships that are
-
Available on the Resource
-
Whitelisted (when specified)
So that users can't simply request your entire object graph.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jsonapi_compliable/query.rb', line 50 def include_hash @include_hash ||= begin requested = include_directive.to_hash whitelist = nil if resource.context whitelist = resource.context.sideload_whitelist whitelist = whitelist[resource.context_namespace] if whitelist end whitelist ? Util::IncludeParams.scrub(requested, whitelist) : requested end end |
#to_hash ⇒ Hash
A flat hash of sanitized query parameters. All relationship names are top-level:
{
posts: { filter, sort, ... }
comments: { filter, sort, ... }
}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/jsonapi_compliable/query.rb', line 124 def to_hash hash = { resource.type => self.class.default_hash } association_names.each do |name| hash[name] = self.class.default_hash end fields = parse_fields({}, :fields) extra_fields = parse_fields({}, :extra_fields) hash.each_pair do |type, query_hash| hash[type][:fields] = fields hash[type][:extra_fields] = extra_fields end parse_filter(hash) parse_sort(hash) parse_pagination(hash) parse_include(hash, include_hash, resource.type) parse_stats(hash) hash end |
#zero_results? ⇒ Boolean
Check if the user has requested 0 actual results They may have done this to get, say, the total count without the overhead of fetching actual records.
163 164 165 166 167 |
# File 'lib/jsonapi_compliable/query.rb', line 163 def zero_results? !@params[:page].nil? && !@params[:page][:size].nil? && @params[:page][:size].to_i == 0 end |