Class: ActiveRecord::Associations::CountPreloader::CountHasManyThrough

Inherits:
Count
  • Object
show all
Includes:
ThroughAssociation
Defined in:
lib/includes-count.rb

Instance Method Summary collapse

Methods inherited from Count

#build_scope, #count_name, #initialize, #preload

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::CountPreloader::Count

Instance Method Details

#associated_records_by_ownerObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/includes-count.rb', line 44

def associated_records_by_owner          
  through_records = through_records_by_owner

  ActiveRecord::Associations::CountPreloader.new(
    through_records.values.flatten,
    source_reflection.name, options.merge(@preload_options)
  ).run

  through_records
  
end

#through_optionsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/includes-count.rb', line 74

def through_options
  through_options = {}

  if options[:source_type]
    through_options[:conditions] = { reflection.foreign_type => options[:source_type] }
  else
    if options[:conditions]
      through_options[:include]    = options[:include] || options[:source]
      through_options[:conditions] = options[:conditions]
    end

    through_options[:order] = options[:order]
  end
  
  if @preload_options[:through_options]
    through_preload_options = @preload_options[:through_options][through_reflection.name.to_sym] || {}
    through_options.merge!(through_preload_options)
  end

  through_options
end

#through_records_by_ownerObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/includes-count.rb', line 56

def through_records_by_owner
  ActiveRecord::Associations::Preloader.new(
    owners, through_reflection.name,
    through_options
  ).run

  Hash[owners.map do |owner|
    through_records = Array.wrap(owner.send(through_reflection.name))

    # Dont cache the association - we would only be caching a subset
    if reflection.options[:source_type] && through_reflection.collection?
      owner.association(through_reflection.name).reset
    end

    [owner, through_records]
  end]
end