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