Sha256: d3b569a7fa4358749a79e8e80305dbaf8c568e66e936a65975b09caab6a2a2d1

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

require 'active_record/precount/reflection_checker'

module ActiveRecord
  module Precount
    class CountLoaderBuilder
      def initialize(model)
        @model = model
      end

      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

      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

      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
    end
  end

  module Associations
    module Builder
      class CountLoader < SingularAssociation
        def self.valid_options(*)
          [:class, :class_name, :foreign_key]
        end

        if ActiveRecord.version.segments.first >= 5
          def self.macro
            :count_loader
          end
        else
          def macro
            :count_loader
          end
        end

        def self.valid_dependent_options
          []
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activerecord-precount-0.7.0 lib/active_record/precount/count_loader_builder.rb
activerecord-precount-0.7.0.beta1 lib/active_record/precount/count_loader_builder.rb
activerecord-precount-0.6.3 lib/active_record/precount/count_loader_builder.rb
activerecord-precount-0.6.2 lib/active_record/precount/count_loader_builder.rb
activerecord-precount-0.6.1 lib/active_record/precount/count_loader_builder.rb