Class: ActiveRecord::Precount::CountLoaderBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/precount/count_loader_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ CountLoaderBuilder

Returns a new instance of CountLoaderBuilder.



6
7
8
# File 'lib/active_record/precount/count_loader_builder.rb', line 6

def initialize(model)
  @model = model
end

Instance Method Details

#build_from_has_many(name, scope, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/precount/count_loader_builder.rb', line 10

def build_from_has_many(name, scope, options)
  name_with_count =
    if options[:count_loader].is_a?(Symbol)
      options[:count_loader]
    else
      :"#{name}_count"
    end

  add_reflection(name_with_count, scope, options)
end

#build_from_query_methods(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/precount/count_loader_builder.rb', line 21

def build_from_query_methods(*args)
  args.each do |arg|
    next if ReflectionChecker.has_reflection?(@model, counter_name = :"#{arg}_count")
    unless ReflectionChecker.has_reflection?(@model, arg)
      raise ArgumentError, "Association named '#{arg}' was not found on #{@model.name}."
    end

    original_reflection = @model.reflections[arg.to_s]
    add_reflection(counter_name, original_reflection.scope, original_reflection.options)
  end
end