Class: ActiveRecord::Embedded::Aggregation Abstract

Inherits:
Object
  • Object
show all
Includes:
Query
Defined in:
lib/active_record/embedded/aggregation.rb,
lib/active_record/embedded/aggregation/native.rb,
lib/active_record/embedded/aggregation/postgresql.rb

Overview

This class is abstract.

Subclass to define a new adapter

Aggregations are queries made across the entire database, rather than within a certain record. Since aggregations require

Direct Known Subclasses

Native

Defined Under Namespace

Classes: Native, Postgresql

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Query

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

Constructor Details

#initialize(*_args) ⇒ Aggregation


16
17
18
19
# File 'lib/active_record/embedded/aggregation.rb', line 16

def initialize(*_args)
  super
  @association ||= parent_model&.association
end

Class Method Details

.create(**options) ⇒ Aggregation

Shorthand for defining a new aggregation with the correct adapter.


40
41
42
# File 'lib/active_record/embedded/aggregation.rb', line 40

def self.create(**options)
  find(Embedded.config.adapter).new(**options)
end

.find(id = :native) ⇒ Class

Find an adapter class for the given config.adapter. Fall back to the native adapter (e.g., iterating in Ruby) when none can be found.


27
28
29
30
31
32
33
34
# File 'lib/active_record/embedded/aggregation.rb', line 27

def self.find(id = :native)
  driver = id.to_s.demodulize.classify
  "ActiveRecord::Embedded::Aggregation::#{driver}".constantize
rescue NameError
  Rails.logger.debug("Database '#{id}' has no embedded adapter")
  Rails.logger.debug('Falling back to native...')
  ActiveRecord::Embedded::Aggregation::Native
end

Instance Method Details

#each {|ActiveRecord::Embedded::Model| ... } ⇒ Object

Instantiate the proper model objects for each search result in the query, populating said object with the state of its parent model/association as well.


49
50
51
52
53
54
55
# File 'lib/active_record/embedded/aggregation.rb', line 49

def each
  results.each do |model, items|
    items.each do |item|
      yield build(model, item)
    end
  end
end

#inspectObject


66
67
68
69
70
71
72
73
74
75
# File 'lib/active_record/embedded/aggregation.rb', line 66

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

#resultsArray<Array>

This method is abstract.

Override this method to define behavior when

aggregation query needs to retrieve results from the database. This should be a 2-dimensional Array with the values [model, params]

Raises:

  • (NotImplementedError)

62
63
64
# File 'lib/active_record/embedded/aggregation.rb', line 62

def results
  raise NotImplementedError, "#{self.class.name}#results"
end