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 |
# File 'lib/jsonapi_compliable/query.rb', line 24 def initialize(resource, params) @resource = resource @params = params 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]
71 72 73 |
# File 'lib/jsonapi_compliable/query.rb', line 71 def association_names @association_names ||= Util::Hash.keys(include_hash) end |
#include_directive ⇒ JSONAPI::IncludeDirective
The relevant include directive
32 33 34 |
# File 'lib/jsonapi_compliable/query.rb', line 32 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.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jsonapi_compliable/query.rb', line 49 def include_hash @include_hash ||= begin requested = include_directive.to_hash all_allowed = resource.sideloading.to_hash[:base] whitelist = resource.sideload_whitelist.values.reduce(:merge) allowed = whitelist ? Util::IncludeParams.scrub(all_allowed, whitelist) : all_allowed Util::IncludeParams.scrub(requested, allowed) end end |
#to_hash ⇒ Hash
A flat hash of sanitized query parameters. All relationship names are top-level:
{
posts: { filter, sort, ... }
comments: { filter, sort, ... }
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jsonapi_compliable/query.rb', line 120 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.
159 160 161 162 163 |
# File 'lib/jsonapi_compliable/query.rb', line 159 def zero_results? !@params[:page].nil? && !@params[:page][:size].nil? && @params[:page][:size].to_i == 0 end |