Module: CompositePrimaryKeys::ActiveRecord::AssociationPreload::ClassMethods
- Defined in:
- lib/composite_primary_keys/association_preload.rb
Overview
Composite key versions of Association functions
Instance Method Summary collapse
-
#construct_id_map_for_composite(records) ⇒ Object
Given a collection of ActiveRecord objects, constructs a Hash which maps the objects’ IDs to the relevant objects.
- #find_associated_records(ids, reflection, preload_options) ⇒ Object
- #full_composite_join_clause(reflection, table1, full_keys1, table2, full_keys2) ⇒ Object
- #preload_belongs_to_association(records, reflection, preload_options = {}) ⇒ Object
- #preload_has_and_belongs_to_many_association(records, reflection, preload_options = {}) ⇒ Object
- #preload_has_many_association(records, reflection, preload_options = {}) ⇒ Object
- #preload_through_records(records, reflection, through_association) ⇒ Object
- #set_association_collection_records(id_to_record_map, reflection_name, associated_records, key) ⇒ Object
- #set_association_single_records(id_to_record_map, reflection_name, associated_records, key) ⇒ Object
Instance Method Details
#construct_id_map_for_composite(records) ⇒ Object
Given a collection of ActiveRecord objects, constructs a Hash which maps the objects’ IDs to the relevant objects. Returns a 2-tuple (id_to_record_map, ids)
where id_to_record_map
is the Hash, and ids
is an Array of record IDs.
226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/composite_primary_keys/association_preload.rb', line 226 def construct_id_map_for_composite(records) id_to_record_map = {} ids = [] records.each do |record| primary_key ||= record.class.primary_key ids << record.id mapped_records = (id_to_record_map[record.id.to_s] ||= []) mapped_records << record end ids.uniq! return id_to_record_map, ids end |
#find_associated_records(ids, reflection, preload_options) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/composite_primary_keys/association_preload.rb', line 189 def find_associated_records(ids, reflection, ) = reflection. table_name = reflection.klass.quoted_table_name if interface = reflection.[:as] raise AssociationNotSupported, "Polymorphic joins not supported for composite keys" else connection = reflection.active_record.connection foreign_key = reflection.primary_key_name conditions = ["#{table_name}.#{connection.quote_column_name(foreign_key)} IN (?)", ids] if composite? foreign_keys = foreign_key.to_s.split(CompositePrimaryKeys::ID_SEP) where = (foreign_keys * ids.size).in_groups_of(foreign_keys.size).map do |keys| "(" + keys.map{|key| "#{table_name}.#{connection.quote_column_name(key)} = ?"}.join(" AND ") + ")" end.join(" OR ") conditions = [where, ids].flatten end end conditions.first << append_conditions(reflection, ) reflection.klass.find(:all, :select => ([:select] || [:select] || "#{table_name}.*"), :include => [:include] || [:include], :conditions => conditions, :joins => [:joins], :group => [:group] || [:group], :order => [:order] || [:order]) end |
#full_composite_join_clause(reflection, table1, full_keys1, table2, full_keys2) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/composite_primary_keys/association_preload.rb', line 239 def full_composite_join_clause(reflection, table1, full_keys1, table2, full_keys2) connection = reflection.active_record.connection full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String) full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String) where_clause = [full_keys1, full_keys2].transpose.map do |key_pair| quoted1 = connection.quote_table_name(table1) quoted2 = connection.quote_table_name(table2) "#{quoted1}.#{connection.quote_column_name(key_pair.first)}=#{quoted2}.#{connection.quote_column_name(key_pair.last)}" end.join(" AND ") "(#{where_clause})" end |
#preload_belongs_to_association(records, reflection, preload_options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/composite_primary_keys/association_preload.rb', line 103 def preload_belongs_to_association(records, reflection, ={}) = reflection. primary_key_name = reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP) if [:polymorphic] raise AssociationNotSupported, "Polymorphic joins not supported for composite keys" else # I need to keep the original ids for each record (as opposed to the stringified) so # that they get properly converted for each db so the id_map ends up looking like: # # { '1,2' => {:id => [1,2], :records => [...records...]}} id_map = {} records.each do |record| key = primary_key_name.map{|k| record.send(k)} key_as_string = key.join(CompositePrimaryKeys::ID_SEP) if key_as_string mapped_records = (id_map[key_as_string] ||= {:id => key, :records => []}) mapped_records[:records] << record end end klasses_and_ids = [[reflection.klass.name, id_map]] end klasses_and_ids.each do |klass_and_id| klass_name, id_map = *klass_and_id klass = klass_name.constantize table_name = klass.quoted_table_name connection = reflection.active_record.connection if composite? primary_key = klass.primary_key.to_s.split(CompositePrimaryKeys::ID_SEP) ids = id_map.keys.uniq.map {|id| id_map[id][:id]} where = (primary_key * ids.size).in_groups_of(primary_key.size).map do |keys| "(" + keys.map{|key| "#{table_name}.#{connection.quote_column_name(key)} = ?"}.join(" AND ") + ")" end.join(" OR ") conditions = [where, ids].flatten else conditions = ["#{table_name}.#{connection.quote_column_name(primary_key)} IN (?)", id_map.keys.uniq] end conditions.first << append_conditions(reflection, ) associated_records = klass.find(:all, :conditions => conditions, :include => [:include], :select => [:select], :joins => [:joins], :order => [:order]) set_association_single_records(id_map, reflection.name, associated_records, primary_key) end end |
#preload_has_and_belongs_to_many_association(records, reflection, preload_options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/composite_primary_keys/association_preload.rb', line 11 def preload_has_and_belongs_to_many_association(records, reflection, ={}) table_name = reflection.klass.quoted_table_name id_to_record_map, ids = construct_id_map_for_composite(records) records.each {|record| record.send(reflection.name).loaded} = reflection. if composite? primary_key = reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP) where = (primary_key * ids.size).in_groups_of(primary_key.size).map do |keys| "(" + keys.map{|key| "t0.#{connection.quote_column_name(key)} = ?"}.join(" AND ") + ")" end.join(" OR ") conditions = [where, ids].flatten joins = "INNER JOIN #{connection.quote_table_name [:join_table]} t0 ON #{full_composite_join_clause(reflection, reflection.klass.table_name, reflection.klass.primary_key, 't0', reflection.association_foreign_key)}" parent_primary_keys = reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP).map{|k| "t0.#{connection.quote_column_name(k)}"} parent_record_id = connection.concat(*parent_primary_keys.zip(["','"] * (parent_primary_keys.size - 1)).flatten.compact) else conditions = ["t0.#{connection.quote_column_name(reflection.primary_key_name)} IN (?)", ids] joins = "INNER JOIN #{connection.quote_table_name [:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{connection.quote_column_name(reflection.klass.primary_key)} = t0.#{connection.quote_column_name(reflection.association_foreign_key)})" parent_record_id = reflection.primary_key_name end conditions.first << append_conditions(reflection, ) associated_records = reflection.klass.find(:all, :conditions => conditions, :include => [:include], :joins => joins, :select => "#{[:select] || table_name+'.*'}, #{parent_record_id} as parent_record_id_", :order => [:order]) set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'parent_record_id_') end |
#preload_has_many_association(records, reflection, preload_options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/composite_primary_keys/association_preload.rb', line 45 def preload_has_many_association(records, reflection, ={}) id_to_record_map, ids = construct_id_map_for_composite(records) records.each {|record| record.send(reflection.name).loaded} = reflection. if [:through] through_records = preload_through_records(records, reflection, [:through]) through_reflection = reflections[[:through]] through_primary_key = through_reflection.primary_key_name unless through_records.empty? source = reflection.source_reflection.name #add conditions from reflection! through_records.first.class.preload_associations(through_records, source, reflection.) through_records.each do |through_record| key = through_primary_key.to_s.split(CompositePrimaryKeys::ID_SEP).map{|k| through_record.send(k)}.join(CompositePrimaryKeys::ID_SEP) add_preloaded_records_to_collection(id_to_record_map[key], reflection.name, through_record.send(source)) end end else associated_records = find_associated_records(ids, reflection, ) set_association_collection_records(id_to_record_map, reflection.name, associated_records, reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP)) end end |
#preload_through_records(records, reflection, through_association) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/composite_primary_keys/association_preload.rb', line 70 def preload_through_records(records, reflection, through_association) through_reflection = reflections[through_association] through_primary_key = through_reflection.primary_key_name if reflection.[:source_type] interface = reflection.source_reflection.[:foreign_type] = {:conditions => ["#{connection.quote_column_name interface} = ?", reflection.[:source_type]]} records.compact! records.first.class.preload_associations(records, through_association, ) # Dont cache the association - we would only be caching a subset through_records = [] records.each do |record| proxy = record.send(through_association) if proxy.respond_to?(:target) through_records << proxy.target proxy.reset else # this is a has_one :through reflection through_records << proxy if proxy end end through_records.flatten! else records.first.class.preload_associations(records, through_association) through_records = records.map {|record| record.send(through_association)}.flatten end through_records.compact! through_records end |
#set_association_collection_records(id_to_record_map, reflection_name, associated_records, key) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/composite_primary_keys/association_preload.rb', line 162 def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key) associated_records.each do |associated_record| associated_record_key = associated_record[key] associated_record_key = associated_record_key.is_a?(Array) ? associated_record_key.join(CompositePrimaryKeys::ID_SEP) : associated_record_key.to_s mapped_records = id_to_record_map[associated_record_key] add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record) end end |
#set_association_single_records(id_to_record_map, reflection_name, associated_records, key) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/composite_primary_keys/association_preload.rb', line 171 def set_association_single_records(id_to_record_map, reflection_name, associated_records, key) seen_keys = {} associated_records.each do |associated_record| associated_record_key = associated_record[key] associated_record_key = associated_record_key.is_a?(Array) ? associated_record_key.join(CompositePrimaryKeys::ID_SEP) : associated_record_key.to_s #this is a has_one or belongs_to: there should only be one record. #Unfortunately we can't (in portable way) ask the database for 'all records where foo_id in (x,y,z), but please # only one row per distinct foo_id' so this where we enforce that next if seen_keys[associated_record_key] seen_keys[associated_record_key] = true mapped_records = id_to_record_map[associated_record_key][:records] mapped_records.each do |mapped_record| mapped_record.send("set_#{reflection_name}_target", associated_record) end end end |