Class: ActiveRecord::Precounter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/precounter.rb,
lib/active_record/precounter/version.rb

Defined Under Namespace

Classes: MissingInverseOf

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Precounter

Returns a new instance of Precounter.

Parameters:

  • relation (ActiveRecord::Relation)
    • Parent resources relation.


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

def initialize(relation)
  @relation = relation
end

Instance Method Details

#precount(*association_names) ⇒ Array<ActiveRecord::Base>

Parameters:

  • association_names (Array<String,Symbol>)
    • Eager loaded association names. e.g. ‘[:users, :likes]`

Returns:

  • (Array<ActiveRecord::Base>)

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/precounter.rb', line 15

def precount(*association_names)
  # Allow single record instances as well as relations to be passed.
  # The splat here will return an array of the single record if it's
  # not a relation or otherwise return the records themselves via #to_a.
  records = *@relation
  return records if records.empty?

  # We need to get the relation's active class, which is the class itself
  # in the case of a single record.
  klass = @relation.respond_to?(:klass) ? @relation.klass : @relation.class

  association_names.each do |association_name|
    association_name = association_name.to_s
    reflection = klass.reflections.fetch(association_name)

    if reflection.inverse_of.nil?
      raise MissingInverseOf.new(
        "`#{reflection.klass}` does not have inverse of `#{klass}##{reflection.name}`. "\
        "Probably missing to call `#{reflection.klass}.belongs_to #{klass.name.underscore.to_sym.inspect}`?"
      )
    end

    primary_key = reflection.inverse_of.association_primary_key.to_sym

    count_by_id = if reflection.has_scope?
                    # ActiveRecord 5.0 unscopes #scope_for argument, so adding #where outside that:
                    # https://github.com/rails/rails/blob/v5.0.7/activerecord/lib/active_record/reflection.rb#L314-L316
                    reflection.scope_for(reflection.klass.unscoped).where(reflection.inverse_of.name => records.map(&primary_key)).group(
                      reflection.inverse_of.foreign_key
                    ).count
                  else
                    reflection.klass.where(reflection.inverse_of.name => records.map(&primary_key)).group(
                      reflection.inverse_of.foreign_key
                    ).count
                  end

    writer = define_count_accessor(klass, association_name)
    records.each do |record|
      record.public_send(writer, count_by_id.fetch(record.public_send(primary_key), 0))
    end
  end
  records
end