Class: ActiveRecord::Collection

Constant Summary collapse

@@lock =
Mutex.new

Class Attribute Summary collapse

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

Class Attribute Details

.collectionsObject (readonly)

Returns the value of attribute collections.



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

def collections
  @collections
end

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



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

def collectable(klass=nil)
  @@lock.synchronize do
    unless klass.nil?
      raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless klass.ancestors.include?(ActiveRecord::Base)
      @collectable = klass
    end

    if @collectable.nil?
      begin
        klass = self.name.demodulize.singularize.constantize
        @collectable = klass if !klass.nil? && klass.ancestors.include?(ActiveRecord::Base)
      rescue
        # singularized class doesn't exist
      end
    end

    raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if @collectable.nil? # TODO implement real exceptions
    @collectable
  end
end

.inherited(subclass) ⇒ Object



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

def inherited(subclass)
  @@lock.synchronize do
    (@collections ||= []) << subclass
  end
end

.modelObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record/collection.rb', line 41

def collectable(klass=nil)
  @@lock.synchronize do
    unless klass.nil?
      raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless klass.ancestors.include?(ActiveRecord::Base)
      @collectable = klass
    end

    if @collectable.nil?
      begin
        klass = self.name.demodulize.singularize.constantize
        @collectable = klass if !klass.nil? && klass.ancestors.include?(ActiveRecord::Base)
      rescue
        # singularized class doesn't exist
      end
    end

    raise "Unable to determine a model to use for your collection, please set one with the `collectable` class method" if @collectable.nil? # TODO implement real exceptions
    @collectable
  end
end

Instance Method Details

#collectableObject Also known as: model



44
45
46
# File 'lib/active_record/collection.rb', line 44

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



51
52
53
54
55
56
57
# File 'lib/active_record/collection.rb', line 51

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