Class: ActiveRecord::Embedded::Relation

Inherits:
Object
  • Object
show all
Includes:
Query
Defined in:
lib/active_record/embedded/relation.rb

Overview

Collection of embedded models represented in a similar way as an ActiveRecord::Relation. Stores a query in memory which is applied when data is requested, (e.g. the #each method is called).

Instance Method Summary collapse

Methods included from Query

#build, #create, #create!, #find, #find!, #find_by, #find_by!, #initialize, #order, #where

Instance Method Details

#eachObject

Apply query and iterate over each model in the collection.


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/embedded/relation.rb', line 29

def each
  if model[association.name]['index'].key?(query_name)
    values = model[association.name]['index'][query_name]['values']
    indexes = params.values.map { |value| values.index(value) }
    data = indexes.map { |index| model[association.name]['data'][index] }
  else
    data = model[association.name]['data']
    data = apply_filters!(data)
    data = apply_sorts!(data)
  end

  data.each { |params| yield build(params) }
end

#inspectString

Return a subset of the results in this query, up to the first 10.

Returns:

  • (String)

    Human-readable representation of this object


46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/embedded/relation.rb', line 46

def inspect
  entries = if limit_value == -1
              take(11).map!(&:inspect)
            else
              take([limit_value, 11].compact.min).map!(&:inspect)
            end
  entries[10] = '...' if entries.size == 11

  "#<#{self.class.name} [#{entries.join(', ')}]>"
end

#query_nameString

Name of this query, if it matches an index then an index can be used.

Returns:

  • (String)

18
19
20
21
22
23
24
# File 'lib/active_record/embedded/relation.rb', line 18

def query_name
  if filters.one?
    filters.keys.first.to_s
  else
    filters.keys.join('_and_')
  end
end