| | 1 | module ActiveRecord |
|---|
| | 2 | module AssociationPreload #:nodoc: |
|---|
| | 3 | def self.included(base) |
|---|
| | 4 | base.extend(ClassMethods) |
|---|
| | 5 | end |
|---|
| | 6 | |
|---|
| | 7 | module ClassMethods |
|---|
| | 8 | |
|---|
| | 9 | # Loads the named associations for the activerecord record (or records) given |
|---|
| | 10 | # preload_options is passed only one level deep: don't pass to the child associations when associations is a Hash |
|---|
| | 11 | protected |
|---|
| | 12 | def preload_associations(records, associations, preload_options={}) |
|---|
| | 13 | records = [records].flatten.compact |
|---|
| | 14 | return if records.empty? |
|---|
| | 15 | case associations |
|---|
| | 16 | when Array then associations.each {|association| preload_associations(records, association, preload_options)} |
|---|
| | 17 | when Symbol, String then preload_one_association(records, associations.to_sym, preload_options) |
|---|
| | 18 | when Hash then |
|---|
| | 19 | associations.each do |parent, child| |
|---|
| | 20 | raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol) |
|---|
| | 21 | preload_associations(records, parent, preload_options) |
|---|
| | 22 | reflection = reflections[parent] |
|---|
| | 23 | parents = records.map {|record| record.send(reflection.name)}.flatten |
|---|
| | 24 | unless parents.empty? |
|---|
| | 25 | parents.first.class.preload_associations(parents, child) |
|---|
| | 26 | end |
|---|
| | 27 | end |
|---|
| | 28 | end |
|---|
| | 29 | end |
|---|
| | 30 | |
|---|
| | 31 | private |
|---|
| | 32 | |
|---|
| | 33 | def preload_one_association(records, association, preload_options={}) |
|---|
| | 34 | reflection = reflections[association] |
|---|
| | 35 | raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection |
|---|
| | 36 | |
|---|
| | 37 | send(:"preload_#{reflection.macro}_association", records, reflection, preload_options) |
|---|
| | 38 | end |
|---|
| | 39 | |
|---|
| | 40 | def add_preloaded_records_to_collection(parent_records, reflection_name, associated_record) |
|---|
| | 41 | parent_records.each do |parent_record| |
|---|
| | 42 | association_proxy = parent_record.send(reflection_name) |
|---|
| | 43 | association_proxy.loaded |
|---|
| | 44 | association_proxy.target.push(*[associated_record].flatten) |
|---|
| | 45 | end |
|---|
| | 46 | end |
|---|
| | 47 | |
|---|
| | 48 | def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key) |
|---|
| | 49 | associated_records.each do |associated_record| |
|---|
| | 50 | mapped_records = id_to_record_map[associated_record[key].to_i] |
|---|
| | 51 | add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record) |
|---|
| | 52 | end |
|---|
| | 53 | end |
|---|
| | 54 | |
|---|
| | 55 | def set_association_single_records(id_to_record_map, reflection_name, associated_records, key) |
|---|
| | 56 | associated_records.each do |associated_record| |
|---|
| | 57 | mapped_records = id_to_record_map[associated_record[key].to_i] |
|---|
| | 58 | mapped_records.each do |mapped_record| |
|---|
| | 59 | mapped_record.send("set_#{reflection_name}_target", associated_record) |
|---|
| | 60 | end |
|---|
| | 61 | end |
|---|
| | 62 | end |
|---|
| | 63 | |
|---|
| | 64 | def construct_id_map(records) |
|---|
| | 65 | id_to_record_map = {} |
|---|
| | 66 | ids = [] |
|---|
| | 67 | records.each do |record| |
|---|
| | 68 | ids << record.id |
|---|
| | 69 | mapped_records = (id_to_record_map[record.id] ||= []) |
|---|
| | 70 | mapped_records << record |
|---|
| | 71 | end |
|---|
| | 72 | ids.uniq! |
|---|
| | 73 | return id_to_record_map, ids |
|---|
| | 74 | end |
|---|
| | 75 | |
|---|
| | 76 | def preload_has_and_belongs_to_many_association(records, reflection, preload_options={}) |
|---|
| | 77 | table_name = reflection.klass.table_name |
|---|
| | 78 | id_to_record_map, ids = construct_id_map(records) |
|---|
| | 79 | records.each {|record| record.send(reflection.name).loaded} |
|---|
| | 80 | options = reflection.options |
|---|
| | 81 | |
|---|
| | 82 | conditions = "t0.#{reflection.primary_key_name} IN (?)" |
|---|
| | 83 | conditions << append_conditions(options, preload_options) |
|---|
| | 84 | |
|---|
| | 85 | associated_records = reflection.klass.find(:all, :conditions => [conditions, ids], |
|---|
| | 86 | :include => options[:include], |
|---|
| | 87 | :joins => "INNER JOIN #{options[:join_table]} as t0 ON #{reflection.klass.table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}", |
|---|
| | 88 | :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as _parent_record_id", |
|---|
| | 89 | :order => options[:order]) |
|---|
| | 90 | |
|---|
| | 91 | set_association_collection_records(id_to_record_map, reflection.name, associated_records, '_parent_record_id') |
|---|
| | 92 | end |
|---|
| | 93 | |
|---|
| | 94 | def foreign_key_for_preload reflection |
|---|
| | 95 | options = reflection.options |
|---|
| | 96 | if options[:as] |
|---|
| | 97 | foreign_key = "#{options[:as]}_id" |
|---|
| | 98 | else |
|---|
| | 99 | foreign_key = options[:foreign_key] || reflection.active_record.to_s.foreign_key |
|---|
| | 100 | end |
|---|
| | 101 | end |
|---|
| | 102 | |
|---|
| | 103 | def preload_has_one_association(records, reflection, preload_options={}) |
|---|
| | 104 | id_to_record_map, ids = construct_id_map(records) |
|---|
| | 105 | records.each {|record| record.send("set_#{reflection.name}_target", nil)} |
|---|
| | 106 | |
|---|
| | 107 | set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), |
|---|
| | 108 | foreign_key_for_preload(reflection)) |
|---|
| | 109 | end |
|---|
| | 110 | |
|---|
| | 111 | def preload_has_many_association(records, reflection, preload_options={}) |
|---|
| | 112 | id_to_record_map, ids = construct_id_map(records) |
|---|
| | 113 | records.each {|record| record.send(reflection.name).loaded} |
|---|
| | 114 | options = reflection.options |
|---|
| | 115 | |
|---|
| | 116 | if options[:through] |
|---|
| | 117 | through_records = preload_through_records(records, reflection, options[:through]) |
|---|
| | 118 | through_reflection = reflections[options[:through]] |
|---|
| | 119 | through_primary_key = through_reflection.primary_key_name |
|---|
| | 120 | unless through_records.empty? |
|---|
| | 121 | source = reflection.source_reflection.name |
|---|
| | 122 | through_records.first.class.preload_associations(through_records, source) |
|---|
| | 123 | through_records.compact.each do |through_record| |
|---|
| | 124 | add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_i], |
|---|
| | 125 | reflection.name, through_record.send(source)) |
|---|
| | 126 | end |
|---|
| | 127 | end |
|---|
| | 128 | else |
|---|
| | 129 | set_association_collection_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), |
|---|
| | 130 | foreign_key_for_preload(reflection)) |
|---|
| | 131 | end |
|---|
| | 132 | end |
|---|
| | 133 | |
|---|
| | 134 | def preload_through_records(records, reflection, through_association) |
|---|
| | 135 | through_reflection = reflections[through_association] |
|---|
| | 136 | through_primary_key = through_reflection.primary_key_name |
|---|
| | 137 | |
|---|
| | 138 | if reflection.options[:source_type] |
|---|
| | 139 | interface = reflection.source_reflection.options[:foreign_type] |
|---|
| | 140 | preload_options = {:conditions => ["#{interface} = ?", reflection.options[:source_type]]} |
|---|
| | 141 | |
|---|
| | 142 | records.first.class.preload_associations(records, through_association, preload_options) |
|---|
| | 143 | |
|---|
| | 144 | # Dont cache the association - we would only be caching a subset |
|---|
| | 145 | through_records = [] |
|---|
| | 146 | records.compact.each do |record| |
|---|
| | 147 | proxy = record.send(through_association) |
|---|
| | 148 | through_records << proxy.target |
|---|
| | 149 | proxy.reset |
|---|
| | 150 | end |
|---|
| | 151 | through_records = through_records.flatten |
|---|
| | 152 | else |
|---|
| | 153 | records.first.class.preload_associations(records, through_association) |
|---|
| | 154 | through_records = records.compact.map {|record| record.send(through_association)}.flatten |
|---|
| | 155 | end |
|---|
| | 156 | end |
|---|
| | 157 | |
|---|
| | 158 | def preload_belongs_to_association(records, reflection, preload_options={}) |
|---|
| | 159 | options = reflection.options |
|---|
| | 160 | primary_key_name = reflection.primary_key_name |
|---|
| | 161 | |
|---|
| | 162 | if options[:polymorphic] |
|---|
| | 163 | polymorph_type = options[:foreign_type] |
|---|
| | 164 | klasses_and_ids = {} |
|---|
| | 165 | |
|---|
| | 166 | # Construct a mapping from klass to a list of ids to load and a mapping of those ids back to their parent_records |
|---|
| | 167 | records.each do |record| |
|---|
| | 168 | klass = record.send(polymorph_type) |
|---|
| | 169 | klass_id = record.send(primary_key_name) |
|---|
| | 170 | |
|---|
| | 171 | id_map = klasses_and_ids[klass] ||= {} |
|---|
| | 172 | id_list_for_klass_id = (id_map[klass_id] ||= []) |
|---|
| | 173 | id_list_for_klass_id << record |
|---|
| | 174 | end |
|---|
| | 175 | klasses_and_ids = klasses_and_ids.to_a |
|---|
| | 176 | else |
|---|
| | 177 | id_map = {} |
|---|
| | 178 | records.each do |record| |
|---|
| | 179 | mapped_records = (id_map[record.send(primary_key_name)] ||= []) |
|---|
| | 180 | mapped_records << record |
|---|
| | 181 | end |
|---|
| | 182 | klasses_and_ids = [[reflection.klass.name, id_map]] |
|---|
| | 183 | end |
|---|
| | 184 | |
|---|
| | 185 | klasses_and_ids.each do |klass_and_id| |
|---|
| | 186 | klass_name, id_map = *klass_and_id |
|---|
| | 187 | klass = klass_name.constantize |
|---|
| | 188 | |
|---|
| | 189 | table_name = klass.table_name |
|---|
| | 190 | conditions = "#{table_name}.#{primary_key} IN (?)" |
|---|
| | 191 | conditions << append_conditions(options, preload_options) |
|---|
| | 192 | associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq], |
|---|
| | 193 | :include => options[:include], |
|---|
| | 194 | :order => options[:order]) |
|---|
| | 195 | set_association_single_records(id_map, reflection.name, associated_records, 'id') |
|---|
| | 196 | end |
|---|
| | 197 | end |
|---|
| | 198 | |
|---|
| | 199 | def find_associated_records(ids, reflection, preload_options) |
|---|
| | 200 | options = reflection.options |
|---|
| | 201 | table_name = reflection.klass.table_name |
|---|
| | 202 | |
|---|
| | 203 | if interface = reflection.options[:as] |
|---|
| | 204 | conditions = "#{reflection.klass.table_name}.#{interface}_id IN (?) and #{reflection.klass.table_name}.#{interface}_type = '#{self.name}'" |
|---|
| | 205 | else |
|---|
| | 206 | foreign_key = options[:foreign_key] || reflection.active_record.to_s.foreign_key |
|---|
| | 207 | conditions = "#{reflection.klass.table_name}.#{foreign_key} IN (?)" |
|---|
| | 208 | end |
|---|
| | 209 | |
|---|
| | 210 | conditions << append_conditions(options, preload_options) |
|---|
| | 211 | |
|---|
| | 212 | reflection.klass.find(:all, |
|---|
| | 213 | :conditions => [conditions, ids], |
|---|
| | 214 | :select => (options[:select] || "#{table_name}.*"), |
|---|
| | 215 | :include => options[:include], |
|---|
| | 216 | :order => options[:order]) |
|---|
| | 217 | end |
|---|
| | 218 | |
|---|
| | 219 | |
|---|
| | 220 | def interpolate_sql_for_preload(sql) |
|---|
| | 221 | instance_eval("%@#{sql.gsub('@', '\@')}@") |
|---|
| | 222 | end |
|---|
| | 223 | |
|---|
| | 224 | def append_conditions(options, preload_options) |
|---|
| | 225 | sql = "" |
|---|
| | 226 | sql << " AND (#{interpolate_sql_for_preload(sanitize_sql(options[:conditions]))})" if options[:conditions] |
|---|
| | 227 | sql << " AND (#{sanitize_sql preload_options[:conditions]})" if preload_options[:conditions] |
|---|
| | 228 | sql |
|---|
| | 229 | end |
|---|
| | 230 | |
|---|
| | 231 | end |
|---|
| | 232 | end |
|---|
| | 233 | end |