activerecord 3.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +35 -44
  5. data/examples/performance.rb +110 -100
  6. data/lib/active_record/aggregations.rb +59 -75
  7. data/lib/active_record/associations/alias_tracker.rb +76 -0
  8. data/lib/active_record/associations/association.rb +248 -0
  9. data/lib/active_record/associations/association_scope.rb +135 -0
  10. data/lib/active_record/associations/belongs_to_association.rb +60 -59
  11. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
  12. data/lib/active_record/associations/builder/association.rb +108 -0
  13. data/lib/active_record/associations/builder/belongs_to.rb +98 -0
  14. data/lib/active_record/associations/builder/collection_association.rb +89 -0
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
  16. data/lib/active_record/associations/builder/has_many.rb +15 -0
  17. data/lib/active_record/associations/builder/has_one.rb +25 -0
  18. data/lib/active_record/associations/builder/singular_association.rb +32 -0
  19. data/lib/active_record/associations/collection_association.rb +608 -0
  20. data/lib/active_record/associations/collection_proxy.rb +986 -0
  21. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +40 -112
  22. data/lib/active_record/associations/has_many_association.rb +83 -76
  23. data/lib/active_record/associations/has_many_through_association.rb +147 -66
  24. data/lib/active_record/associations/has_one_association.rb +67 -108
  25. data/lib/active_record/associations/has_one_through_association.rb +21 -25
  26. data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
  27. data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
  28. data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
  29. data/lib/active_record/associations/join_dependency.rb +235 -0
  30. data/lib/active_record/associations/join_helper.rb +45 -0
  31. data/lib/active_record/associations/preloader/association.rb +121 -0
  32. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  33. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  34. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
  35. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  36. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  37. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  38. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  39. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  40. data/lib/active_record/associations/preloader/through_association.rb +63 -0
  41. data/lib/active_record/associations/preloader.rb +178 -0
  42. data/lib/active_record/associations/singular_association.rb +64 -0
  43. data/lib/active_record/associations/through_association.rb +87 -0
  44. data/lib/active_record/associations.rb +512 -1224
  45. data/lib/active_record/attribute_assignment.rb +201 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
  47. data/lib/active_record/attribute_methods/dirty.rb +51 -28
  48. data/lib/active_record/attribute_methods/primary_key.rb +94 -22
  49. data/lib/active_record/attribute_methods/query.rb +5 -4
  50. data/lib/active_record/attribute_methods/read.rb +63 -72
  51. data/lib/active_record/attribute_methods/serialization.rb +162 -0
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
  53. data/lib/active_record/attribute_methods/write.rb +39 -13
  54. data/lib/active_record/attribute_methods.rb +362 -29
  55. data/lib/active_record/autosave_association.rb +132 -75
  56. data/lib/active_record/base.rb +83 -1627
  57. data/lib/active_record/callbacks.rb +69 -47
  58. data/lib/active_record/coders/yaml_column.rb +38 -0
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
  70. data/lib/active_record/connection_adapters/column.rb +318 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
  72. data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
  73. data/lib/active_record/connection_adapters/mysql_adapter.rb +365 -450
  74. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
  75. data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
  76. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
  81. data/lib/active_record/connection_adapters/postgresql_adapter.rb +672 -752
  82. data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
  83. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
  84. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  85. data/lib/active_record/connection_handling.rb +98 -0
  86. data/lib/active_record/core.rb +463 -0
  87. data/lib/active_record/counter_cache.rb +108 -101
  88. data/lib/active_record/dynamic_matchers.rb +131 -0
  89. data/lib/active_record/errors.rb +54 -13
  90. data/lib/active_record/explain.rb +38 -0
  91. data/lib/active_record/explain_registry.rb +30 -0
  92. data/lib/active_record/explain_subscriber.rb +29 -0
  93. data/lib/active_record/fixture_set/file.rb +55 -0
  94. data/lib/active_record/fixtures.rb +703 -785
  95. data/lib/active_record/inheritance.rb +200 -0
  96. data/lib/active_record/integration.rb +60 -0
  97. data/lib/active_record/locale/en.yml +8 -1
  98. data/lib/active_record/locking/optimistic.rb +69 -60
  99. data/lib/active_record/locking/pessimistic.rb +34 -12
  100. data/lib/active_record/log_subscriber.rb +40 -6
  101. data/lib/active_record/migration/command_recorder.rb +164 -0
  102. data/lib/active_record/migration/join_table.rb +15 -0
  103. data/lib/active_record/migration.rb +614 -216
  104. data/lib/active_record/model_schema.rb +345 -0
  105. data/lib/active_record/nested_attributes.rb +248 -119
  106. data/lib/active_record/null_relation.rb +65 -0
  107. data/lib/active_record/persistence.rb +275 -57
  108. data/lib/active_record/query_cache.rb +29 -9
  109. data/lib/active_record/querying.rb +62 -0
  110. data/lib/active_record/railtie.rb +135 -21
  111. data/lib/active_record/railties/console_sandbox.rb +5 -0
  112. data/lib/active_record/railties/controller_runtime.rb +17 -5
  113. data/lib/active_record/railties/databases.rake +249 -359
  114. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  115. data/lib/active_record/readonly_attributes.rb +30 -0
  116. data/lib/active_record/reflection.rb +283 -103
  117. data/lib/active_record/relation/batches.rb +38 -34
  118. data/lib/active_record/relation/calculations.rb +252 -139
  119. data/lib/active_record/relation/delegation.rb +125 -0
  120. data/lib/active_record/relation/finder_methods.rb +182 -188
  121. data/lib/active_record/relation/merger.rb +161 -0
  122. data/lib/active_record/relation/predicate_builder.rb +86 -21
  123. data/lib/active_record/relation/query_methods.rb +917 -134
  124. data/lib/active_record/relation/spawn_methods.rb +53 -92
  125. data/lib/active_record/relation.rb +405 -143
  126. data/lib/active_record/result.rb +67 -0
  127. data/lib/active_record/runtime_registry.rb +17 -0
  128. data/lib/active_record/sanitization.rb +168 -0
  129. data/lib/active_record/schema.rb +20 -14
  130. data/lib/active_record/schema_dumper.rb +55 -46
  131. data/lib/active_record/schema_migration.rb +39 -0
  132. data/lib/active_record/scoping/default.rb +146 -0
  133. data/lib/active_record/scoping/named.rb +175 -0
  134. data/lib/active_record/scoping.rb +82 -0
  135. data/lib/active_record/serialization.rb +8 -46
  136. data/lib/active_record/serializers/xml_serializer.rb +21 -68
  137. data/lib/active_record/statement_cache.rb +26 -0
  138. data/lib/active_record/store.rb +156 -0
  139. data/lib/active_record/tasks/database_tasks.rb +203 -0
  140. data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
  141. data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
  142. data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
  143. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  144. data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
  145. data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
  146. data/lib/active_record/test_case.rb +57 -28
  147. data/lib/active_record/timestamp.rb +49 -18
  148. data/lib/active_record/transactions.rb +106 -63
  149. data/lib/active_record/translation.rb +22 -0
  150. data/lib/active_record/validations/associated.rb +25 -24
  151. data/lib/active_record/validations/presence.rb +65 -0
  152. data/lib/active_record/validations/uniqueness.rb +123 -83
  153. data/lib/active_record/validations.rb +29 -29
  154. data/lib/active_record/version.rb +7 -5
  155. data/lib/active_record.rb +83 -34
  156. data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
  157. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  158. data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
  159. data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
  160. data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
  161. data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
  162. data/lib/rails/generators/active_record.rb +4 -8
  163. metadata +163 -121
  164. data/CHANGELOG +0 -6023
  165. data/examples/associations.png +0 -0
  166. data/lib/active_record/association_preload.rb +0 -403
  167. data/lib/active_record/associations/association_collection.rb +0 -562
  168. data/lib/active_record/associations/association_proxy.rb +0 -295
  169. data/lib/active_record/associations/through_association_scope.rb +0 -154
  170. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
  171. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
  172. data/lib/active_record/dynamic_finder_match.rb +0 -53
  173. data/lib/active_record/dynamic_scope_match.rb +0 -32
  174. data/lib/active_record/named_scope.rb +0 -138
  175. data/lib/active_record/observer.rb +0 -140
  176. data/lib/active_record/session_store.rb +0 -340
  177. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
  178. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  179. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
  180. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
  181. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
