class Airmodel::Model

Public Class Methods

all() click to toggle source
# File lib/airmodel/model.rb, line 11
def self.all
  Query.new(self).all
end
by_formula(args) click to toggle source
# File lib/airmodel/model.rb, line 19
def self.by_formula(args)
  Query.new(self).by_formula(args)
end
create(*models) click to toggle source

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_sort() click to toggle source

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(id) click to toggle source

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_by(filters) click to toggle source

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
first() click to toggle source

return the first record

# File lib/airmodel/model.rb, line 69
def self.first
  Query.new(self).first
end
inherited(base) click to toggle source
Calls superclass method
# File lib/airmodel/model.rb, line 6
def self.inherited(base)
  Airmodel.warn
  super
end
limit(args) click to toggle source

Model.limit(10)

# File lib/airmodel/model.rb, line 33
def self.limit(args)
  Query.new(self).limit(args)
end
order(args) click to toggle source

Model.order(“Name DESC”)

# File lib/airmodel/model.rb, line 28
def self.order(args)
  Query.new(self).order(*args)
end
patch(id, fields) click to toggle source

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
where(args) click to toggle source
# File lib/airmodel/model.rb, line 15
def self.where(args)
  Query.new(self).where(args)
end

Public Instance Methods

cache_key() click to toggle source
# File lib/airmodel/model.rb, line 134
def cache_key
  "#{self.class.table_name}_#{self.id}"
end
changed_fields() click to toggle source
# 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
destroy() click to toggle source
# File lib/airmodel/model.rb, line 120
def destroy
  self.table.destroy(id)
end
errors() click to toggle source

override if you want to return validation errors

# File lib/airmodel/model.rb, line 144
def errors
  {}
end
formatted_fields() click to toggle source
# File lib/airmodel/model.rb, line 97
def formatted_fields
  self.class.airtable_formatted(self.fields)
end
new_record?() click to toggle source
# File lib/airmodel/model.rb, line 130
def new_record?
  id.nil?
end
save() click to toggle source
# 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
table() click to toggle source

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
update(fields) click to toggle source
# 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
valid?() click to toggle source

override if you want to write server-side model validations

# File lib/airmodel/model.rb, line 139
def valid?
  true
end