module AssociationCount

Constants

VERSION

Public Class Methods

config() click to toggle source
# File lib/association_count.rb, line 55
def self.config
  configuration
end
configuration() click to toggle source
# File lib/association_count.rb, line 51
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/association_count.rb, line 59
def self.configure
  yield(configuration) if block_given?
  configuration
end

Public Instance Methods

association_count( counted_model, distinct: AssociationCount.config.distinct, join_type: AssociationCount.config.join_type ) click to toggle source
# File lib/association_count.rb, line 4
def association_count(
  counted_model,
  distinct: AssociationCount.config.distinct,
  join_type: AssociationCount.config.join_type
)
  table_name    = self.table_name
  counted_table = counted_model.table_name
  counted_name  = counted_table.singularize
  distinct_sql  = distinct ? 'DISTINCT' : ''

  public_send(join_type, counted_table.to_sym)
    .select("#{table_name}.*, COUNT(#{distinct_sql} #{counted_table}.id) as #{counted_name}_count_raw")
    .group("#{table_name}.id")
end
can_count(model_name, opts = {}) click to toggle source
# File lib/association_count.rb, line 19
def can_count(model_name, opts = {})
  model_name = model_name.to_s
  reflection = reflections[model_name]
  raise ArgumentError, "No such reflection: '#{model_name}'" unless reflection

  options = {
    distinct: AssociationCount.config.distinct,
    join_type: AssociationCount.config.join_type
  }.merge!(opts)
  singular_name = model_name.singularize

  define_association_count_method(model_name, singular_name)
  define_count_scope(singular_name, reflection, options[:distinct], options[:join_type])
end
define_association_count_method(model_name, singular_name) click to toggle source
# File lib/association_count.rb, line 34
def define_association_count_method(model_name, singular_name)
  define_method "#{singular_name}_count" do
    raw_count_name = "#{singular_name}_count_raw"
    return send(raw_count_name) if self.respond_to?(raw_count_name)
    send(model_name).count
  end
end
define_count_scope(singular_name, reflection, default_distinct, default_join_type) click to toggle source
# File lib/association_count.rb, line 42
def define_count_scope(singular_name, reflection, default_distinct, default_join_type)
  scope_name = "include_#{singular_name}_count"
  class_eval do
    scope scope_name, ->(distinct: default_distinct, join_type: default_join_type) {
      association_count(reflection.klass, distinct: distinct, join_type: join_type)
    }
  end
end