class Airmodel::Model
Public Class Methods
# File lib/airmodel/model.rb, line 11 def self.all Query.new(self).all end
# File lib/airmodel/model.rb, line 19 def self.by_formula(args) Query.new(self).by_formula(args) end
create a new record and save it to Airtable
# File lib/airmodel/model.rb, line 74 def self.create(*models) results = models.map{|r| record = self.new(r) table.create(record) } results.length == 1 ? results.first : results end
default to whatever order Airtable returns override this method if you want to sort by something else
# File lib/airmodel/model.rb, line 64 def self.default_sort nil end
find a record by ID. IF you've (1) defined an 'id' Field in Airtable, (2) made it a formula, and (3) set the formula to RECORD_ID(), THEN you can pass self.find() and it will return each record in that order. This is mostly only useful for looking up records linked to a particular record.
# File lib/airmodel/model.rb, line 43 def self.find(id) if id.is_a? String results = self.classify table.find(id) results.count == 0 ? nil : results.first else formula = "OR(" + id.map{|x| "id='#{x}'" }.join(',') + ")" self.classify( table.records(filterByFormula: formula).sort_by do |x| id.index(x.id) end ) end end
find a record by specified attributes, return it
# File lib/airmodel/model.rb, line 58 def self.find_by(filters) Query.new(self).find_by(filters) end
return the first record
# File lib/airmodel/model.rb, line 69 def self.first Query.new(self).first end
# File lib/airmodel/model.rb, line 6 def self.inherited(base) Airmodel.warn super end
# File lib/airmodel/model.rb, line 33 def self.limit(args) Query.new(self).limit(args) end
Model.order
(“Name DESC”)
# File lib/airmodel/model.rb, line 28 def self.order(args) Query.new(self).order(*args) end
send a PATCH request to update a few fields on a record in one API call
# File lib/airmodel/model.rb, line 83 def self.patch(id, fields) r = table.update_record_fields(id, airtable_formatted(fields)) self.new(r.fields) end
# File lib/airmodel/model.rb, line 23 def self.search(args) Query.new(self).search(args) end
# File lib/airmodel/model.rb, line 15 def self.where(args) Query.new(self).where(args) end
Public Instance Methods
# File lib/airmodel/model.rb, line 134 def cache_key "#{self.class.table_name}_#{self.id}" end
# File lib/airmodel/model.rb, line 114 def changed_fields current = fields old = self.class.find_by(id: id).fields self.class.hash_diff(current, old) end
# File lib/airmodel/model.rb, line 120 def destroy self.table.destroy(id) end
override if you want to return validation errors
# File lib/airmodel/model.rb, line 144 def errors {} end
# File lib/airmodel/model.rb, line 97 def formatted_fields self.class.airtable_formatted(self.fields) end
# File lib/airmodel/model.rb, line 130 def new_record? id.nil? end
# File lib/airmodel/model.rb, line 101 def save if self.valid? if new_record? self.table.create(self) else result = self.table.update_record_fields(id, self.changed_fields) result end else false end end
return self.class.table. defined as an instance method to allow individual models to override it and connect to a different base in strange circumstances.
# File lib/airmodel/model.rb, line 93 def table self.class.table end
# File lib/airmodel/model.rb, line 124 def update(fields) res = self.table.update_record_fields(id, fields) res.fields.each{|field, value| self[field] = value } self end
override if you want to write server-side model validations
# File lib/airmodel/model.rb, line 139 def valid? true end