class ActiveRecord::Precount::CountLoaderBuilder

Public Class Methods

new(model) click to toggle source
# File lib/active_record/precount/count_loader_builder.rb, line 6
def initialize(model)
  @model = model
end

Public Instance Methods

build_from_has_many(name, scope, options) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

add_reflection(name, scope, options) click to toggle source
# File lib/active_record/precount/count_loader_builder.rb, line 35
def add_reflection(name, scope, options)
  valid_options = options.slice(*Associations::Builder::CountLoader.valid_options)
  reflection = Associations::Builder::CountLoader.build(@model, name, scope, valid_options)
  Reflection.add_reflection(@model, name, reflection)
end