class Airmodel::Query
Public Class Methods
new(querying_class)
click to toggle source
# File lib/airmodel/query.rb, line 5 def initialize(querying_class) @querying_class = querying_class end
Public Instance Methods
all()
click to toggle source
# File lib/airmodel/query.rb, line 90 def all to_a end
by_formula(formula)
click to toggle source
# File lib/airmodel/query.rb, line 23 def by_formula(formula) params[:formulas].push formula self end
each(&block)
click to toggle source
# File lib/airmodel/query.rb, line 94 def each(&block) to_a.each(&block) end
find_by(filters)
click to toggle source
# File lib/airmodel/query.rb, line 110 def find_by(filters) params[:limit] = 1 params[:where_clauses] = filters first end
get_offset()
click to toggle source
return saved airtable offset for this query
# File lib/airmodel/query.rb, line 71 def get_offset @offset end
inspect()
click to toggle source
# File lib/airmodel/query.rb, line 102 def inspect to_a.inspect end
last()
click to toggle source
# File lib/airmodel/query.rb, line 106 def last to_a.last end
limit(lim)
click to toggle source
# File lib/airmodel/query.rb, line 49 def limit(lim) params[:limit] = lim ? lim.to_i : nil self end
map(&block)
click to toggle source
# File lib/airmodel/query.rb, line 98 def map(&block) to_a.map(&block) end
offset(airtable_offset_key)
click to toggle source
# File lib/airmodel/query.rb, line 65 def offset(airtable_offset_key) params[:offset] = airtable_offset_key self end
order(order_string)
click to toggle source
# File lib/airmodel/query.rb, line 54 def order(order_string) if order_string ordr = order_string.split(" ") column = ordr.first direction = ordr.length > 1 ? ordr.last.downcase : "asc" params[:order] = [column, direction] end self end
params()
click to toggle source
# File lib/airmodel/query.rb, line 9 def params @params ||= { where_clauses: {}, formulas: [], order: @querying_class.default_sort, offset: nil } end
search(args={})
click to toggle source
# File lib/airmodel/query.rb, line 28 def search(args={}) if args && args[:q] && args[:fields] searchfields = if args[:fields].is_a?(String) args[:fields].split(",").map{|f| f.strip } else args[:fields] end query = if args[:q].respond_to?(:downcase) args[:q].downcase else args[:q] end f = "OR(" + searchfields.map{|field| # convert strings to case-insensitive searches "FIND('#{query}', LOWER({#{field}}))" }.join(',') + ")" params[:formulas].push f end self end
to_a()
click to toggle source
add kicker methods
# File lib/airmodel/query.rb, line 76 def to_a puts "RUNNING EXPENSIVE API QUERY TO AIRTABLE (#{@querying_class.name})" # merge explicit formulas and abstracted where-clauses into one Airtable Formula formula = "AND(" + params[:where_clauses].map{|k,v| "{#{k}}='#{v}'" }.join(',') + params[:formulas].join(',') + ")" records = @querying_class.table.records( sort: params[:order], filterByFormula: formula, limit: params[:limit], offset: params[:offset] ) @offset = records.offset @querying_class.classify records end
where(args)
click to toggle source
# File lib/airmodel/query.rb, line 18 def where(args) params[:where_clauses].merge!(args) self end