Class: Airtable::ORM::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/airtable/orm/collection.rb

Overview

Thin wrapper around Array that enables chainable preloading.

cases = Airtable::Case.all.preload(:clients)
active = cases.select { |c| c[:state] == "Active" }.preload(:advisors)

Filtering methods (select, reject, sort_by) return new Collections, preserving the ability to chain preload.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, model_class:) ⇒ Collection

Returns a new instance of Collection.



15
16
17
18
# File 'lib/airtable/orm/collection.rb', line 15

def initialize(records, model_class:)
  super(records)
  @model_class = model_class
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



13
14
15
# File 'lib/airtable/orm/collection.rb', line 13

def model_class
  @model_class
end

Instance Method Details

#preload(*names) ⇒ Object

Eager-load associations for every record in the collection. Returns self so calls can be chained.



22
23
24
25
# File 'lib/airtable/orm/collection.rb', line 22

def preload(*names)
  model_class.preload(self, *names)
  self
end

#reject(&block) ⇒ Object



33
34
35
36
37
# File 'lib/airtable/orm/collection.rb', line 33

def reject(&block)
  return super unless block

  self.class.new(super, model_class: model_class)
end

#select(&block) ⇒ Object



27
28
29
30
31
# File 'lib/airtable/orm/collection.rb', line 27

def select(&block)
  return super unless block

  self.class.new(super, model_class: model_class)
end

#sort_by(&block) ⇒ Object



39
40
41
42
43
# File 'lib/airtable/orm/collection.rb', line 39

def sort_by(&block)
  return super unless block

  self.class.new(super, model_class: model_class)
end