Binary file
@@ -1,403 +0,0 @@
1
- require 'active_support/core_ext/array/wrap'
2
- require 'active_support/core_ext/enumerable'
3
-
4
- module ActiveRecord
5
- # See ActiveRecord::AssociationPreload::ClassMethods for documentation.
6
- module AssociationPreload #:nodoc:
7
- extend ActiveSupport::Concern
8
-
9
- # Implements the details of eager loading of Active Record associations.
10
- # Application developers should not use this module directly.
11
- #
12
- # <tt>ActiveRecord::Base</tt> is extended with this module. The source code in
13
- # <tt>ActiveRecord::Base</tt> references methods defined in this module.
14
- #
15
- # Note that 'eager loading' and 'preloading' are actually the same thing.
16
- # However, there are two different eager loading strategies.
17
- #
18
- # The first one is by using table joins. This was only strategy available
19
- # prior to Rails 2.1. Suppose that you have an Author model with columns
20
- # 'name' and 'age', and a Book model with columns 'name' and 'sales'. Using
21
- # this strategy, Active Record would try to retrieve all data for an author
22
- # and all of its books via a single query:
23
- #
24
- # SELECT * FROM authors
25
- # LEFT OUTER JOIN books ON authors.id = books.id
26
- # WHERE authors.name = 'Ken Akamatsu'
27
- #
28
- # However, this could result in many rows that contain redundant data. After
29
- # having received the first row, we already have enough data to instantiate
30
- # the Author object. In all subsequent rows, only the data for the joined
31
- # 'books' table is useful; the joined 'authors' data is just redundant, and
32
- # processing this redundant data takes memory and CPU time. The problem
33
- # quickly becomes worse and worse as the level of eager loading increases
34
- # (i.e. if Active Record is to eager load the associations' associations as
35
- # well).
36
- #
37
- # The second strategy is to use multiple database queries, one for each
38
- # level of association. Since Rails 2.1, this is the default strategy. In
39
- # situations where a table join is necessary (e.g. when the +:conditions+
40
- # option references an association's column), it will fallback to the table
41
- # join strategy.
42
- #
43
- # See also ActiveRecord::Associations::ClassMethods, which explains eager
44
- # loading in a more high-level (application developer-friendly) manner.
45
- module ClassMethods
46
- protected
47
-
48
- # Eager loads the named associations for the given Active Record record(s).
49
- #
50
- # In this description, 'association name' shall refer to the name passed
51
- # to an association creation method. For example, a model that specifies
52
- # <tt>belongs_to :author</tt>, <tt>has_many :buyers</tt> has association
53
- # names +:author+ and +:buyers+.
54
- #
55
- # == Parameters
56
- # +records+ is an array of ActiveRecord::Base. This array needs not be flat,
57
- # i.e. +records+ itself may also contain arrays of records. In any case,
58
- # +preload_associations+ will preload the all associations records by
59
- # flattening +records+.
60
- #
61
- # +associations+ specifies one or more associations that you want to
62
- # preload. It may be:
63
- # - a Symbol or a String which specifies a single association name. For
64
- # example, specifying +:books+ allows this method to preload all books
65
- # for an Author.
66
- # - an Array which specifies multiple association names. This array
67
- # is processed recursively. For example, specifying <tt>[:avatar, :books]</tt>
68
- # allows this method to preload an author's avatar as well as all of his
69
- # books.
70
- # - a Hash which specifies multiple association names, as well as
71
- # association names for the to-be-preloaded association objects. For
72
- # example, specifying <tt>{ :author => :avatar }</tt> will preload a
73
- # book's author, as well as that author's avatar.
74
- #
75
- # +:associations+ has the same format as the +:include+ option for
76
- # <tt>ActiveRecord::Base.find</tt>. So +associations+ could look like this:
77
- #
78
- # :books
79
- # [ :books, :author ]
80
- # { :author => :avatar }
81
- # [ :books, { :author => :avatar } ]
82
- #
83
- # +preload_options+ contains options that will be passed to ActiveRecord::Base#find
84
- # (which is called under the hood for preloading records). But it is passed
85
- # only one level deep in the +associations+ argument, i.e. it's not passed
86
- # to the child associations when +associations+ is a Hash.
87
- def preload_associations(records, associations, preload_options={})
88
- records = Array.wrap(records).compact.uniq
89
- return if records.empty?
90
- case associations
91
- when Array then associations.each {|association| preload_associations(records, association, preload_options)}
92
- when Symbol, String then preload_one_association(records, associations.to_sym, preload_options)
93
- when Hash then
94
- associations.each do |parent, child|
95
- raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
96
- preload_associations(records, parent, preload_options)
97
- reflection = reflections[parent]
98
- parents = records.sum { |record| Array.wrap(record.send(reflection.name)) }
99
- unless parents.empty?
100
- parents.first.class.preload_associations(parents, child)
101
- end
102
- end
103
- end
104
- end
105
-
106
- private
107
-
108
- # Preloads a specific named association for the given records. This is
109
- # called by +preload_associations+ as its base case.
110
- def preload_one_association(records, association, preload_options={})
111
- class_to_reflection = {}
112
- # Not all records have the same class, so group then preload
113
- # group on the reflection itself so that if various subclass share the same association then
114
- # we do not split them unnecessarily
115
- records.group_by { |record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, _records|
116
- raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
117
-
118
- # 'reflection.macro' can return 'belongs_to', 'has_many', etc. Thus,
119
- # the following could call 'preload_belongs_to_association',
120
- # 'preload_has_many_association', etc.
121
- send("preload_#{reflection.macro}_association", _records, reflection, preload_options)
122
- end
123
- end
124
-
125
- def add_preloaded_records_to_collection(parent_records, reflection_name, associated_record)
126
- parent_records.each do |parent_record|
127
- association_proxy = parent_record.send(reflection_name)
128
- association_proxy.loaded
129
- association_proxy.target.push(*Array.wrap(associated_record))
130
-
131
- association_proxy.__send__(:set_inverse_instance, associated_record, parent_record)
132
- end
133
- end
134
-
135
- def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
136
- parent_records.each do |parent_record|
137
- parent_record.send("set_#{reflection_name}_target", associated_record)
138
- end
139
- end
140
-
141
- def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key)
142
- associated_records.each do |associated_record|
143
- mapped_records = id_to_record_map[associated_record[key].to_s]
144
- add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record)
145
- end
146
- end
147
-
148
- def set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
149
- seen_keys = {}
150
- associated_records.each do |associated_record|
151
- #this is a has_one or belongs_to: there should only be one record.
152
- #Unfortunately we can't (in portable way) ask the database for
153
- #'all records where foo_id in (x,y,z), but please
154
- # only one row per distinct foo_id' so this where we enforce that
155
- next if seen_keys[associated_record[key].to_s]
156
- seen_keys[associated_record[key].to_s] = true
157
- mapped_records = id_to_record_map[associated_record[key].to_s]
158
- mapped_records.each do |mapped_record|
159
- association_proxy = mapped_record.send("set_#{reflection_name}_target", associated_record)
160
- association_proxy.__send__(:set_inverse_instance, associated_record, mapped_record)
161
- end
162
- end
163
-
164
- id_to_record_map.each do |id, records|
165
- next if seen_keys.include?(id.to_s)
166
- records.each {|record| record.send("set_#{reflection_name}_target", nil) }
167
- end
168
- end
169
-
170
- # Given a collection of Active Record objects, constructs a Hash which maps
171
- # the objects' IDs to the relevant objects. Returns a 2-tuple
172
- # <tt>(id_to_record_map, ids)</tt> where +id_to_record_map+ is the Hash,
173
- # and +ids+ is an Array of record IDs.
174
- def construct_id_map(records, primary_key=nil)
175
- id_to_record_map = {}
176
- ids = []
177
- records.each do |record|
178
- primary_key ||= record.class.primary_key
179
- ids << record[primary_key]
180
- mapped_records = (id_to_record_map[ids.last.to_s] ||= [])
181
- mapped_records << record
182
- end
183
- ids.uniq!
184
- return id_to_record_map, ids
185
- end
186
-
187
- def preload_has_and_belongs_to_many_association(records, reflection, preload_options={})
188
- table_name = reflection.klass.quoted_table_name
189
- id_to_record_map, ids = construct_id_map(records)
190
- records.each {|record| record.send(reflection.name).loaded}
191
- options = reflection.options
192
-
193
- conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
194
- conditions << append_conditions(reflection, preload_options)
195
-
196
- associated_records = reflection.klass.unscoped.where([conditions, ids]).
197
- includes(options[:include]).
198
- joins("INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}").
199
- select("#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id").
200
- order(options[:order]).to_a
201
-
202
- set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'the_parent_record_id')
203
- end
204
-
205
- def preload_has_one_association(records, reflection, preload_options={})
206
- return if records.first.send("loaded_#{reflection.name}?")
207
- id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
208
- options = reflection.options
209
- records.each {|record| record.send("set_#{reflection.name}_target", nil)}
210
- if options[:through]
211
- through_records = preload_through_records(records, reflection, options[:through])
212
- through_reflection = reflections[options[:through]]
213
- through_primary_key = through_reflection.primary_key_name
214
- unless through_records.empty?
215
- source = reflection.source_reflection.name
216
- through_records.first.class.preload_associations(through_records, source)
217
- if through_reflection.macro == :belongs_to
218
- rev_id_to_record_map, rev_ids = construct_id_map(records, through_primary_key)
219
- rev_primary_key = through_reflection.klass.primary_key
220
- through_records.each do |through_record|
221
- add_preloaded_record_to_collection(rev_id_to_record_map[through_record[rev_primary_key].to_s],
222
- reflection.name, through_record.send(source))
223
- end
224
- else
225
- through_records.each do |through_record|
226
- add_preloaded_record_to_collection(id_to_record_map[through_record[through_primary_key].to_s],
227
- reflection.name, through_record.send(source))
228
- end
229
- end
230
- end
231
- else
232
- set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
233
- end
234
- end
235
-
236
- def preload_has_many_association(records, reflection, preload_options={})
237
- return if records.first.send(reflection.name).loaded?
238
- options = reflection.options
239
-
240
- primary_key_name = reflection.through_reflection_primary_key_name
241
- id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
242
- records.each {|record| record.send(reflection.name).loaded}
243
-
244
- if options[:through]
245
- through_records = preload_through_records(records, reflection, options[:through])
246
- through_reflection = reflections[options[:through]]
247
- unless through_records.empty?
248
- source = reflection.source_reflection.name
249
- through_records.first.class.preload_associations(through_records, source, options)
250
- through_records.each do |through_record|
251
- through_record_id = through_record[reflection.through_reflection_primary_key].to_s
252
- add_preloaded_records_to_collection(id_to_record_map[through_record_id], reflection.name, through_record.send(source))
253
- end
254
- end
255
-
256
- else
257
- set_association_collection_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options),
258
- reflection.primary_key_name)
259
- end
260
- end
261
-
262
- def preload_through_records(records, reflection, through_association)
263
- through_reflection = reflections[through_association]
264
- through_primary_key = through_reflection.primary_key_name
265
-
266
- through_records = []
267
- if reflection.options[:source_type]
268
- interface = reflection.source_reflection.options[:foreign_type]
269
- preload_options = {:conditions => ["#{connection.quote_column_name interface} = ?", reflection.options[:source_type]]}
270
-
271
- records.compact!
272
- records.first.class.preload_associations(records, through_association, preload_options)
273
-
274
- # Dont cache the association - we would only be caching a subset
275
- records.each do |record|
276
- proxy = record.send(through_association)
277
-
278
- if proxy.respond_to?(:target)
279
- through_records.concat Array.wrap(proxy.target)
280
- proxy.reset
281
- else # this is a has_one :through reflection
282
- through_records << proxy if proxy
283
- end
284
- end
285
- else
286
- options = {}
287
- options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions]
288
- options[:order] = reflection.options[:order]
289
- options[:conditions] = reflection.options[:conditions]
290
- records.first.class.preload_associations(records, through_association, options)
291
-
292
- records.each do |record|
293
- through_records.concat Array.wrap(record.send(through_association))
294
- end
295
- end
296
- through_records
297
- end
298
-
299
- def preload_belongs_to_association(records, reflection, preload_options={})
300
- return if records.first.send("loaded_#{reflection.name}?")
301
- options = reflection.options
302
- primary_key_name = reflection.primary_key_name
303
-
304
- if options[:polymorphic]
305
- polymorph_type = options[:foreign_type]
306
- klasses_and_ids = {}
307
-
308
- # Construct a mapping from klass to a list of ids to load and a mapping of those ids back
309
- # to their parent_records
310
- records.each do |record|
311
- if klass = record.send(polymorph_type)
312
- klass_id = record.send(primary_key_name)
313
- if klass_id
314
- id_map = klasses_and_ids[klass] ||= {}
315
- id_list_for_klass_id = (id_map[klass_id.to_s] ||= [])
316
- id_list_for_klass_id << record
317
- end
318
- end
319
- end
320
- klasses_and_ids = klasses_and_ids.to_a
321
- else
322
- id_map = {}
323
- records.each do |record|
324
- key = record.send(primary_key_name)
325
- if key
326
- mapped_records = (id_map[key.to_s] ||= [])
327
- mapped_records << record
328
- end
329
- end
330
- klasses_and_ids = [[reflection.klass.name, id_map]]
331
- end
332
-
333
- klasses_and_ids.each do |klass_and_id|
334
- klass_name, id_map = *klass_and_id
335
- next if id_map.empty?
336
- klass = klass_name.constantize
337
-
338
- table_name = klass.quoted_table_name
339
- primary_key = reflection.options[:primary_key] || klass.primary_key
340
- column_type = klass.columns.detect{|c| c.name == primary_key}.type
341
-
342
- ids = id_map.keys.map do |id|
343
- if column_type == :integer
344
- id.to_i
345
- elsif column_type == :float
346
- id.to_f
347
- else
348
- id
349
- end
350
- end
351
-
352
- conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
353
- conditions << append_conditions(reflection, preload_options)
354
-
355
- associated_records = klass.unscoped.where([conditions, ids]).apply_finder_options(options.slice(:include, :select, :joins, :order)).to_a
356
-
357
- set_association_single_records(id_map, reflection.name, associated_records, primary_key)
358
- end
359
- end
360
-
361
- def find_associated_records(ids, reflection, preload_options)
362
- options = reflection.options
363
- table_name = reflection.klass.quoted_table_name
364
-
365
- if interface = reflection.options[:as]
366
- conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} #{in_or_equals_for_ids(ids)} and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.sti_name}'"
367
- else
368
- foreign_key = reflection.primary_key_name
369
- conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} #{in_or_equals_for_ids(ids)}"
370
- end
371
-
372
- conditions << append_conditions(reflection, preload_options)
373
-
374
- find_options = {
375
- :select => preload_options[:select] || options[:select] || Arel::SqlLiteral.new("#{table_name}.*"),
376
- :include => preload_options[:include] || options[:include],
377
- :conditions => [conditions, ids],
378
- :joins => options[:joins],
379
- :group => preload_options[:group] || options[:group],
380
- :order => preload_options[:order] || options[:order]
381
- }
382
-
383
- reflection.klass.scoped.apply_finder_options(find_options).to_a
384
- end
385
-
386
-
387
- def interpolate_sql_for_preload(sql)
388
- instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
389
- end
390
-
391
- def append_conditions(reflection, preload_options)
392
- sql = ""
393
- sql << " AND (#{interpolate_sql_for_preload(reflection.sanitized_conditions)})" if reflection.sanitized_conditions
394
- sql << " AND (#{sanitize_sql preload_options[:conditions]})" if preload_options[:conditions]
395
- sql
396
- end
397
-
398
- def in_or_equals_for_ids(ids)
399
- ids.size > 1 ? "IN (?)" : "= ?"
400
- end
401
- end
402
- end
403
- end