Module: ArV4Patch

Included in:
ActiveRecord::Associations::Preloader
Defined in:
lib/kashmir/patches/active_record.rb

Overview

We have to reopen Preloader to allow for it to accept any random attribute name as a preloadable association.

This allows us to send any arbitrary Hash to Preloader, without requiring it to be an ActiveRecord relation in advance.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
13
14
# File 'lib/kashmir/patches/active_record.rb', line 10

def self.included(klass)
  klass.instance_eval do
    remove_method :grouped_records
  end
end

Instance Method Details

#grouped_records(association, records) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kashmir/patches/active_record.rb', line 16

def grouped_records(association, records)
  h = {}
  records.each do |record|
    next unless record

    unless record.class._reflect_on_association(association)
      next
    end

    assoc = record.association(association)
    klasses = h[assoc.reflection] ||= {}
    (klasses[assoc.klass] ||= []) << record
  end

  h.delete(nil)
  h
end