Module: ContentfulModel::ChainableQueries::ClassMethods
- Defined in:
- lib/contentful_model/chainable_queries.rb
Instance Method Summary collapse
- #all ⇒ Object
- #find_by(*args) ⇒ Object
- #first ⇒ Object
- #offset(n) ⇒ Object (also: #skip)
- #order(args) ⇒ Object
- #search(parameters) ⇒ Object
Instance Method Details
#all ⇒ Object
10 11 12 13 |
# File 'lib/contentful_model/chainable_queries.rb', line 10 def all raise ArgumentError, "You need to set self.content_type in your model class" if @content_type_id.nil? self end |
#find_by(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/contentful_model/chainable_queries.rb', line 41 def find_by(*args) args.each do |query| #query is a hash if query.values.first.is_a?(Array) #we need to do an 'in' query @query << {"fields.#{query.keys.first}[in]" => query.values.first.join(",")} elsif query.values.first.is_a?(String) @query << {"fields.#{query.keys.first}" => query.values.first} end end self end |
#first ⇒ Object
15 16 17 18 |
# File 'lib/contentful_model/chainable_queries.rb', line 15 def first @query << {'limit' => 1} load.first end |
#offset(n) ⇒ Object Also known as: skip
20 21 22 23 |
# File 'lib/contentful_model/chainable_queries.rb', line 20 def offset(n) @query << {'skip' => n} self end |
#order(args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/contentful_model/chainable_queries.rb', line 25 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 = '' end @query << {'order' => "#{prefix}fields.#{column.camelize(:lower)}"} puts @query.inspect self end |
#search(parameters) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/contentful_model/chainable_queries.rb', line 53 def search(parameters) if parameters.is_a?(Hash) parameters.each do |field, search| @query << {"fields.#{field}[match]" => search} end elsif parameters.is_a?(String) @query << {"query" => parameters} end self end |