activerecord 3.2.22.4 → 4.0.13
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2799 -617
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -32
- data/examples/performance.rb +1 -1
- data/lib/active_record/aggregations.rb +40 -34
- data/lib/active_record/association_relation.rb +22 -0
- data/lib/active_record/associations/alias_tracker.rb +4 -2
- data/lib/active_record/associations/association.rb +60 -46
- data/lib/active_record/associations/association_scope.rb +46 -40
- data/lib/active_record/associations/belongs_to_association.rb +17 -4
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +1 -1
- data/lib/active_record/associations/builder/association.rb +81 -28
- data/lib/active_record/associations/builder/belongs_to.rb +73 -56
- data/lib/active_record/associations/builder/collection_association.rb +54 -40
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +23 -41
- data/lib/active_record/associations/builder/has_many.rb +8 -64
- data/lib/active_record/associations/builder/has_one.rb +13 -50
- data/lib/active_record/associations/builder/singular_association.rb +13 -13
- data/lib/active_record/associations/collection_association.rb +130 -96
- data/lib/active_record/associations/collection_proxy.rb +916 -63
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +15 -13
- data/lib/active_record/associations/has_many_association.rb +35 -8
- data/lib/active_record/associations/has_many_through_association.rb +37 -17
- data/lib/active_record/associations/has_one_association.rb +42 -19
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +39 -22
- data/lib/active_record/associations/join_dependency/join_base.rb +2 -2
- data/lib/active_record/associations/join_dependency/join_part.rb +21 -8
- data/lib/active_record/associations/join_dependency.rb +30 -9
- data/lib/active_record/associations/join_helper.rb +1 -11
- data/lib/active_record/associations/preloader/association.rb +29 -33
- data/lib/active_record/associations/preloader/collection_association.rb +1 -1
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +2 -2
- data/lib/active_record/associations/preloader/has_many_through.rb +6 -2
- data/lib/active_record/associations/preloader/has_one.rb +1 -1
- data/lib/active_record/associations/preloader/through_association.rb +13 -17
- data/lib/active_record/associations/preloader.rb +20 -43
- data/lib/active_record/associations/singular_association.rb +11 -11
- data/lib/active_record/associations/through_association.rb +3 -3
- data/lib/active_record/associations.rb +223 -282
- data/lib/active_record/attribute_assignment.rb +134 -154
- data/lib/active_record/attribute_methods/before_type_cast.rb +44 -5
- data/lib/active_record/attribute_methods/dirty.rb +36 -29
- data/lib/active_record/attribute_methods/primary_key.rb +45 -31
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +67 -90
- data/lib/active_record/attribute_methods/serialization.rb +133 -70
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +51 -45
- data/lib/active_record/attribute_methods/write.rb +34 -39
- data/lib/active_record/attribute_methods.rb +268 -108
- data/lib/active_record/autosave_association.rb +80 -73
- data/lib/active_record/base.rb +54 -451
- data/lib/active_record/callbacks.rb +60 -22
- data/lib/active_record/coders/yaml_column.rb +18 -21
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +347 -197
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +146 -138
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +25 -19
- data/lib/active_record/connection_adapters/abstract/quoting.rb +19 -3
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +151 -142
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +499 -217
- data/lib/active_record/connection_adapters/abstract/transaction.rb +208 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +209 -44
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +169 -61
- data/lib/active_record/connection_adapters/column.rb +67 -36
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +28 -29
- data/lib/active_record/connection_adapters/mysql_adapter.rb +200 -73
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +98 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +160 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +240 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +374 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +183 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +508 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +544 -899
- data/lib/active_record/connection_adapters/schema_cache.rb +76 -16
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +595 -16
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +472 -0
- data/lib/active_record/counter_cache.rb +107 -108
- data/lib/active_record/dynamic_matchers.rb +115 -63
- data/lib/active_record/errors.rb +36 -18
- data/lib/active_record/explain.rb +15 -63
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +8 -4
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +159 -155
- data/lib/active_record/inheritance.rb +93 -59
- data/lib/active_record/integration.rb +8 -8
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +39 -43
- data/lib/active_record/locking/pessimistic.rb +4 -4
- data/lib/active_record/log_subscriber.rb +19 -9
- data/lib/active_record/migration/command_recorder.rb +102 -33
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +411 -173
- data/lib/active_record/model_schema.rb +81 -94
- data/lib/active_record/nested_attributes.rb +173 -131
- data/lib/active_record/null_relation.rb +67 -0
- data/lib/active_record/persistence.rb +254 -106
- data/lib/active_record/query_cache.rb +18 -36
- data/lib/active_record/querying.rb +19 -15
- data/lib/active_record/railtie.rb +113 -38
- data/lib/active_record/railties/console_sandbox.rb +3 -4
- data/lib/active_record/railties/controller_runtime.rb +4 -3
- data/lib/active_record/railties/databases.rake +115 -368
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +7 -3
- data/lib/active_record/reflection.rb +110 -61
- data/lib/active_record/relation/batches.rb +29 -29
- data/lib/active_record/relation/calculations.rb +155 -125
- data/lib/active_record/relation/delegation.rb +94 -18
- data/lib/active_record/relation/finder_methods.rb +151 -203
- data/lib/active_record/relation/merger.rb +188 -0
- data/lib/active_record/relation/predicate_builder.rb +85 -42
- data/lib/active_record/relation/query_methods.rb +793 -146
- data/lib/active_record/relation/spawn_methods.rb +43 -150
- data/lib/active_record/relation.rb +293 -173
- data/lib/active_record/result.rb +48 -7
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +41 -54
- data/lib/active_record/schema.rb +19 -12
- data/lib/active_record/schema_dumper.rb +41 -41
- data/lib/active_record/schema_migration.rb +46 -0
- data/lib/active_record/scoping/default.rb +56 -52
- data/lib/active_record/scoping/named.rb +78 -103
- data/lib/active_record/scoping.rb +54 -124
- data/lib/active_record/serialization.rb +6 -2
- data/lib/active_record/serializers/xml_serializer.rb +9 -15
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +131 -15
- data/lib/active_record/tasks/database_tasks.rb +204 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +144 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +67 -38
- data/lib/active_record/timestamp.rb +16 -11
- data/lib/active_record/transactions.rb +73 -51
- data/lib/active_record/validations/associated.rb +19 -13
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +110 -57
- data/lib/active_record/validations.rb +18 -17
- data/lib/active_record/version.rb +7 -6
- data/lib/active_record.rb +63 -45
- data/lib/rails/generators/active_record/migration/migration_generator.rb +45 -8
- data/lib/rails/generators/active_record/{model/templates/migration.rb → migration/templates/create_table_migration.rb} +4 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +20 -15
- data/lib/rails/generators/active_record/model/model_generator.rb +5 -4
- data/lib/rails/generators/active_record/model/templates/model.rb +4 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- data/lib/rails/generators/active_record.rb +3 -5
- metadata +43 -29
- data/examples/associations.png +0 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/session_store.rb +0 -360
- data/lib/rails/generators/active_record/migration.rb +0 -15
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'active_support/core_ext/hash/except'
|
2
2
|
require 'active_support/core_ext/object/try'
|
3
|
-
require 'active_support/core_ext/object/blank'
|
4
3
|
require 'active_support/core_ext/hash/indifferent_access'
|
5
|
-
require 'active_support/core_ext/class/attribute'
|
6
4
|
|
7
5
|
module ActiveRecord
|
8
6
|
module NestedAttributes #:nodoc:
|
@@ -12,17 +10,17 @@ module ActiveRecord
|
|
12
10
|
extend ActiveSupport::Concern
|
13
11
|
|
14
12
|
included do
|
15
|
-
class_attribute :nested_attributes_options, :
|
13
|
+
class_attribute :nested_attributes_options, instance_writer: false
|
16
14
|
self.nested_attributes_options = {}
|
17
15
|
end
|
18
16
|
|
19
17
|
# = Active Record Nested Attributes
|
20
18
|
#
|
21
19
|
# Nested attributes allow you to save attributes on associated records
|
22
|
-
# through the parent. By default nested attribute updating is turned off
|
23
|
-
# you can enable it using the accepts_nested_attributes_for class
|
24
|
-
# When you enable nested attributes an attribute writer is
|
25
|
-
# the model.
|
20
|
+
# through the parent. By default nested attribute updating is turned off
|
21
|
+
# and you can enable it using the accepts_nested_attributes_for class
|
22
|
+
# method. When you enable nested attributes an attribute writer is
|
23
|
+
# defined on the model.
|
26
24
|
#
|
27
25
|
# The attribute writer is named after the association, which means that
|
28
26
|
# in the following example, two new methods are added to your model:
|
@@ -52,15 +50,15 @@ module ActiveRecord
|
|
52
50
|
# Enabling nested attributes on a one-to-one association allows you to
|
53
51
|
# create the member and avatar in one go:
|
54
52
|
#
|
55
|
-
# params = { :
|
53
|
+
# params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } }
|
56
54
|
# member = Member.create(params[:member])
|
57
55
|
# member.avatar.id # => 2
|
58
56
|
# member.avatar.icon # => 'smiling'
|
59
57
|
#
|
60
58
|
# It also allows you to update the avatar through the member:
|
61
59
|
#
|
62
|
-
# params = { :
|
63
|
-
# member.
|
60
|
+
# params = { member: { avatar_attributes: { id: '2', icon: 'sad' } } }
|
61
|
+
# member.update params[:member]
|
64
62
|
# member.avatar.icon # => 'sad'
|
65
63
|
#
|
66
64
|
# By default you will only be able to set and update attributes on the
|
@@ -70,13 +68,13 @@ module ActiveRecord
|
|
70
68
|
#
|
71
69
|
# class Member < ActiveRecord::Base
|
72
70
|
# has_one :avatar
|
73
|
-
# accepts_nested_attributes_for :avatar, :
|
71
|
+
# accepts_nested_attributes_for :avatar, allow_destroy: true
|
74
72
|
# end
|
75
73
|
#
|
76
74
|
# Now, when you add the <tt>_destroy</tt> key to the attributes hash, with a
|
77
75
|
# value that evaluates to +true+, you will destroy the associated model:
|
78
76
|
#
|
79
|
-
# member.avatar_attributes = { :
|
77
|
+
# member.avatar_attributes = { id: '2', _destroy: '1' }
|
80
78
|
# member.avatar.marked_for_destruction? # => true
|
81
79
|
# member.save
|
82
80
|
# member.reload.avatar # => nil
|
@@ -100,15 +98,15 @@ module ActiveRecord
|
|
100
98
|
# be instantiated, unless the hash also contains a <tt>_destroy</tt> key
|
101
99
|
# that evaluates to +true+.
|
102
100
|
#
|
103
|
-
# params = { :
|
104
|
-
# :
|
105
|
-
# { :
|
106
|
-
# { :
|
107
|
-
# { :
|
101
|
+
# params = { member: {
|
102
|
+
# name: 'joe', posts_attributes: [
|
103
|
+
# { title: 'Kari, the awesome Ruby documentation browser!' },
|
104
|
+
# { title: 'The egalitarian assumption of the modern citizen' },
|
105
|
+
# { title: '', _destroy: '1' } # this will be ignored
|
108
106
|
# ]
|
109
107
|
# }}
|
110
108
|
#
|
111
|
-
# member = Member.create(params[
|
109
|
+
# member = Member.create(params[:member])
|
112
110
|
# member.posts.length # => 2
|
113
111
|
# member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
|
114
112
|
# member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
|
@@ -119,18 +117,18 @@ module ActiveRecord
|
|
119
117
|
#
|
120
118
|
# class Member < ActiveRecord::Base
|
121
119
|
# has_many :posts
|
122
|
-
# accepts_nested_attributes_for :posts, :
|
120
|
+
# accepts_nested_attributes_for :posts, reject_if: proc { |attributes| attributes['title'].blank? }
|
123
121
|
# end
|
124
122
|
#
|
125
|
-
# params = { :
|
126
|
-
# :
|
127
|
-
# { :
|
128
|
-
# { :
|
129
|
-
# { :
|
123
|
+
# params = { member: {
|
124
|
+
# name: 'joe', posts_attributes: [
|
125
|
+
# { title: 'Kari, the awesome Ruby documentation browser!' },
|
126
|
+
# { title: 'The egalitarian assumption of the modern citizen' },
|
127
|
+
# { title: '' } # this will be ignored because of the :reject_if proc
|
130
128
|
# ]
|
131
129
|
# }}
|
132
130
|
#
|
133
|
-
# member = Member.create(params[
|
131
|
+
# member = Member.create(params[:member])
|
134
132
|
# member.posts.length # => 2
|
135
133
|
# member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
|
136
134
|
# member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
|
@@ -139,12 +137,12 @@ module ActiveRecord
|
|
139
137
|
#
|
140
138
|
# class Member < ActiveRecord::Base
|
141
139
|
# has_many :posts
|
142
|
-
# accepts_nested_attributes_for :posts, :
|
140
|
+
# accepts_nested_attributes_for :posts, reject_if: :new_record?
|
143
141
|
# end
|
144
142
|
#
|
145
143
|
# class Member < ActiveRecord::Base
|
146
144
|
# has_many :posts
|
147
|
-
# accepts_nested_attributes_for :posts, :
|
145
|
+
# accepts_nested_attributes_for :posts, reject_if: :reject_posts
|
148
146
|
#
|
149
147
|
# def reject_posts(attributed)
|
150
148
|
# attributed['title'].blank?
|
@@ -155,10 +153,10 @@ module ActiveRecord
|
|
155
153
|
# associated record, the matching record will be modified:
|
156
154
|
#
|
157
155
|
# member.attributes = {
|
158
|
-
# :
|
159
|
-
# :
|
160
|
-
# { :
|
161
|
-
# { :
|
156
|
+
# name: 'Joe',
|
157
|
+
# posts_attributes: [
|
158
|
+
# { id: 1, title: '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
|
159
|
+
# { id: 2, title: '[UPDATED] other post' }
|
162
160
|
# ]
|
163
161
|
# }
|
164
162
|
#
|
@@ -173,14 +171,14 @@ module ActiveRecord
|
|
173
171
|
#
|
174
172
|
# class Member < ActiveRecord::Base
|
175
173
|
# has_many :posts
|
176
|
-
# accepts_nested_attributes_for :posts, :
|
174
|
+
# accepts_nested_attributes_for :posts, allow_destroy: true
|
177
175
|
# end
|
178
176
|
#
|
179
|
-
# params = { :
|
180
|
-
# :
|
177
|
+
# params = { member: {
|
178
|
+
# posts_attributes: [{ id: '2', _destroy: '1' }]
|
181
179
|
# }}
|
182
180
|
#
|
183
|
-
# member.attributes = params[
|
181
|
+
# member.attributes = params[:member]
|
184
182
|
# member.posts.detect { |p| p.id == 2 }.marked_for_destruction? # => true
|
185
183
|
# member.posts.length # => 2
|
186
184
|
# member.save
|
@@ -189,15 +187,15 @@ module ActiveRecord
|
|
189
187
|
# Nested attributes for an associated collection can also be passed in
|
190
188
|
# the form of a hash of hashes instead of an array of hashes:
|
191
189
|
#
|
192
|
-
# Member.create(:
|
193
|
-
# :
|
194
|
-
#
|
190
|
+
# Member.create(name: 'joe',
|
191
|
+
# posts_attributes: { first: { title: 'Foo' },
|
192
|
+
# second: { title: 'Bar' } })
|
195
193
|
#
|
196
194
|
# has the same effect as
|
197
195
|
#
|
198
|
-
# Member.create(:
|
199
|
-
# :
|
200
|
-
#
|
196
|
+
# Member.create(name: 'joe',
|
197
|
+
# posts_attributes: [ { title: 'Foo' },
|
198
|
+
# { title: 'Bar' } ])
|
201
199
|
#
|
202
200
|
# The keys of the hash which is the value for +:posts_attributes+ are
|
203
201
|
# ignored in this case.
|
@@ -216,18 +214,6 @@ module ActiveRecord
|
|
216
214
|
# the parent model is saved. This happens inside the transaction initiated
|
217
215
|
# by the parents save method. See ActiveRecord::AutosaveAssociation.
|
218
216
|
#
|
219
|
-
# === Using with attr_accessible
|
220
|
-
#
|
221
|
-
# The use of <tt>attr_accessible</tt> can interfere with nested attributes
|
222
|
-
# if you're not careful. For example, if the <tt>Member</tt> model above
|
223
|
-
# was using <tt>attr_accessible</tt> like this:
|
224
|
-
#
|
225
|
-
# attr_accessible :name
|
226
|
-
#
|
227
|
-
# You would need to modify it to look like this:
|
228
|
-
#
|
229
|
-
# attr_accessible :name, :posts_attributes
|
230
|
-
#
|
231
217
|
# === Validating the presence of a parent model
|
232
218
|
#
|
233
219
|
# If you want to validate that a child record is associated with a parent
|
@@ -235,20 +221,35 @@ module ActiveRecord
|
|
235
221
|
# <tt>inverse_of</tt> as this example illustrates:
|
236
222
|
#
|
237
223
|
# class Member < ActiveRecord::Base
|
238
|
-
# has_many :posts, :
|
224
|
+
# has_many :posts, inverse_of: :member
|
239
225
|
# accepts_nested_attributes_for :posts
|
240
226
|
# end
|
241
227
|
#
|
242
228
|
# class Post < ActiveRecord::Base
|
243
|
-
# belongs_to :member, :
|
229
|
+
# belongs_to :member, inverse_of: :posts
|
244
230
|
# validates_presence_of :member
|
245
231
|
# end
|
232
|
+
#
|
233
|
+
# For one-to-one nested associations, if you build the new (in-memory)
|
234
|
+
# child object yourself before assignment, then this module will not
|
235
|
+
# overwrite it, e.g.:
|
236
|
+
#
|
237
|
+
# class Member < ActiveRecord::Base
|
238
|
+
# has_one :avatar
|
239
|
+
# accepts_nested_attributes_for :avatar
|
240
|
+
#
|
241
|
+
# def avatar
|
242
|
+
# super || build_avatar(width: 200)
|
243
|
+
# end
|
244
|
+
# end
|
245
|
+
#
|
246
|
+
# member = Member.new
|
247
|
+
# member.avatar_attributes = {icon: 'sad'}
|
248
|
+
# member.avatar.width # => 200
|
246
249
|
module ClassMethods
|
247
250
|
REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
|
248
251
|
|
249
|
-
# Defines an attributes writer for the specified association(s).
|
250
|
-
# are using <tt>attr_protected</tt> or <tt>attr_accessible</tt>, then you
|
251
|
-
# will need to add the attribute writer to the allowed list.
|
252
|
+
# Defines an attributes writer for the specified association(s).
|
252
253
|
#
|
253
254
|
# Supported options:
|
254
255
|
# [:allow_destroy]
|
@@ -267,23 +268,32 @@ module ActiveRecord
|
|
267
268
|
# any value for _destroy.
|
268
269
|
# [:limit]
|
269
270
|
# Allows you to specify the maximum number of the associated records that
|
270
|
-
# can be processed with the nested attributes.
|
271
|
+
# can be processed with the nested attributes. Limit also can be specified as a
|
272
|
+
# Proc or a Symbol pointing to a method that should return number. If the size of the
|
271
273
|
# nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords
|
272
274
|
# exception is raised. If omitted, any number associations can be processed.
|
273
275
|
# Note that the :limit option is only applicable to one-to-many associations.
|
274
276
|
# [:update_only]
|
275
|
-
#
|
276
|
-
#
|
277
|
-
#
|
278
|
-
#
|
277
|
+
# For a one-to-one association, this option allows you to specify how
|
278
|
+
# nested attributes are to be used when an associated record already
|
279
|
+
# exists. In general, an existing record may either be updated with the
|
280
|
+
# new set of attribute values or be replaced by a wholly new record
|
281
|
+
# containing those values. By default the :update_only option is +false+
|
282
|
+
# and the nested attributes are used to update the existing record only
|
283
|
+
# if they include the record's <tt>:id</tt> value. Otherwise a new
|
284
|
+
# record will be instantiated and used to replace the existing one.
|
285
|
+
# However if the :update_only option is +true+, the nested attributes
|
286
|
+
# are used to update the record's attributes always, regardless of
|
287
|
+
# whether the <tt>:id</tt> is present. The option is ignored for collection
|
288
|
+
# associations.
|
279
289
|
#
|
280
290
|
# Examples:
|
281
291
|
# # creates avatar_attributes=
|
282
|
-
# accepts_nested_attributes_for :avatar, :
|
292
|
+
# accepts_nested_attributes_for :avatar, reject_if: proc { |attributes| attributes['name'].blank? }
|
283
293
|
# # creates avatar_attributes=
|
284
|
-
# accepts_nested_attributes_for :avatar, :
|
294
|
+
# accepts_nested_attributes_for :avatar, reject_if: :all_blank
|
285
295
|
# # creates avatar_attributes= and posts_attributes=
|
286
|
-
# accepts_nested_attributes_for :avatar, :posts, :
|
296
|
+
# accepts_nested_attributes_for :avatar, :posts, allow_destroy: true
|
287
297
|
def accepts_nested_attributes_for(*attr_names)
|
288
298
|
options = { :allow_destroy => false, :update_only => false }
|
289
299
|
options.update(attr_names.extract_options!)
|
@@ -300,24 +310,36 @@ module ActiveRecord
|
|
300
310
|
self.nested_attributes_options = nested_attributes_options
|
301
311
|
|
302
312
|
type = (reflection.collection? ? :collection : :one_to_one)
|
303
|
-
|
304
|
-
# remove_possible_method :pirate_attributes=
|
305
|
-
#
|
306
|
-
# def pirate_attributes=(attributes)
|
307
|
-
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes, mass_assignment_options)
|
308
|
-
# end
|
309
|
-
class_eval <<-eoruby, __FILE__, __LINE__ + 1
|
310
|
-
remove_possible_method(:#{association_name}_attributes=)
|
311
|
-
|
312
|
-
def #{association_name}_attributes=(attributes)
|
313
|
-
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes, mass_assignment_options)
|
314
|
-
end
|
315
|
-
eoruby
|
313
|
+
generate_association_writer(association_name, type)
|
316
314
|
else
|
317
315
|
raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
|
318
316
|
end
|
319
317
|
end
|
320
318
|
end
|
319
|
+
|
320
|
+
private
|
321
|
+
|
322
|
+
# Generates a writer method for this association. Serves as a point for
|
323
|
+
# accessing the objects in the association. For example, this method
|
324
|
+
# could generate the following:
|
325
|
+
#
|
326
|
+
# def pirate_attributes=(attributes)
|
327
|
+
# assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
|
328
|
+
# end
|
329
|
+
#
|
330
|
+
# This redirects the attempts to write objects in an association through
|
331
|
+
# the helper methods defined below. Makes it seem like the nested
|
332
|
+
# associations are just regular associations.
|
333
|
+
def generate_association_writer(association_name, type)
|
334
|
+
generated_feature_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1
|
335
|
+
if method_defined?(:#{association_name}_attributes=)
|
336
|
+
remove_method(:#{association_name}_attributes=)
|
337
|
+
end
|
338
|
+
def #{association_name}_attributes=(attributes)
|
339
|
+
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
|
340
|
+
end
|
341
|
+
eoruby
|
342
|
+
end
|
321
343
|
end
|
322
344
|
|
323
345
|
# Returns ActiveRecord::AutosaveAssociation::marked_for_destruction? It's
|
@@ -337,31 +359,42 @@ module ActiveRecord
|
|
337
359
|
|
338
360
|
# Assigns the given attributes to the association.
|
339
361
|
#
|
340
|
-
# If
|
341
|
-
#
|
342
|
-
#
|
343
|
-
#
|
362
|
+
# If an associated record does not yet exist, one will be instantiated. If
|
363
|
+
# an associated record already exists, the method's behavior depends on
|
364
|
+
# the value of the update_only option. If update_only is +false+ and the
|
365
|
+
# given attributes include an <tt>:id</tt> that matches the existing record's
|
366
|
+
# id, then the existing record will be modified. If no <tt>:id</tt> is provided
|
367
|
+
# it will be replaced with a new record. If update_only is +true+ the existing
|
368
|
+
# record will be modified regardless of whether an <tt>:id</tt> is provided.
|
344
369
|
#
|
345
370
|
# If the given attributes include a matching <tt>:id</tt> attribute, or
|
346
371
|
# update_only is true, and a <tt>:_destroy</tt> key set to a truthy value,
|
347
372
|
# then the existing record will be marked for destruction.
|
348
|
-
def assign_nested_attributes_for_one_to_one_association(association_name, attributes
|
373
|
+
def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
|
349
374
|
options = self.nested_attributes_options[association_name]
|
350
375
|
attributes = attributes.with_indifferent_access
|
376
|
+
existing_record = send(association_name)
|
351
377
|
|
352
|
-
if (options[:update_only] || !attributes['id'].blank?) &&
|
353
|
-
(options[:update_only] ||
|
354
|
-
assign_to_or_mark_for_destruction(
|
378
|
+
if (options[:update_only] || !attributes['id'].blank?) && existing_record &&
|
379
|
+
(options[:update_only] || existing_record.id.to_s == attributes['id'].to_s)
|
380
|
+
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)
|
355
381
|
|
356
|
-
elsif attributes['id'].present?
|
357
|
-
raise_nested_attributes_record_not_found(association_name, attributes['id'])
|
382
|
+
elsif attributes['id'].present?
|
383
|
+
raise_nested_attributes_record_not_found!(association_name, attributes['id'])
|
358
384
|
|
359
385
|
elsif !reject_new_record?(association_name, attributes)
|
360
|
-
|
361
|
-
|
362
|
-
|
386
|
+
assignable_attributes = attributes.except(*UNASSIGNABLE_KEYS)
|
387
|
+
|
388
|
+
if existing_record && existing_record.new_record?
|
389
|
+
existing_record.assign_attributes(assignable_attributes)
|
390
|
+
association(association_name).initialize_attributes(existing_record)
|
363
391
|
else
|
364
|
-
|
392
|
+
method = "build_#{association_name}"
|
393
|
+
if respond_to?(method)
|
394
|
+
send(method, assignable_attributes)
|
395
|
+
else
|
396
|
+
raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?"
|
397
|
+
end
|
365
398
|
end
|
366
399
|
end
|
367
400
|
end
|
@@ -377,37 +410,35 @@ module ActiveRecord
|
|
377
410
|
# For example:
|
378
411
|
#
|
379
412
|
# assign_nested_attributes_for_collection_association(:people, {
|
380
|
-
# '1' => { :
|
381
|
-
# '2' => { :
|
382
|
-
# '3' => { :
|
413
|
+
# '1' => { id: '1', name: 'Peter' },
|
414
|
+
# '2' => { name: 'John' },
|
415
|
+
# '3' => { id: '2', _destroy: true }
|
383
416
|
# })
|
384
417
|
#
|
385
418
|
# Will update the name of the Person with ID 1, build a new associated
|
386
|
-
# person with the name
|
419
|
+
# person with the name 'John', and mark the associated Person with ID 2
|
387
420
|
# for destruction.
|
388
421
|
#
|
389
422
|
# Also accepts an Array of attribute hashes:
|
390
423
|
#
|
391
424
|
# assign_nested_attributes_for_collection_association(:people, [
|
392
|
-
# { :
|
393
|
-
# { :
|
394
|
-
# { :
|
425
|
+
# { id: '1', name: 'Peter' },
|
426
|
+
# { name: 'John' },
|
427
|
+
# { id: '2', _destroy: true }
|
395
428
|
# ])
|
396
|
-
def assign_nested_attributes_for_collection_association(association_name, attributes_collection
|
429
|
+
def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
|
397
430
|
options = self.nested_attributes_options[association_name]
|
398
431
|
|
399
432
|
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
|
400
433
|
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
|
401
434
|
end
|
402
435
|
|
403
|
-
|
404
|
-
raise TooManyRecords, "Maximum #{options[:limit]} records are allowed. Got #{attributes_collection.size} records instead."
|
405
|
-
end
|
436
|
+
check_record_limit!(options[:limit], attributes_collection)
|
406
437
|
|
407
438
|
if attributes_collection.is_a? Hash
|
408
439
|
keys = attributes_collection.keys
|
409
440
|
attributes_collection = if keys.include?('id') || keys.include?(:id)
|
410
|
-
|
441
|
+
[attributes_collection]
|
411
442
|
else
|
412
443
|
attributes_collection.values
|
413
444
|
end
|
@@ -419,7 +450,7 @@ module ActiveRecord
|
|
419
450
|
association.target
|
420
451
|
else
|
421
452
|
attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
|
422
|
-
attribute_ids.empty? ? [] : association.
|
453
|
+
attribute_ids.empty? ? [] : association.scope.where(association.klass.primary_key => attribute_ids)
|
423
454
|
end
|
424
455
|
|
425
456
|
attributes_collection.each do |attributes|
|
@@ -427,7 +458,7 @@ module ActiveRecord
|
|
427
458
|
|
428
459
|
if attributes['id'].blank?
|
429
460
|
unless reject_new_record?(association_name, attributes)
|
430
|
-
association.build(attributes.except(*
|
461
|
+
association.build(attributes.except(*UNASSIGNABLE_KEYS))
|
431
462
|
end
|
432
463
|
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
|
433
464
|
unless association.loaded? || call_reject_if(association_name, attributes)
|
@@ -440,24 +471,44 @@ module ActiveRecord
|
|
440
471
|
else
|
441
472
|
association.add_to_target(existing_record)
|
442
473
|
end
|
443
|
-
|
444
474
|
end
|
445
475
|
|
446
476
|
if !call_reject_if(association_name, attributes)
|
447
|
-
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]
|
477
|
+
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
|
448
478
|
end
|
449
|
-
elsif assignment_opts[:without_protection]
|
450
|
-
association.build(attributes.except(*unassignable_keys(assignment_opts)), assignment_opts)
|
451
479
|
else
|
452
|
-
raise_nested_attributes_record_not_found(association_name, attributes['id'])
|
480
|
+
raise_nested_attributes_record_not_found!(association_name, attributes['id'])
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
# Takes in a limit and checks if the attributes_collection has too many
|
486
|
+
# records. The method will take limits in the form of symbols, procs, and
|
487
|
+
# number-like objects (anything that can be compared with an integer).
|
488
|
+
#
|
489
|
+
# Will raise an TooManyRecords error if the attributes_collection is
|
490
|
+
# larger than the limit.
|
491
|
+
def check_record_limit!(limit, attributes_collection)
|
492
|
+
if limit
|
493
|
+
limit = case limit
|
494
|
+
when Symbol
|
495
|
+
send(limit)
|
496
|
+
when Proc
|
497
|
+
limit.call
|
498
|
+
else
|
499
|
+
limit
|
500
|
+
end
|
501
|
+
|
502
|
+
if limit && attributes_collection.size > limit
|
503
|
+
raise TooManyRecords, "Maximum #{limit} records are allowed. Got #{attributes_collection.size} records instead."
|
453
504
|
end
|
454
505
|
end
|
455
506
|
end
|
456
507
|
|
457
508
|
# Updates a record with the +attributes+ or marks it for destruction if
|
458
509
|
# +allow_destroy+ is +true+ and has_destroy_flag? returns +true+.
|
459
|
-
def assign_to_or_mark_for_destruction(record, attributes, allow_destroy
|
460
|
-
record.assign_attributes(attributes.except(*
|
510
|
+
def assign_to_or_mark_for_destruction(record, attributes, allow_destroy)
|
511
|
+
record.assign_attributes(attributes.except(*UNASSIGNABLE_KEYS))
|
461
512
|
record.mark_for_destruction if has_destroy_flag?(attributes) && allow_destroy
|
462
513
|
end
|
463
514
|
|
@@ -470,12 +521,16 @@ module ActiveRecord
|
|
470
521
|
# has_destroy_flag? or if a <tt>:reject_if</tt> proc exists for this
|
471
522
|
# association and evaluates to +true+.
|
472
523
|
def reject_new_record?(association_name, attributes)
|
473
|
-
|
524
|
+
has_destroy_flag?(attributes) || call_reject_if(association_name, attributes)
|
474
525
|
end
|
475
526
|
|
527
|
+
# Determines if a record with the particular +attributes+ should be
|
528
|
+
# rejected by calling the reject_if Symbol or Proc (if defined).
|
529
|
+
# The reject_if option is defined by +accepts_nested_attributes_for+.
|
530
|
+
#
|
531
|
+
# Returns false if there is a +destroy_flag+ on the attributes.
|
476
532
|
def call_reject_if(association_name, attributes)
|
477
|
-
return false if
|
478
|
-
|
533
|
+
return false if has_destroy_flag?(attributes)
|
479
534
|
case callback = self.nested_attributes_options[association_name][:reject_if]
|
480
535
|
when Symbol
|
481
536
|
method(callback).arity == 0 ? send(callback) : send(callback, attributes)
|
@@ -484,21 +539,8 @@ module ActiveRecord
|
|
484
539
|
end
|
485
540
|
end
|
486
541
|
|
487
|
-
|
488
|
-
def will_be_destroyed?(association_name, attributes)
|
489
|
-
allow_destroy?(association_name) && has_destroy_flag?(attributes)
|
490
|
-
end
|
491
|
-
|
492
|
-
def allow_destroy?(association_name)
|
493
|
-
self.nested_attributes_options[association_name][:allow_destroy]
|
494
|
-
end
|
495
|
-
|
496
|
-
def raise_nested_attributes_record_not_found(association_name, record_id)
|
542
|
+
def raise_nested_attributes_record_not_found!(association_name, record_id)
|
497
543
|
raise RecordNotFound, "Couldn't find #{self.class.reflect_on_association(association_name).klass.name} with ID=#{record_id} for #{self.class.name} with ID=#{id}"
|
498
544
|
end
|
499
|
-
|
500
|
-
def unassignable_keys(assignment_opts)
|
501
|
-
assignment_opts[:without_protection] ? UNASSIGNABLE_KEYS - %w[id] : UNASSIGNABLE_KEYS
|
502
|
-
end
|
503
545
|
end
|
504
546
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module NullRelation # :nodoc:
|
5
|
+
def exec_queries
|
6
|
+
@records = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def pluck(*column_names)
|
10
|
+
[]
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete_all(_conditions = nil)
|
14
|
+
0
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_all(_updates, _conditions = nil, _options = {})
|
18
|
+
0
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete(_id_or_array)
|
22
|
+
0
|
23
|
+
end
|
24
|
+
|
25
|
+
def size
|
26
|
+
0
|
27
|
+
end
|
28
|
+
|
29
|
+
def empty?
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def any?
|
34
|
+
false
|
35
|
+
end
|
36
|
+
|
37
|
+
def many?
|
38
|
+
false
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_sql
|
42
|
+
@to_sql ||= ""
|
43
|
+
end
|
44
|
+
|
45
|
+
def count(*)
|
46
|
+
calculate :count, nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def sum(*)
|
50
|
+
0
|
51
|
+
end
|
52
|
+
|
53
|
+
def calculate(operation, _column_name, _options = {})
|
54
|
+
# TODO: Remove _options argument as soon we remove support to
|
55
|
+
# activerecord-deprecated_finders.
|
56
|
+
if operation == :count
|
57
|
+
group_values.any? ? Hash.new : 0
|
58
|
+
else
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def exists?(_id = false)
|
64
|
+
false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|