Class: ActiveRecord::Collection

Constant Summary collapse

COLLECTABLES =
{}
COLLECTIONS =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::Collections::Pagination

#current_page, #default_per_page, #first_page?, included, #last_page?, #next_page, #next_page?, #out_of_range?, #page, #page!, #paginate!, #per, #per!, #per_page, #prev_page, #prev_page?, #total_pages

Methods included from ActiveRecord::Collections::Serialization

#as_json, included, #to_hash, #to_json, #to_param, #to_sql

Methods included from ActiveRecord::Collections::Delegation

included, #method_missing, #on_records, #on_relation, #respond_to_missing?

Methods included from ActiveRecord::Collections::Batching

#as_batch, #as_batches, #as_next_batch, #batch, #batch!, #batch_by_default?, #batch_map, #batch_size, #batchify!, #batching_threshold, #current_batch, #default_batch_size, #each_batch, #first_batch, #first_batch!, #flat_batch_map, included, #is_batch!, #is_batch?, #is_batched?, #last_batch, #last_batch!, #next_batch, #next_batch!, #next_batch?, #per_batch, #per_batch!, #prev_batch, #prev_batch!, #prev_batch?, #should_batch?, #to_batches, #total_batches

Methods included from ActiveRecord::Collections::Records

#each, #each_in_batches, #flat_map, #flat_map_in_batches, #length, #map, #map_in_batches, #pluck, #record_ids, #records, #size, #to_ary, #total_count

Methods included from ActiveRecord::Collections::Relation

#all, #distinct, #distinct!, #group, #group!, included, #includes, #includes!, #joins, #joins!, #limit, #limit!, #load, #loaded?, #not, #not!, #offset, #offset!, #or, #or!, #order, #order!, #references, #references!, #reset, #reset!, #select, #select!, #where, #where!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveRecord::Collections::Delegation

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/active_record/collection.rb', line 9

def options
  @options
end

#relationObject (readonly)

Returns the value of attribute relation.



9
10
11
# File 'lib/active_record/collection.rb', line 9

def relation
  @relation
end

Class Method Details

.collectable(klass = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/collection.rb', line 33

def collectable(klass=nil)
  if klass.nil?
    self.collectable = infer_collectable if kollektable.nil?
    raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if kollektable.nil?
  else
    self.collectable = klass
  end

  kollektable.constantize
end

.collectable=(klass) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/active_record/collection.rb', line 28

def collectable=(klass)
  raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless !klass.nil? && klass < ActiveRecord::Base
  ActiveRecord::Collection::COLLECTABLES[name] = klass.name
end

.collectionsObject



20
21
22
# File 'lib/active_record/collection.rb', line 20

def collections
  ActiveRecord::Collection::COLLECTIONS.map(&:constantize)
end

.infer_collectableObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/collection.rb', line 45

def infer_collectable
  singular = name.demodulize.singularize
  raise "Cannot infer collectable name from collection name" if singular == name.demodulize
  singular.constantize
rescue
  parent = ancestors[1]
  return nil unless parent < ActiveRecord::Collection
  begin
    parent.collectable
  rescue
    nil
  end
end

.inherited(subclass) ⇒ Object



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

def inherited(subclass)
  ActiveRecord::Collection::COLLECTIONS << subclass.name unless ActiveRecord::Collection::COLLECTIONS.include?(subclass.name)
  subclass.collectable rescue nil #noop
end

.kollektableObject



24
25
26
# File 'lib/active_record/collection.rb', line 24

def kollektable
  ActiveRecord::Collection::COLLECTABLES[name]
end

.modelObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/active_record/collection.rb', line 43

def collectable(klass=nil)
  if klass.nil?
    self.collectable = infer_collectable if kollektable.nil?
    raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if kollektable.nil?
  else
    self.collectable = klass
  end

  kollektable.constantize
end

Instance Method Details

#collectableObject Also known as: model



60
61
62
# File 'lib/active_record/collection.rb', line 60

def collectable
  @collectable# ||= self.class.collectable
end

#inspectObject

dup relation and call none so that we don’t end up inspecting it and loading it before we want it



67
68
69
70
71
72
73
# File 'lib/active_record/collection.rb', line 67

def inspect
  relation_backup = relation.dup
  @records = @relation = relation.none
  inspected = super
  @records = @relation = relation_backup
  inspected
end