activerecord 3.2.22.5 → 4.0.0.beta1
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 +1024 -543
- data/MIT-LICENSE +1 -1
- data/README.rdoc +20 -29
- data/examples/performance.rb +1 -1
- data/lib/active_record.rb +55 -44
- data/lib/active_record/aggregations.rb +40 -34
- data/lib/active_record/associations.rb +204 -276
- data/lib/active_record/associations/alias_tracker.rb +1 -1
- data/lib/active_record/associations/association.rb +30 -35
- data/lib/active_record/associations/association_scope.rb +40 -40
- data/lib/active_record/associations/belongs_to_association.rb +15 -2
- data/lib/active_record/associations/builder/association.rb +81 -28
- data/lib/active_record/associations/builder/belongs_to.rb +35 -57
- 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 +92 -88
- data/lib/active_record/associations/collection_proxy.rb +913 -63
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +12 -10
- data/lib/active_record/associations/has_many_association.rb +35 -9
- data/lib/active_record/associations/has_many_through_association.rb +24 -14
- data/lib/active_record/associations/has_one_association.rb +33 -13
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +2 -2
- data/lib/active_record/associations/join_dependency/join_association.rb +17 -22
- data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
- data/lib/active_record/associations/join_helper.rb +1 -11
- data/lib/active_record/associations/preloader.rb +14 -17
- 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 +1 -1
- data/lib/active_record/associations/preloader/has_many_through.rb +1 -1
- 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/singular_association.rb +11 -11
- data/lib/active_record/associations/through_association.rb +2 -2
- data/lib/active_record/attribute_assignment.rb +133 -153
- data/lib/active_record/attribute_methods.rb +196 -93
- data/lib/active_record/attribute_methods/before_type_cast.rb +44 -5
- data/lib/active_record/attribute_methods/dirty.rb +31 -28
- data/lib/active_record/attribute_methods/primary_key.rb +38 -30
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +62 -91
- data/lib/active_record/attribute_methods/serialization.rb +97 -66
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -45
- data/lib/active_record/attribute_methods/write.rb +32 -39
- data/lib/active_record/autosave_association.rb +56 -70
- data/lib/active_record/base.rb +53 -450
- data/lib/active_record/callbacks.rb +53 -18
- data/lib/active_record/coders/yaml_column.rb +11 -9
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +353 -197
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +130 -131
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -19
- data/lib/active_record/connection_adapters/abstract/quoting.rb +23 -3
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +101 -91
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +59 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +225 -96
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +99 -46
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +114 -36
- data/lib/active_record/connection_adapters/column.rb +46 -24
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +16 -32
- data/lib/active_record/connection_adapters/mysql_adapter.rb +181 -64
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +132 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +347 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +158 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +448 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +454 -885
- data/lib/active_record/connection_adapters/schema_cache.rb +48 -16
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +574 -13
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +428 -0
- data/lib/active_record/counter_cache.rb +106 -108
- data/lib/active_record/dynamic_matchers.rb +110 -63
- data/lib/active_record/errors.rb +25 -8
- data/lib/active_record/explain.rb +8 -58
- data/lib/active_record/explain_subscriber.rb +6 -3
- data/lib/active_record/fixture_set/file.rb +56 -0
- data/lib/active_record/fixtures.rb +146 -148
- data/lib/active_record/inheritance.rb +77 -59
- data/lib/active_record/integration.rb +5 -5
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +38 -42
- data/lib/active_record/locking/pessimistic.rb +4 -4
- data/lib/active_record/log_subscriber.rb +19 -9
- data/lib/active_record/migration.rb +318 -153
- data/lib/active_record/migration/command_recorder.rb +90 -31
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/model_schema.rb +69 -92
- data/lib/active_record/nested_attributes.rb +113 -148
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +188 -97
- data/lib/active_record/query_cache.rb +18 -36
- data/lib/active_record/querying.rb +19 -15
- data/lib/active_record/railtie.rb +91 -36
- data/lib/active_record/railties/console_sandbox.rb +0 -2
- data/lib/active_record/railties/controller_runtime.rb +2 -2
- data/lib/active_record/railties/databases.rake +90 -309
- 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 +72 -56
- data/lib/active_record/relation.rb +241 -157
- data/lib/active_record/relation/batches.rb +25 -22
- data/lib/active_record/relation/calculations.rb +143 -121
- data/lib/active_record/relation/delegation.rb +96 -18
- data/lib/active_record/relation/finder_methods.rb +117 -183
- data/lib/active_record/relation/merger.rb +133 -0
- data/lib/active_record/relation/predicate_builder.rb +90 -42
- data/lib/active_record/relation/query_methods.rb +666 -136
- data/lib/active_record/relation/spawn_methods.rb +43 -150
- data/lib/active_record/result.rb +33 -6
- data/lib/active_record/sanitization.rb +24 -50
- data/lib/active_record/schema.rb +19 -12
- data/lib/active_record/schema_dumper.rb +31 -39
- data/lib/active_record/schema_migration.rb +36 -0
- data/lib/active_record/scoping.rb +0 -124
- data/lib/active_record/scoping/default.rb +48 -45
- data/lib/active_record/scoping/named.rb +74 -103
- data/lib/active_record/serialization.rb +6 -2
- data/lib/active_record/serializers/xml_serializer.rb +9 -15
- data/lib/active_record/store.rb +119 -15
- data/lib/active_record/tasks/database_tasks.rb +158 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +138 -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/test_case.rb +61 -38
- data/lib/active_record/timestamp.rb +8 -9
- data/lib/active_record/transactions.rb +65 -51
- data/lib/active_record/validations.rb +17 -15
- data/lib/active_record/validations/associated.rb +20 -14
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +93 -52
- data/lib/active_record/version.rb +4 -4
- data/lib/rails/generators/active_record.rb +3 -5
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -7
- data/lib/rails/generators/active_record/migration/templates/migration.rb +20 -15
- data/lib/rails/generators/active_record/model/model_generator.rb +4 -3
- data/lib/rails/generators/active_record/model/templates/model.rb +1 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- metadata +53 -46
- 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
data/lib/active_record/base.rb
CHANGED
@@ -1,30 +1,19 @@
|
|
1
|
-
begin
|
2
|
-
require 'psych'
|
3
|
-
rescue LoadError
|
4
|
-
end
|
5
|
-
|
6
1
|
require 'yaml'
|
7
2
|
require 'set'
|
8
|
-
require 'thread'
|
9
3
|
require 'active_support/benchmarkable'
|
10
4
|
require 'active_support/dependencies'
|
11
5
|
require 'active_support/descendants_tracker'
|
12
6
|
require 'active_support/time'
|
13
|
-
require 'active_support/core_ext/class/attribute'
|
14
7
|
require 'active_support/core_ext/class/attribute_accessors'
|
15
8
|
require 'active_support/core_ext/class/delegating_attributes'
|
16
|
-
require 'active_support/core_ext/class/attribute'
|
17
9
|
require 'active_support/core_ext/array/extract_options'
|
18
10
|
require 'active_support/core_ext/hash/deep_merge'
|
19
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
20
11
|
require 'active_support/core_ext/hash/slice'
|
21
12
|
require 'active_support/core_ext/string/behavior'
|
22
13
|
require 'active_support/core_ext/kernel/singleton_class'
|
23
|
-
require 'active_support/core_ext/module/delegation'
|
24
14
|
require 'active_support/core_ext/module/introspection'
|
25
15
|
require 'active_support/core_ext/object/duplicable'
|
26
|
-
require 'active_support/core_ext/
|
27
|
-
require 'active_support/deprecation'
|
16
|
+
require 'active_support/core_ext/class/subclasses'
|
28
17
|
require 'arel'
|
29
18
|
require 'active_record/errors'
|
30
19
|
require 'active_record/log_subscriber'
|
@@ -47,7 +36,7 @@ module ActiveRecord #:nodoc:
|
|
47
36
|
# method is especially useful when you're receiving the data from somewhere else, like an
|
48
37
|
# HTTP request. It works like this:
|
49
38
|
#
|
50
|
-
# user = User.new(:
|
39
|
+
# user = User.new(name: "David", occupation: "Code Artist")
|
51
40
|
# user.name # => "David"
|
52
41
|
#
|
53
42
|
# You can also use block initialization:
|
@@ -80,7 +69,7 @@ module ActiveRecord #:nodoc:
|
|
80
69
|
# end
|
81
70
|
#
|
82
71
|
# def self.authenticate_safely_simply(user_name, password)
|
83
|
-
# where(:
|
72
|
+
# where(user_name: user_name, password: password).first
|
84
73
|
# end
|
85
74
|
# end
|
86
75
|
#
|
@@ -98,27 +87,27 @@ module ActiveRecord #:nodoc:
|
|
98
87
|
#
|
99
88
|
# Company.where(
|
100
89
|
# "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
|
101
|
-
# { :
|
90
|
+
# { id: 3, name: "37signals", division: "First", accounting_date: '2005-01-01' }
|
102
91
|
# ).first
|
103
92
|
#
|
104
93
|
# Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
|
105
94
|
# operator. For instance:
|
106
95
|
#
|
107
|
-
# Student.where(:
|
96
|
+
# Student.where(first_name: "Harvey", status: 1)
|
108
97
|
# Student.where(params[:student])
|
109
98
|
#
|
110
99
|
# A range may be used in the hash to use the SQL BETWEEN operator:
|
111
100
|
#
|
112
|
-
# Student.where(:
|
101
|
+
# Student.where(grade: 9..12)
|
113
102
|
#
|
114
103
|
# An array may be used in the hash to use the SQL IN operator:
|
115
104
|
#
|
116
|
-
# Student.where(:
|
105
|
+
# Student.where(grade: [9,11,12])
|
117
106
|
#
|
118
107
|
# When joining tables, nested hashes or keys written in the form 'table_name.column_name'
|
119
108
|
# can be used to qualify the table name of a particular condition. For instance:
|
120
109
|
#
|
121
|
-
# Student.joins(:schools).where(:
|
110
|
+
# Student.joins(:schools).where(schools: { category: 'public' })
|
122
111
|
# Student.joins(:schools).where('schools.category' => 'public' )
|
123
112
|
#
|
124
113
|
# == Overwriting default accessors
|
@@ -152,10 +141,10 @@ module ActiveRecord #:nodoc:
|
|
152
141
|
# For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
|
153
142
|
# to determine whether the user has a name:
|
154
143
|
#
|
155
|
-
# user = User.new(:
|
144
|
+
# user = User.new(name: "David")
|
156
145
|
# user.name? # => true
|
157
146
|
#
|
158
|
-
# anonymous = User.new(:
|
147
|
+
# anonymous = User.new(name: "")
|
159
148
|
# anonymous.name? # => false
|
160
149
|
#
|
161
150
|
# == Accessing attributes before they have been typecasted
|
@@ -173,12 +162,9 @@ module ActiveRecord #:nodoc:
|
|
173
162
|
#
|
174
163
|
# Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects
|
175
164
|
# by simple queries without turning to SQL. They work by appending the name of an attribute
|
176
|
-
# to <tt>find_by_</tt
|
177
|
-
#
|
178
|
-
# <tt>
|
179
|
-
# <tt>Person.where(:user_name => user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
180
|
-
# And instead of writing <tt>Person.where(:last_name => last_name).all</tt>, you just do
|
181
|
-
# <tt>Person.find_all_by_last_name(last_name)</tt>.
|
165
|
+
# to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
|
166
|
+
# Instead of writing <tt>Person.where(user_name: user_name).first</tt>, you just do
|
167
|
+
# <tt>Person.find_by_user_name(user_name)</tt>.
|
182
168
|
#
|
183
169
|
# It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
|
184
170
|
# <tt>ActiveRecord::RecordNotFound</tt> error if they do not return any records,
|
@@ -186,51 +172,12 @@ module ActiveRecord #:nodoc:
|
|
186
172
|
#
|
187
173
|
# It's also possible to use multiple attributes in the same find by separating them with "_and_".
|
188
174
|
#
|
189
|
-
# Person.where(:
|
175
|
+
# Person.where(user_name: user_name, password: password).first
|
190
176
|
# Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
|
191
177
|
#
|
192
178
|
# It's even possible to call these dynamic finder methods on relations and named scopes.
|
193
179
|
#
|
194
|
-
# Payment.order("created_on").
|
195
|
-
# Payment.pending.find_last_by_amount(100)
|
196
|
-
#
|
197
|
-
# The same dynamic finder style can be used to create the object if it doesn't already exist.
|
198
|
-
# This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if
|
199
|
-
# it already exists and otherwise creates it, then returns it. Protected attributes won't be set
|
200
|
-
# unless they are given in a block.
|
201
|
-
#
|
202
|
-
# # No 'Summer' tag exists
|
203
|
-
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
|
204
|
-
#
|
205
|
-
# # Now the 'Summer' tag does exist
|
206
|
-
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
|
207
|
-
#
|
208
|
-
# # Now 'Bob' exist and is an 'admin'
|
209
|
-
# User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }
|
210
|
-
#
|
211
|
-
# Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will
|
212
|
-
# raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid.
|
213
|
-
#
|
214
|
-
# Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without
|
215
|
-
# saving it first. Protected attributes won't be set unless they are given in a block.
|
216
|
-
#
|
217
|
-
# # No 'Winter' tag exists
|
218
|
-
# winter = Tag.find_or_initialize_by_name("Winter")
|
219
|
-
# winter.persisted? # false
|
220
|
-
#
|
221
|
-
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
|
222
|
-
# a list of parameters.
|
223
|
-
#
|
224
|
-
# Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
|
225
|
-
#
|
226
|
-
# That will either find an existing tag named "rails", or create a new one while setting the
|
227
|
-
# user that created it.
|
228
|
-
#
|
229
|
-
# Just like <tt>find_by_*</tt>, you can also use <tt>scoped_by_*</tt> to retrieve data. The good thing about
|
230
|
-
# using this feature is that the very first time result is returned using <tt>method_missing</tt> technique
|
231
|
-
# but after that the method is declared on the class. Henceforth <tt>method_missing</tt> will not be hit.
|
232
|
-
#
|
233
|
-
# User.scoped_by_user_name('David')
|
180
|
+
# Payment.order("created_on").find_by_amount(50)
|
234
181
|
#
|
235
182
|
# == Saving arrays, hashes, and other non-mappable objects in text columns
|
236
183
|
#
|
@@ -243,7 +190,7 @@ module ActiveRecord #:nodoc:
|
|
243
190
|
# serialize :preferences
|
244
191
|
# end
|
245
192
|
#
|
246
|
-
# user = User.create(:
|
193
|
+
# user = User.create(preferences: { "background" => "black", "display" => large })
|
247
194
|
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
|
248
195
|
#
|
249
196
|
# You can also specify a class option as the second parameter that'll raise an exception
|
@@ -253,7 +200,7 @@ module ActiveRecord #:nodoc:
|
|
253
200
|
# serialize :preferences, Hash
|
254
201
|
# end
|
255
202
|
#
|
256
|
-
# user = User.create(:
|
203
|
+
# user = User.create(preferences: %w( one two three ))
|
257
204
|
# User.find(user.id).preferences # raises SerializationTypeMismatch
|
258
205
|
#
|
259
206
|
# When you specify a class option, the default value for that attribute will be a new
|
@@ -278,9 +225,9 @@ module ActiveRecord #:nodoc:
|
|
278
225
|
# class Client < Company; end
|
279
226
|
# class PriorityClient < Client; end
|
280
227
|
#
|
281
|
-
# When you do <tt>Firm.create(:
|
228
|
+
# When you do <tt>Firm.create(name: "37signals")</tt>, this record will be saved in
|
282
229
|
# the companies table with type = "Firm". You can then fetch this row again using
|
283
|
-
# <tt>Company.where(:
|
230
|
+
# <tt>Company.where(name: '37signals').first</tt> and it will return a Firm object.
|
284
231
|
#
|
285
232
|
# If you don't have a type column defined in your table, single-table inheritance won't
|
286
233
|
# be triggered. In that case, it'll work just like normal subclasses with no special magic
|
@@ -311,410 +258,66 @@ module ActiveRecord #:nodoc:
|
|
311
258
|
# (or a bad spelling of an existing one).
|
312
259
|
# * AssociationTypeMismatch - The object assigned to the association wasn't of the type
|
313
260
|
# specified in the association definition.
|
314
|
-
# *
|
315
|
-
#
|
261
|
+
# * AttributeAssignmentError - An error occurred while doing a mass assignment through the
|
262
|
+
# <tt>attributes=</tt> method.
|
263
|
+
# You can inspect the +attribute+ property of the exception object to determine which attribute
|
264
|
+
# triggered the error.
|
265
|
+
# * ConnectionNotEstablished - No connection has been established. Use <tt>establish_connection</tt>
|
316
266
|
# before querying.
|
317
|
-
# * RecordNotFound - No record responded to the +find+ method. Either the row with the given ID doesn't exist
|
318
|
-
# or the row didn't meet the additional restrictions. Some +find+ calls do not raise this exception to signal
|
319
|
-
# nothing was found, please check its documentation for further details.
|
320
|
-
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
321
267
|
# * MultiparameterAssignmentErrors - Collection of errors that occurred during a mass assignment using the
|
322
268
|
# <tt>attributes=</tt> method. The +errors+ property of this exception contains an array of
|
323
269
|
# AttributeAssignmentError
|
324
270
|
# objects that should be inspected to determine which attributes triggered the errors.
|
325
|
-
# *
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
271
|
+
# * RecordInvalid - raised by save! and create! when the record is invalid.
|
272
|
+
# * RecordNotFound - No record responded to the +find+ method. Either the row with the given ID doesn't exist
|
273
|
+
# or the row didn't meet the additional restrictions. Some +find+ calls do not raise this exception to signal
|
274
|
+
# nothing was found, please check its documentation for further details.
|
275
|
+
# * SerializationTypeMismatch - The serialized object wasn't of the class specified as the second parameter.
|
276
|
+
# * StatementInvalid - The database server rejected the SQL statement. The precise error is added in the message.
|
329
277
|
#
|
330
278
|
# *Note*: The attributes listed are class-level attributes (accessible from both the class and instance level).
|
331
279
|
# So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
|
332
280
|
# instances in the current object space.
|
333
281
|
class Base
|
334
|
-
##
|
335
|
-
# :singleton-method:
|
336
|
-
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class,
|
337
|
-
# which is then passed on to any new database connections made and which can be retrieved on both
|
338
|
-
# a class and instance level by calling +logger+.
|
339
|
-
cattr_accessor :logger, :instance_writer => false
|
340
|
-
|
341
|
-
##
|
342
|
-
# :singleton-method:
|
343
|
-
# Contains the database configuration - as is typically stored in config/database.yml -
|
344
|
-
# as a Hash.
|
345
|
-
#
|
346
|
-
# For example, the following database.yml...
|
347
|
-
#
|
348
|
-
# development:
|
349
|
-
# adapter: sqlite3
|
350
|
-
# database: db/development.sqlite3
|
351
|
-
#
|
352
|
-
# production:
|
353
|
-
# adapter: sqlite3
|
354
|
-
# database: db/production.sqlite3
|
355
|
-
#
|
356
|
-
# ...would result in ActiveRecord::Base.configurations to look like this:
|
357
|
-
#
|
358
|
-
# {
|
359
|
-
# 'development' => {
|
360
|
-
# 'adapter' => 'sqlite3',
|
361
|
-
# 'database' => 'db/development.sqlite3'
|
362
|
-
# },
|
363
|
-
# 'production' => {
|
364
|
-
# 'adapter' => 'sqlite3',
|
365
|
-
# 'database' => 'db/production.sqlite3'
|
366
|
-
# }
|
367
|
-
# }
|
368
|
-
cattr_accessor :configurations, :instance_writer => false
|
369
|
-
@@configurations = {}
|
370
|
-
|
371
|
-
##
|
372
|
-
# :singleton-method:
|
373
|
-
# Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling
|
374
|
-
# dates and times from the database. This is set to :local by default.
|
375
|
-
cattr_accessor :default_timezone, :instance_writer => false
|
376
|
-
@@default_timezone = :local
|
377
|
-
|
378
|
-
##
|
379
|
-
# :singleton-method:
|
380
|
-
# Specifies the format to use when dumping the database schema with Rails'
|
381
|
-
# Rakefile. If :sql, the schema is dumped as (potentially database-
|
382
|
-
# specific) SQL statements. If :ruby, the schema is dumped as an
|
383
|
-
# ActiveRecord::Schema file which can be loaded into any database that
|
384
|
-
# supports migrations. Use :ruby if you want to have different database
|
385
|
-
# adapters for, e.g., your development and test environments.
|
386
|
-
cattr_accessor :schema_format , :instance_writer => false
|
387
|
-
@@schema_format = :ruby
|
388
|
-
|
389
|
-
##
|
390
|
-
# :singleton-method:
|
391
|
-
# Specify whether or not to use timestamps for migration versions
|
392
|
-
cattr_accessor :timestamped_migrations , :instance_writer => false
|
393
|
-
@@timestamped_migrations = true
|
394
|
-
|
395
|
-
class << self # Class methods
|
396
|
-
def inherited(child_class) #:nodoc:
|
397
|
-
child_class.initialize_generated_modules
|
398
|
-
super
|
399
|
-
end
|
400
|
-
|
401
|
-
def initialize_generated_modules #:nodoc:
|
402
|
-
@attribute_methods_mutex = Mutex.new
|
403
|
-
|
404
|
-
# force attribute methods to be higher in inheritance hierarchy than other generated methods
|
405
|
-
generated_attribute_methods
|
406
|
-
generated_feature_methods
|
407
|
-
end
|
408
|
-
|
409
|
-
def generated_feature_methods
|
410
|
-
@generated_feature_methods ||= begin
|
411
|
-
mod = const_set(:GeneratedFeatureMethods, Module.new)
|
412
|
-
include mod
|
413
|
-
mod
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
# Returns a string like 'Post(id:integer, title:string, body:text)'
|
418
|
-
def inspect
|
419
|
-
if self == Base
|
420
|
-
super
|
421
|
-
elsif abstract_class?
|
422
|
-
"#{super}(abstract)"
|
423
|
-
elsif table_exists?
|
424
|
-
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
|
425
|
-
"#{super}(#{attr_list})"
|
426
|
-
else
|
427
|
-
"#{super}(Table doesn't exist)"
|
428
|
-
end
|
429
|
-
end
|
430
|
-
|
431
|
-
# Overwrite the default class equality method to provide support for association proxies.
|
432
|
-
def ===(object)
|
433
|
-
object.is_a?(self)
|
434
|
-
end
|
435
|
-
|
436
|
-
def arel_table
|
437
|
-
@arel_table ||= Arel::Table.new(table_name, arel_engine)
|
438
|
-
end
|
439
|
-
|
440
|
-
def arel_engine
|
441
|
-
@arel_engine ||= begin
|
442
|
-
if self == ActiveRecord::Base
|
443
|
-
ActiveRecord::Base
|
444
|
-
else
|
445
|
-
connection_handler.retrieve_connection_pool(self) ? self : superclass.arel_engine
|
446
|
-
end
|
447
|
-
end
|
448
|
-
end
|
449
|
-
|
450
|
-
private
|
451
|
-
|
452
|
-
def relation #:nodoc:
|
453
|
-
relation = Relation.new(self, arel_table)
|
454
|
-
|
455
|
-
if finder_needs_type_condition?
|
456
|
-
relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
|
457
|
-
else
|
458
|
-
relation
|
459
|
-
end
|
460
|
-
end
|
461
|
-
end
|
462
|
-
|
463
|
-
public
|
464
|
-
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
|
465
|
-
# attributes but not yet saved (pass a hash with key names matching the associated table column names).
|
466
|
-
# In both instances, valid attribute keys are determined by the column names of the associated table --
|
467
|
-
# hence you can't have attributes that aren't part of the table columns.
|
468
|
-
#
|
469
|
-
# +initialize+ respects mass-assignment security and accepts either +:as+ or +:without_protection+ options
|
470
|
-
# in the +options+ parameter.
|
471
|
-
#
|
472
|
-
# ==== Examples
|
473
|
-
# # Instantiates a single new object
|
474
|
-
# User.new(:first_name => 'Jamie')
|
475
|
-
#
|
476
|
-
# # Instantiates a single new object using the :admin mass-assignment security role
|
477
|
-
# User.new({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
|
478
|
-
#
|
479
|
-
# # Instantiates a single new object bypassing mass-assignment security
|
480
|
-
# User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
|
481
|
-
def initialize(attributes = nil, options = {})
|
482
|
-
defaults = Hash[self.class.column_defaults.map { |k, v| [k, v.duplicable? ? v.dup : v] }]
|
483
|
-
@attributes = self.class.initialize_attributes(defaults)
|
484
|
-
@association_cache = {}
|
485
|
-
@aggregation_cache = {}
|
486
|
-
@attributes_cache = {}
|
487
|
-
@new_record = true
|
488
|
-
@readonly = false
|
489
|
-
@destroyed = false
|
490
|
-
@marked_for_destruction = false
|
491
|
-
@previously_changed = {}
|
492
|
-
@changed_attributes = {}
|
493
|
-
|
494
|
-
ensure_proper_type
|
495
|
-
|
496
|
-
populate_with_current_scope_attributes
|
497
|
-
|
498
|
-
assign_attributes(attributes, options) if attributes
|
499
|
-
|
500
|
-
yield self if block_given?
|
501
|
-
run_callbacks :initialize
|
502
|
-
end
|
503
|
-
|
504
|
-
# Initialize an empty model object from +coder+. +coder+ must contain
|
505
|
-
# the attributes necessary for initializing an empty model object. For
|
506
|
-
# example:
|
507
|
-
#
|
508
|
-
# class Post < ActiveRecord::Base
|
509
|
-
# end
|
510
|
-
#
|
511
|
-
# post = Post.allocate
|
512
|
-
# post.init_with('attributes' => { 'title' => 'hello world' })
|
513
|
-
# post.title # => 'hello world'
|
514
|
-
def init_with(coder)
|
515
|
-
@attributes = self.class.initialize_attributes(coder['attributes'])
|
516
|
-
@relation = nil
|
517
|
-
|
518
|
-
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
|
519
|
-
@association_cache = {}
|
520
|
-
@aggregation_cache = {}
|
521
|
-
@readonly = @destroyed = @marked_for_destruction = false
|
522
|
-
@new_record = false
|
523
|
-
run_callbacks :find
|
524
|
-
run_callbacks :initialize
|
525
|
-
|
526
|
-
self
|
527
|
-
end
|
528
|
-
|
529
|
-
# Duped objects have no id assigned and are treated as new records. Note
|
530
|
-
# that this is a "shallow" copy as it copies the object's attributes
|
531
|
-
# only, not its associations. The extent of a "deep" copy is application
|
532
|
-
# specific and is therefore left to the application to implement according
|
533
|
-
# to its need.
|
534
|
-
# The dup method does not preserve the timestamps (created|updated)_(at|on).
|
535
|
-
def initialize_dup(other)
|
536
|
-
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
|
537
|
-
self.class.initialize_attributes(cloned_attributes, :serialized => false)
|
538
|
-
|
539
|
-
cloned_attributes.delete(self.class.primary_key)
|
540
|
-
|
541
|
-
@attributes = cloned_attributes
|
542
|
-
|
543
|
-
_run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
|
544
|
-
|
545
|
-
@changed_attributes = {}
|
546
|
-
self.class.column_defaults.each do |attr, orig_value|
|
547
|
-
@changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value, @attributes[attr])
|
548
|
-
end
|
549
|
-
|
550
|
-
@aggregation_cache = {}
|
551
|
-
@association_cache = {}
|
552
|
-
@attributes_cache = {}
|
553
|
-
@new_record = true
|
554
|
-
|
555
|
-
ensure_proper_type
|
556
|
-
super
|
557
|
-
end
|
558
|
-
|
559
|
-
# Backport dup from 1.9 so that initialize_dup() gets called
|
560
|
-
unless Object.respond_to?(:initialize_dup, true)
|
561
|
-
def dup # :nodoc:
|
562
|
-
copy = super
|
563
|
-
copy.initialize_dup(self)
|
564
|
-
copy
|
565
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
# Populate +coder+ with attributes about this record that should be
|
569
|
-
# serialized. The structure of +coder+ defined in this method is
|
570
|
-
# guaranteed to match the structure of +coder+ passed to the +init_with+
|
571
|
-
# method.
|
572
|
-
#
|
573
|
-
# Example:
|
574
|
-
#
|
575
|
-
# class Post < ActiveRecord::Base
|
576
|
-
# end
|
577
|
-
# coder = {}
|
578
|
-
# Post.new.encode_with(coder)
|
579
|
-
# coder # => { 'id' => nil, ... }
|
580
|
-
def encode_with(coder)
|
581
|
-
coder['attributes'] = attributes
|
582
|
-
end
|
583
|
-
|
584
|
-
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+
|
585
|
-
# is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
|
586
|
-
#
|
587
|
-
# Note that new records are different from any other record by definition, unless the
|
588
|
-
# other record is the receiver itself. Besides, if you fetch existing records with
|
589
|
-
# +select+ and leave the ID out, you're on your own, this predicate will return false.
|
590
|
-
#
|
591
|
-
# Note also that destroying a record preserves its ID in the model instance, so deleted
|
592
|
-
# models are still comparable.
|
593
|
-
def ==(comparison_object)
|
594
|
-
super ||
|
595
|
-
comparison_object.instance_of?(self.class) &&
|
596
|
-
id.present? &&
|
597
|
-
comparison_object.id == id
|
598
|
-
end
|
599
|
-
alias :eql? :==
|
600
|
-
|
601
|
-
# Delegates to id in order to allow two records of the same type and id to work with something like:
|
602
|
-
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
603
|
-
def hash
|
604
|
-
id.hash
|
605
|
-
end
|
606
|
-
|
607
|
-
# Freeze the attributes hash such that associations are still accessible, even on destroyed records.
|
608
|
-
def freeze
|
609
|
-
@attributes.freeze; self
|
610
|
-
end
|
611
|
-
|
612
|
-
# Returns +true+ if the attributes hash has been frozen.
|
613
|
-
def frozen?
|
614
|
-
@attributes.frozen?
|
615
|
-
end
|
616
|
-
|
617
|
-
# Allows sort on objects
|
618
|
-
def <=>(other_object)
|
619
|
-
if other_object.is_a?(self.class)
|
620
|
-
self.to_key <=> other_object.to_key
|
621
|
-
else
|
622
|
-
nil
|
623
|
-
end
|
624
|
-
end
|
625
|
-
|
626
|
-
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back
|
627
|
-
# attributes will be marked as read only since they cannot be saved.
|
628
|
-
def readonly?
|
629
|
-
@readonly
|
630
|
-
end
|
631
|
-
|
632
|
-
# Marks this record as read only.
|
633
|
-
def readonly!
|
634
|
-
@readonly = true
|
635
|
-
end
|
636
|
-
|
637
|
-
# Returns the contents of the record as a nicely formatted string.
|
638
|
-
def inspect
|
639
|
-
inspection = if @attributes
|
640
|
-
self.class.column_names.collect { |name|
|
641
|
-
if has_attribute?(name)
|
642
|
-
"#{name}: #{attribute_for_inspect(name)}"
|
643
|
-
end
|
644
|
-
}.compact.join(", ")
|
645
|
-
else
|
646
|
-
"not initialized"
|
647
|
-
end
|
648
|
-
"#<#{self.class} #{inspection}>"
|
649
|
-
end
|
650
|
-
|
651
|
-
# Hackery to accomodate Syck. Remove for 4.0.
|
652
|
-
def to_yaml(opts = {}) #:nodoc:
|
653
|
-
if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
|
654
|
-
super
|
655
|
-
else
|
656
|
-
coder = {}
|
657
|
-
encode_with(coder)
|
658
|
-
YAML.quick_emit(self, opts) do |out|
|
659
|
-
out.map(taguri, to_yaml_style) do |map|
|
660
|
-
coder.each { |k, v| map.add(k, v) }
|
661
|
-
end
|
662
|
-
end
|
663
|
-
end
|
664
|
-
end
|
665
|
-
|
666
|
-
# Hackery to accomodate Syck. Remove for 4.0.
|
667
|
-
def yaml_initialize(tag, coder) #:nodoc:
|
668
|
-
init_with(coder)
|
669
|
-
end
|
670
|
-
|
671
|
-
private
|
672
|
-
|
673
|
-
# Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements
|
674
|
-
# of the array, and then rescues from the possible NoMethodError. If those elements are
|
675
|
-
# ActiveRecord::Base's, then this triggers the various method_missing's that we have,
|
676
|
-
# which significantly impacts upon performance.
|
677
|
-
#
|
678
|
-
# So we can avoid the method_missing hit by explicitly defining #to_ary as nil here.
|
679
|
-
#
|
680
|
-
# See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/
|
681
|
-
def to_ary # :nodoc:
|
682
|
-
nil
|
683
|
-
end
|
684
|
-
|
685
|
-
include ActiveRecord::Persistence
|
686
282
|
extend ActiveModel::Naming
|
687
|
-
|
283
|
+
|
688
284
|
extend ActiveSupport::Benchmarkable
|
689
285
|
extend ActiveSupport::DescendantsTracker
|
690
286
|
|
287
|
+
extend ConnectionHandling
|
288
|
+
extend QueryCache::ClassMethods
|
691
289
|
extend Querying
|
290
|
+
extend Translation
|
291
|
+
extend DynamicMatchers
|
292
|
+
extend Explain
|
293
|
+
|
294
|
+
include Persistence
|
692
295
|
include ReadonlyAttributes
|
693
296
|
include ModelSchema
|
694
|
-
extend Translation
|
695
297
|
include Inheritance
|
696
298
|
include Scoping
|
697
|
-
extend DynamicMatchers
|
698
299
|
include Sanitization
|
699
300
|
include AttributeAssignment
|
700
301
|
include ActiveModel::Conversion
|
701
302
|
include Integration
|
702
303
|
include Validations
|
703
|
-
|
704
|
-
include Locking::Optimistic
|
304
|
+
include CounterCache
|
305
|
+
include Locking::Optimistic
|
306
|
+
include Locking::Pessimistic
|
705
307
|
include AttributeMethods
|
706
|
-
include Callbacks
|
308
|
+
include Callbacks
|
309
|
+
include Timestamp
|
707
310
|
include Associations
|
708
|
-
include IdentityMap
|
709
311
|
include ActiveModel::SecurePassword
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
include
|
715
|
-
include
|
312
|
+
include AutosaveAssociation
|
313
|
+
include NestedAttributes
|
314
|
+
include Aggregations
|
315
|
+
include Transactions
|
316
|
+
include Reflection
|
317
|
+
include Serialization
|
318
|
+
include Store
|
319
|
+
include Core
|
716
320
|
end
|
717
|
-
end
|
718
321
|
|
719
|
-
|
720
|
-
|
322
|
+
ActiveSupport.run_load_hooks(:active_record, Base)
|
323
|
+
end
|