class ContentfulModel::Query
Class to wrap query parameters
Constants
- SYS_PROPERTIES
Attributes
parameters[RW]
Public Class Methods
new(referenced_class, parameters = nil)
click to toggle source
# File lib/contentful_model/query.rb, line 7 def initialize(referenced_class, parameters = nil) @parameters = parameters || {} @referenced_class = referenced_class end
Public Instance Methods
<<(parameters)
click to toggle source
# File lib/contentful_model/query.rb, line 12 def <<(parameters) @parameters.merge!(parameters) end
client()
click to toggle source
# File lib/contentful_model/query.rb, line 166 def client @client ||= @referenced_class.client end
default_parameters()
click to toggle source
# File lib/contentful_model/query.rb, line 146 def default_parameters { 'content_type' => @referenced_class.content_type_id } end
discover_includes()
click to toggle source
# File lib/contentful_model/query.rb, line 174 def discover_includes @referenced_class.discovered_include_level end
each_entry(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block)
click to toggle source
# File lib/contentful_model/query.rb, line 61 def each_entry(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block) each_page(per_page, order_field, additional_options) do |page| page.each do |entry| block[entry] end end end
each_page(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block)
click to toggle source
# File lib/contentful_model/query.rb, line 52 def each_page(per_page = 100, order_field = 'sys.updatedAt', additional_options = {}, &block) total = self.class.new(@referenced_class).limit(1).load_children(0).params(additional_options).execute.total ((total / per_page) + 1).times do |i| page = self.class.new(@referenced_class).paginate(i, per_page, order_field, additional_options).execute block[page] end end
execute()
click to toggle source
# File lib/contentful_model/query.rb, line 150 def execute query = @parameters.merge(default_parameters) discovered_includes = discover_includes query['include'] = discovered_includes unless query.key?('include') || discovered_includes == 1 result = client.entries(query) result.items.reject!(&:invalid?) result end
Also aliased as: load
find(id)
click to toggle source
# File lib/contentful_model/query.rb, line 92 def find(id) self << { 'sys.id' => id } load.first end
find_by(find_query = {})
click to toggle source
# File lib/contentful_model/query.rb, line 97 def find_by(find_query = {}) find_query.each do |field, value| key = if field.to_s.include?('sys.') || field.to_s.include?('fields.') field elsif SYS_PROPERTIES.include?(field.to_s) "sys.#{field}" else "fields.#{field}" end case value when Array # we need to do an 'in' query self << { "#{key}[in]" => value.join(',') } when String, Numeric, true, false self << { key.to_s => value } when Hash # if the search is a hash, use the key to specify the search field operator # For example # Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now value.each do |search_predicate, search_value| self << { "#{key}[#{search_predicate}]" => search_value } end end end self end
Also aliased as: where
first()
click to toggle source
# File lib/contentful_model/query.rb, line 21 def first self << { 'limit' => 1 } load.first end
limit(n)
click to toggle source
# File lib/contentful_model/query.rb, line 32 def limit(n) self << { 'limit' => n } self end
load!()
click to toggle source
# File lib/contentful_model/query.rb, line 162 def load! load.presence || raise(NotFoundError) end
load_children(n)
click to toggle source
# File lib/contentful_model/query.rb, line 69 def load_children(n) self << { 'include' => n } self end
locale(locale_code)
click to toggle source
# File lib/contentful_model/query.rb, line 37 def locale(locale_code) self << { 'locale' => locale_code } self end
offset(n)
click to toggle source
# File lib/contentful_model/query.rb, line 26 def offset(n) self << { 'skip' => n } self end
Also aliased as: skip
order(args)
click to toggle source
# File lib/contentful_model/query.rb, line 74 def order(args) prefix = '' if args.is_a?(Hash) column = args.first.first.to_s prefix = '-' if args.first.last == :desc elsif args.is_a?(Symbol) column = args.to_s prefix = '' else column = args.to_s end property_name = column.camelize(:lower).to_sym property_type = SYS_PROPERTIES.include?(property_name.to_s) ? 'sys' : 'fields' self << { 'order' => "#{prefix}#{property_type}.#{property_name}" } self end
paginate(page = 1, per_page = 100, order_field = 'sys.updatedAt', additional_options = {})
click to toggle source
# File lib/contentful_model/query.rb, line 42 def paginate(page = 1, per_page = 100, order_field = 'sys.updatedAt', additional_options = {}) page = 1 if page.nil? || !page.is_a?(Numeric) || page <= 0 per_page = 100 if per_page.nil? || !per_page.is_a?(Numeric) || per_page <= 0 skip_records_count = (page - 1) * per_page self << { 'limit' => per_page, 'skip' => skip_records_count, 'order' => order_field } self << additional_options self end
params(options)
click to toggle source
# File lib/contentful_model/query.rb, line 16 def params(options) self << options self end
reset()
click to toggle source
# File lib/contentful_model/query.rb, line 170 def reset @parameters = default_parameters end
search(parameters)
click to toggle source
# File lib/contentful_model/query.rb, line 126 def search(parameters) if parameters.is_a?(Hash) parameters.each do |field, search| # if the search is a hash, use the key to specify the search field operator # For example # Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now if search.is_a?(Hash) search_key, search_value = *search.flatten self << { "fields.#{field.to_s.camelize(:lower)}[#{search_key}]" => search_value } else self << { "fields.#{field.to_s.camelize(:lower)}[match]" => search } end end elsif parameters.is_a?(String) self << { 'query' => parameters } end self end