Class: AirctiveRecord::Relation

Inherits:
Airrel::Relation
  • Object
show all
Defined in:
lib/airctiverecord/relation.rb

Overview

Base Relation class - models create subclasses of this Each model's relation knows about that model's field mappings and scopes

Instance Method Summary collapse

Instance Method Details

#spawnObject

spawn returns a new instance of the same Relation subclass



43
44
45
46
47
48
49
50
# File 'lib/airctiverecord/relation.rb', line 43

def spawn
  self.class.new(klass).tap do |relation|
    relation.instance_variable_set(:@where_clause, @where_clause)
    relation.instance_variable_set(:@order_values, @order_values.dup)
    relation.instance_variable_set(:@limit_value, @limit_value)
    relation.instance_variable_set(:@offset_value, @offset_value)
  end
end

#to_airtable_paramsObject

override to use field mappings when building airtable params



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/airctiverecord/relation.rb', line 19

def to_airtable_params
  params = {}

  # filter
  params[:filter] = @where_clause.to_airtable_formula if @where_clause.any?

  # sort - use field mappings
  if @order_values.any?
    params[:sort] = @order_values.map do |field, direction|
      mapped_field = klass.field_mappings[field.to_s] || field.to_s
      { field: mapped_field, direction: direction.to_s }
    end
  end

  # limit
  params[:max_records] = @limit_value if @limit_value

  # offset
  params[:offset] = @offset_value if @offset_value

  params
end

#where(conditions) ⇒ Object

override where to apply field mappings when building formulas from hashes



8
9
10
11
12
13
14
15
16
# File 'lib/airctiverecord/relation.rb', line 8

def where(conditions)
  if conditions.is_a?(Hash)
    # build formula with field mappings
    formula = Airrel::FormulaBuilder.hash_to_formula(conditions, klass.field_mappings)
    super(formula)
  else
    super
  end
end