activerecord 5.0.7.2 → 5.1.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 +5 -5
- data/CHANGELOG.md +389 -2252
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/examples/performance.rb +28 -28
- data/examples/simple.rb +3 -3
- data/lib/active_record.rb +20 -20
- data/lib/active_record/aggregations.rb +244 -244
- data/lib/active_record/association_relation.rb +5 -5
- data/lib/active_record/associations.rb +1579 -1569
- data/lib/active_record/associations/alias_tracker.rb +1 -1
- data/lib/active_record/associations/association.rb +23 -15
- data/lib/active_record/associations/association_scope.rb +83 -81
- data/lib/active_record/associations/belongs_to_association.rb +0 -1
- data/lib/active_record/associations/builder/belongs_to.rb +16 -14
- data/lib/active_record/associations/builder/collection_association.rb +1 -2
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +27 -27
- data/lib/active_record/associations/collection_association.rb +74 -241
- data/lib/active_record/associations/collection_proxy.rb +144 -70
- data/lib/active_record/associations/has_many_association.rb +15 -19
- data/lib/active_record/associations/has_many_through_association.rb +12 -5
- data/lib/active_record/associations/has_one_association.rb +22 -28
- data/lib/active_record/associations/has_one_through_association.rb +5 -1
- data/lib/active_record/associations/join_dependency.rb +117 -115
- data/lib/active_record/associations/join_dependency/join_association.rb +16 -13
- data/lib/active_record/associations/join_dependency/join_base.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
- data/lib/active_record/associations/preloader.rb +94 -94
- data/lib/active_record/associations/preloader/association.rb +87 -64
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -2
- data/lib/active_record/associations/preloader/collection_association.rb +6 -6
- data/lib/active_record/associations/preloader/has_many.rb +0 -2
- data/lib/active_record/associations/preloader/singular_association.rb +6 -8
- data/lib/active_record/associations/preloader/through_association.rb +34 -41
- data/lib/active_record/associations/singular_association.rb +8 -25
- data/lib/active_record/associations/through_association.rb +3 -6
- data/lib/active_record/attribute.rb +98 -71
- data/lib/active_record/attribute/user_provided_default.rb +4 -2
- data/lib/active_record/attribute_assignment.rb +61 -61
- data/lib/active_record/attribute_decorators.rb +35 -13
- data/lib/active_record/attribute_methods.rb +56 -65
- data/lib/active_record/attribute_methods/before_type_cast.rb +7 -7
- data/lib/active_record/attribute_methods/dirty.rb +216 -34
- data/lib/active_record/attribute_methods/primary_key.rb +78 -73
- data/lib/active_record/attribute_methods/read.rb +39 -35
- data/lib/active_record/attribute_methods/serialization.rb +7 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +35 -58
- data/lib/active_record/attribute_methods/write.rb +36 -30
- data/lib/active_record/attribute_mutation_tracker.rb +53 -10
- data/lib/active_record/attribute_set.rb +9 -6
- data/lib/active_record/attribute_set/builder.rb +41 -49
- data/lib/active_record/attribute_set/yaml_encoder.rb +41 -0
- data/lib/active_record/attributes.rb +21 -21
- data/lib/active_record/autosave_association.rb +13 -13
- data/lib/active_record/base.rb +24 -22
- data/lib/active_record/callbacks.rb +52 -14
- data/lib/active_record/coders/yaml_column.rb +9 -11
- data/lib/active_record/collection_cache_key.rb +6 -17
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +320 -278
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +1 -3
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +22 -34
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +44 -57
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +9 -19
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +78 -79
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +53 -41
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +99 -93
- data/lib/active_record/connection_adapters/abstract/transaction.rb +1 -5
- data/lib/active_record/connection_adapters/abstract_adapter.rb +156 -128
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +424 -382
- data/lib/active_record/connection_adapters/column.rb +27 -5
- data/lib/active_record/connection_adapters/connection_specification.rb +128 -118
- data/lib/active_record/connection_adapters/mysql/column.rb +6 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -43
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +22 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +6 -12
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +49 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +16 -19
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +49 -31
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +5 -6
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +24 -26
- data/lib/active_record/connection_adapters/postgresql/column.rb +1 -28
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -35
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid.rb +22 -21
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +9 -9
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +16 -16
- data/lib/active_record/connection_adapters/postgresql/oid/{rails_5_1_point.rb → legacy_point.rb} +9 -16
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +28 -8
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +28 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +51 -51
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +38 -36
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +37 -24
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -23
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +161 -170
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +4 -4
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -7
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +179 -152
- data/lib/active_record/connection_adapters/schema_cache.rb +16 -7
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +3 -3
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +1 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +16 -20
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +1 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +28 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +187 -130
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -7
- data/lib/active_record/connection_handling.rb +14 -26
- data/lib/active_record/core.rb +110 -93
- data/lib/active_record/counter_cache.rb +62 -13
- data/lib/active_record/define_callbacks.rb +20 -0
- data/lib/active_record/dynamic_matchers.rb +80 -79
- data/lib/active_record/enum.rb +8 -6
- data/lib/active_record/errors.rb +58 -15
- data/lib/active_record/explain.rb +1 -2
- data/lib/active_record/explain_registry.rb +1 -1
- data/lib/active_record/explain_subscriber.rb +7 -4
- data/lib/active_record/fixture_set/file.rb +11 -8
- data/lib/active_record/fixtures.rb +66 -53
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +93 -79
- data/lib/active_record/integration.rb +7 -7
- data/lib/active_record/internal_metadata.rb +3 -16
- data/lib/active_record/legacy_yaml_adapter.rb +1 -1
- data/lib/active_record/locking/optimistic.rb +64 -56
- data/lib/active_record/locking/pessimistic.rb +10 -1
- data/lib/active_record/log_subscriber.rb +29 -29
- data/lib/active_record/migration.rb +155 -172
- data/lib/active_record/migration/command_recorder.rb +94 -94
- data/lib/active_record/migration/compatibility.rb +76 -37
- data/lib/active_record/migration/join_table.rb +6 -6
- data/lib/active_record/model_schema.rb +85 -119
- data/lib/active_record/nested_attributes.rb +200 -199
- data/lib/active_record/null_relation.rb +10 -33
- data/lib/active_record/persistence.rb +45 -38
- data/lib/active_record/query_cache.rb +4 -8
- data/lib/active_record/querying.rb +2 -3
- data/lib/active_record/railtie.rb +16 -17
- data/lib/active_record/railties/controller_runtime.rb +6 -2
- data/lib/active_record/railties/databases.rake +125 -140
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +2 -2
- data/lib/active_record/reflection.rb +79 -96
- data/lib/active_record/relation.rb +72 -115
- data/lib/active_record/relation/batches.rb +87 -58
- data/lib/active_record/relation/batches/batch_enumerator.rb +1 -1
- data/lib/active_record/relation/calculations.rb +154 -160
- data/lib/active_record/relation/delegation.rb +30 -29
- data/lib/active_record/relation/finder_methods.rb +195 -226
- data/lib/active_record/relation/merger.rb +58 -62
- data/lib/active_record/relation/predicate_builder.rb +92 -89
- data/lib/active_record/relation/predicate_builder/array_handler.rb +7 -5
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +23 -23
- data/lib/active_record/relation/predicate_builder/base_handler.rb +3 -1
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +0 -8
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +12 -10
- data/lib/active_record/relation/predicate_builder/range_handler.rb +0 -8
- data/lib/active_record/relation/query_attribute.rb +1 -1
- data/lib/active_record/relation/query_methods.rb +247 -295
- data/lib/active_record/relation/record_fetch_warning.rb +3 -3
- data/lib/active_record/relation/spawn_methods.rb +4 -5
- data/lib/active_record/relation/where_clause.rb +79 -65
- data/lib/active_record/relation/where_clause_factory.rb +47 -8
- data/lib/active_record/result.rb +29 -31
- data/lib/active_record/runtime_registry.rb +3 -3
- data/lib/active_record/sanitization.rb +182 -197
- data/lib/active_record/schema.rb +3 -3
- data/lib/active_record/schema_dumper.rb +14 -37
- data/lib/active_record/schema_migration.rb +3 -3
- data/lib/active_record/scoping.rb +9 -10
- data/lib/active_record/scoping/default.rb +87 -91
- data/lib/active_record/scoping/named.rb +16 -28
- data/lib/active_record/secure_token.rb +2 -2
- data/lib/active_record/statement_cache.rb +13 -15
- data/lib/active_record/store.rb +31 -32
- data/lib/active_record/suppressor.rb +2 -1
- data/lib/active_record/table_metadata.rb +9 -5
- data/lib/active_record/tasks/database_tasks.rb +72 -65
- data/lib/active_record/tasks/mysql_database_tasks.rb +75 -72
- data/lib/active_record/tasks/postgresql_database_tasks.rb +53 -48
- data/lib/active_record/tasks/sqlite_database_tasks.rb +18 -16
- data/lib/active_record/timestamp.rb +39 -25
- data/lib/active_record/touch_later.rb +1 -2
- data/lib/active_record/transactions.rb +98 -110
- data/lib/active_record/type.rb +17 -13
- data/lib/active_record/type/adapter_specific_registry.rb +46 -42
- data/lib/active_record/type/decimal_without_scale.rb +9 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +3 -3
- data/lib/active_record/type/serialized.rb +8 -8
- data/lib/active_record/type/text.rb +9 -0
- data/lib/active_record/type/time.rb +0 -1
- data/lib/active_record/type/type_map.rb +11 -15
- data/lib/active_record/type/unsigned_integer.rb +15 -0
- data/lib/active_record/type_caster.rb +2 -2
- data/lib/active_record/type_caster/connection.rb +8 -6
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/validations.rb +4 -4
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record/validations/presence.rb +2 -2
- data/lib/active_record/validations/uniqueness.rb +8 -39
- data/lib/active_record/version.rb +1 -1
- data/lib/rails/generators/active_record.rb +4 -4
- data/lib/rails/generators/active_record/migration.rb +2 -2
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -34
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -9
- metadata +22 -13
- data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
@@ -6,7 +6,7 @@ module ActiveRecord
|
|
6
6
|
DEFAULT_STATEMENT_LIMIT = 1000
|
7
7
|
|
8
8
|
def initialize(statement_limit = nil)
|
9
|
-
@cache = Hash.new { |h,pid| h[pid] = {} }
|
9
|
+
@cache = Hash.new { |h, pid| h[pid] = {} }
|
10
10
|
@statement_limit = statement_limit || DEFAULT_STATEMENT_LIMIT
|
11
11
|
end
|
12
12
|
|
@@ -47,13 +47,13 @@ module ActiveRecord
|
|
47
47
|
|
48
48
|
private
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
def cache
|
51
|
+
@cache[Process.pid]
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
def dealloc(stmt)
|
55
|
+
raise NotImplementedError
|
56
|
+
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -44,21 +44,18 @@ module ActiveRecord
|
|
44
44
|
#
|
45
45
|
# The exceptions AdapterNotSpecified, AdapterNotFound and +ArgumentError+
|
46
46
|
# may be returned on an error.
|
47
|
-
def establish_connection(
|
48
|
-
raise
|
47
|
+
def establish_connection(config = nil)
|
48
|
+
raise "Anonymous class is not allowed." unless name
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
spec = resolver.spec(spec, self == Base ? "primary" : name)
|
50
|
+
config ||= DEFAULT_ENV.call.to_sym
|
51
|
+
spec_name = self == Base ? "primary" : name
|
52
|
+
self.connection_specification_name = spec_name
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(Base.configurations)
|
55
|
+
spec = resolver.resolve(config).symbolize_keys
|
56
|
+
spec[:name] = spec_name
|
58
57
|
|
59
|
-
|
60
|
-
self.connection_specification_name = spec.name
|
61
|
-
connection_handler.establish_connection spec
|
58
|
+
connection_handler.establish_connection(spec)
|
62
59
|
end
|
63
60
|
|
64
61
|
class MergeAndResolveDefaultUrlConfig # :nodoc:
|
@@ -76,7 +73,7 @@ module ActiveRecord
|
|
76
73
|
private
|
77
74
|
def config
|
78
75
|
@raw_config.dup.tap do |cfg|
|
79
|
-
if url = ENV[
|
76
|
+
if url = ENV["DATABASE_URL"]
|
80
77
|
cfg[@env] ||= {}
|
81
78
|
cfg[@env]["url"] ||= url
|
82
79
|
end
|
@@ -93,8 +90,7 @@ module ActiveRecord
|
|
93
90
|
|
94
91
|
attr_writer :connection_specification_name
|
95
92
|
|
96
|
-
# Return the specification
|
97
|
-
# in the parent.
|
93
|
+
# Return the specification name from the current class or its parent.
|
98
94
|
def connection_specification_name
|
99
95
|
if !defined?(@connection_specification_name) || @connection_specification_name.nil?
|
100
96
|
return self == Base ? "primary" : superclass.connection_specification_name
|
@@ -102,14 +98,6 @@ module ActiveRecord
|
|
102
98
|
@connection_specification_name
|
103
99
|
end
|
104
100
|
|
105
|
-
def connection_id
|
106
|
-
ActiveRecord::RuntimeRegistry.connection_id ||= Thread.current.object_id
|
107
|
-
end
|
108
|
-
|
109
|
-
def connection_id=(connection_id)
|
110
|
-
ActiveRecord::RuntimeRegistry.connection_id = connection_id
|
111
|
-
end
|
112
|
-
|
113
101
|
# Returns the configuration of the associated connection as a hash:
|
114
102
|
#
|
115
103
|
# ActiveRecord::Base.connection_config
|
@@ -121,7 +109,7 @@ module ActiveRecord
|
|
121
109
|
end
|
122
110
|
|
123
111
|
def connection_pool
|
124
|
-
connection_handler.retrieve_connection_pool(connection_specification_name)
|
112
|
+
connection_handler.retrieve_connection_pool(connection_specification_name) || raise(ConnectionNotEstablished)
|
125
113
|
end
|
126
114
|
|
127
115
|
def retrieve_connection
|
@@ -135,7 +123,7 @@ module ActiveRecord
|
|
135
123
|
|
136
124
|
def remove_connection(name = nil)
|
137
125
|
name ||= @connection_specification_name if defined?(@connection_specification_name)
|
138
|
-
# if removing a connection that
|
126
|
+
# if removing a connection that has a pool, we reset the
|
139
127
|
# connection_specification_name so it will use the parent
|
140
128
|
# pool.
|
141
129
|
if connection_handler.retrieve_connection_pool(name)
|
@@ -150,6 +138,6 @@ module ActiveRecord
|
|
150
138
|
end
|
151
139
|
|
152
140
|
delegate :clear_active_connections!, :clear_reloadable_connections!,
|
153
|
-
:clear_all_connections!, :
|
141
|
+
:clear_all_connections!, to: :connection_handler
|
154
142
|
end
|
155
143
|
end
|
data/lib/active_record/core.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "thread"
|
2
|
+
require "active_support/core_ext/hash/indifferent_access"
|
3
|
+
require "active_support/core_ext/object/duplicable"
|
4
|
+
require "active_support/core_ext/string/filters"
|
5
5
|
|
6
6
|
module ActiveRecord
|
7
7
|
module Core
|
@@ -72,11 +72,31 @@ module ActiveRecord
|
|
72
72
|
|
73
73
|
##
|
74
74
|
# :singleton-method:
|
75
|
-
# Specifies if an error should be raised
|
75
|
+
# Specifies if an error should be raised if the query has an order being
|
76
76
|
# ignored when doing batch queries. Useful in applications where the
|
77
|
-
#
|
78
|
-
mattr_accessor :
|
79
|
-
self.
|
77
|
+
# scope being ignored is error-worthy, rather than a warning.
|
78
|
+
mattr_accessor :error_on_ignored_order, instance_writer: false
|
79
|
+
self.error_on_ignored_order = false
|
80
|
+
|
81
|
+
def self.error_on_ignored_order_or_limit
|
82
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
83
|
+
The flag error_on_ignored_order_or_limit is deprecated. Limits are
|
84
|
+
now supported. Please use error_on_ignored_order instead.
|
85
|
+
MSG
|
86
|
+
error_on_ignored_order
|
87
|
+
end
|
88
|
+
|
89
|
+
def error_on_ignored_order_or_limit
|
90
|
+
self.class.error_on_ignored_order_or_limit
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.error_on_ignored_order_or_limit=(value)
|
94
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
95
|
+
The flag error_on_ignored_order_or_limit is deprecated. Limits are
|
96
|
+
now supported. Please use error_on_ignored_order= instead.
|
97
|
+
MSG
|
98
|
+
self.error_on_ignored_order = value
|
99
|
+
end
|
80
100
|
|
81
101
|
##
|
82
102
|
# :singleton-method:
|
@@ -151,30 +171,26 @@ module ActiveRecord
|
|
151
171
|
return super if block_given? ||
|
152
172
|
primary_key.nil? ||
|
153
173
|
scope_attributes? ||
|
154
|
-
columns_hash.include?(inheritance_column)
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
161
|
-
You are passing an instance of ActiveRecord::Base to `find`.
|
162
|
-
Please pass the id of the object by calling `.id`.
|
163
|
-
MSG
|
164
|
-
end
|
174
|
+
columns_hash.include?(inheritance_column)
|
175
|
+
|
176
|
+
id = ids.first
|
177
|
+
|
178
|
+
return super if id.kind_of?(Array) ||
|
179
|
+
id.is_a?(ActiveRecord::Base)
|
165
180
|
|
166
181
|
key = primary_key
|
167
182
|
|
168
183
|
statement = cached_find_by_statement(key) { |params|
|
169
184
|
where(key => params.bind).limit(1)
|
170
185
|
}
|
186
|
+
|
171
187
|
record = statement.execute([id], self, connection).first
|
172
188
|
unless record
|
173
189
|
raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
|
174
190
|
name, primary_key, id)
|
175
191
|
end
|
176
192
|
record
|
177
|
-
rescue RangeError
|
193
|
+
rescue ::RangeError
|
178
194
|
raise RecordNotFound.new("Couldn't find #{name} with an out of range value for '#{primary_key}'",
|
179
195
|
name, primary_key)
|
180
196
|
end
|
@@ -203,13 +219,13 @@ module ActiveRecord
|
|
203
219
|
statement.execute(hash.values, self, connection).first
|
204
220
|
rescue TypeError
|
205
221
|
raise ActiveRecord::StatementInvalid
|
206
|
-
rescue RangeError
|
222
|
+
rescue ::RangeError
|
207
223
|
nil
|
208
224
|
end
|
209
225
|
end
|
210
226
|
|
211
227
|
def find_by!(*args) # :nodoc:
|
212
|
-
find_by(*args)
|
228
|
+
find_by(*args) || raise(RecordNotFound.new("Couldn't find #{name}", name))
|
213
229
|
end
|
214
230
|
|
215
231
|
def initialize_generated_modules # :nodoc:
|
@@ -219,7 +235,9 @@ module ActiveRecord
|
|
219
235
|
def generated_association_methods
|
220
236
|
@generated_association_methods ||= begin
|
221
237
|
mod = const_set(:GeneratedAssociationMethods, Module.new)
|
238
|
+
private_constant :GeneratedAssociationMethods
|
222
239
|
include mod
|
240
|
+
|
223
241
|
mod
|
224
242
|
end
|
225
243
|
end
|
@@ -233,7 +251,7 @@ module ActiveRecord
|
|
233
251
|
elsif !connected?
|
234
252
|
"#{super} (call '#{super}.connection' to establish a connection)"
|
235
253
|
elsif table_exists?
|
236
|
-
attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } *
|
254
|
+
attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", "
|
237
255
|
"#{super}(#{attr_list})"
|
238
256
|
else
|
239
257
|
"#{super}(Table doesn't exist)"
|
@@ -248,7 +266,7 @@ module ActiveRecord
|
|
248
266
|
# Returns an instance of <tt>Arel::Table</tt> loaded with the current table name.
|
249
267
|
#
|
250
268
|
# class Post < ActiveRecord::Base
|
251
|
-
# scope :published_and_commented, -> { published.and(
|
269
|
+
# scope :published_and_commented, -> { published.and(arel_table[:comments_count].gt(0)) }
|
252
270
|
# end
|
253
271
|
def arel_table # :nodoc:
|
254
272
|
@arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
|
@@ -279,26 +297,26 @@ module ActiveRecord
|
|
279
297
|
|
280
298
|
private
|
281
299
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
300
|
+
def cached_find_by_statement(key, &block)
|
301
|
+
cache = @find_by_statement_cache[connection.prepared_statements]
|
302
|
+
cache[key] || cache.synchronize {
|
303
|
+
cache[key] ||= StatementCache.create(connection, &block)
|
304
|
+
}
|
305
|
+
end
|
288
306
|
|
289
|
-
|
290
|
-
|
307
|
+
def relation
|
308
|
+
relation = Relation.create(self, arel_table, predicate_builder)
|
291
309
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
310
|
+
if finder_needs_type_condition? && !ignore_default_scope?
|
311
|
+
relation.where(type_condition).create_with(inheritance_column.to_s => sti_name)
|
312
|
+
else
|
313
|
+
relation
|
314
|
+
end
|
296
315
|
end
|
297
|
-
end
|
298
316
|
|
299
|
-
|
300
|
-
|
301
|
-
|
317
|
+
def table_metadata
|
318
|
+
TableMetadata.new(self, arel_table)
|
319
|
+
end
|
302
320
|
end
|
303
321
|
|
304
322
|
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
|
@@ -338,11 +356,11 @@ module ActiveRecord
|
|
338
356
|
# post.title # => 'hello world'
|
339
357
|
def init_with(coder)
|
340
358
|
coder = LegacyYamlAdapter.convert(self.class, coder)
|
341
|
-
@attributes = coder
|
359
|
+
@attributes = self.class.yaml_encoder.decode(coder)
|
342
360
|
|
343
361
|
init_internals
|
344
362
|
|
345
|
-
@new_record = coder[
|
363
|
+
@new_record = coder["new_record"]
|
346
364
|
|
347
365
|
self.class.define_attribute_methods
|
348
366
|
|
@@ -406,11 +424,9 @@ module ActiveRecord
|
|
406
424
|
# Post.new.encode_with(coder)
|
407
425
|
# coder # => {"attributes" => {"id" => nil, ... }}
|
408
426
|
def encode_with(coder)
|
409
|
-
|
410
|
-
coder[
|
411
|
-
coder[
|
412
|
-
coder['new_record'] = new_record?
|
413
|
-
coder['active_record_yaml_version'] = 1
|
427
|
+
self.class.yaml_encoder.encode(@attributes, coder)
|
428
|
+
coder["new_record"] = new_record?
|
429
|
+
coder["active_record_yaml_version"] = 2
|
414
430
|
end
|
415
431
|
|
416
432
|
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+
|
@@ -434,7 +450,7 @@ module ActiveRecord
|
|
434
450
|
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
435
451
|
def hash
|
436
452
|
if id
|
437
|
-
id.hash
|
453
|
+
self.class.hash ^ id.hash
|
438
454
|
else
|
439
455
|
super
|
440
456
|
end
|
@@ -456,7 +472,7 @@ module ActiveRecord
|
|
456
472
|
# Allows sort on objects
|
457
473
|
def <=>(other_object)
|
458
474
|
if other_object.is_a?(self.class)
|
459
|
-
|
475
|
+
to_key <=> other_object.to_key
|
460
476
|
else
|
461
477
|
super
|
462
478
|
end
|
@@ -482,14 +498,15 @@ module ActiveRecord
|
|
482
498
|
# We check defined?(@attributes) not to issue warnings if the object is
|
483
499
|
# allocated but not initialized.
|
484
500
|
inspection = if defined?(@attributes) && @attributes
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
501
|
+
self.class.attribute_names.collect do |name|
|
502
|
+
if has_attribute?(name)
|
503
|
+
"#{name}: #{attribute_for_inspect(name)}"
|
504
|
+
end
|
505
|
+
end.compact.join(", ")
|
506
|
+
else
|
507
|
+
"not initialized"
|
508
|
+
end
|
509
|
+
|
493
510
|
"#<#{self.class} #{inspection}>"
|
494
511
|
end
|
495
512
|
|
@@ -500,64 +517,64 @@ module ActiveRecord
|
|
500
517
|
pp.object_address_group(self) do
|
501
518
|
if defined?(@attributes) && @attributes
|
502
519
|
column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? }
|
503
|
-
pp.seplist(column_names, proc { pp.text
|
520
|
+
pp.seplist(column_names, proc { pp.text "," }) do |column_name|
|
504
521
|
column_value = read_attribute(column_name)
|
505
|
-
pp.breakable
|
522
|
+
pp.breakable " "
|
506
523
|
pp.group(1) do
|
507
524
|
pp.text column_name
|
508
|
-
pp.text
|
525
|
+
pp.text ":"
|
509
526
|
pp.breakable
|
510
527
|
pp.pp column_value
|
511
528
|
end
|
512
529
|
end
|
513
530
|
else
|
514
|
-
pp.breakable
|
515
|
-
pp.text
|
531
|
+
pp.breakable " "
|
532
|
+
pp.text "not initialized"
|
516
533
|
end
|
517
534
|
end
|
518
535
|
end
|
519
536
|
|
520
537
|
# Returns a hash of the given methods with their names as keys and returned values as values.
|
521
538
|
def slice(*methods)
|
522
|
-
Hash[methods.map! { |method| [method, public_send(method)] }].with_indifferent_access
|
539
|
+
Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access
|
523
540
|
end
|
524
541
|
|
525
542
|
private
|
526
543
|
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
544
|
+
# +Array#flatten+ will call +#to_ary+ (recursively) on each of the elements of
|
545
|
+
# the array, and then rescues from the possible +NoMethodError+. If those elements are
|
546
|
+
# +ActiveRecord::Base+'s, then this triggers the various +method_missing+'s that we have,
|
547
|
+
# which significantly impacts upon performance.
|
548
|
+
#
|
549
|
+
# So we can avoid the +method_missing+ hit by explicitly defining +#to_ary+ as +nil+ here.
|
550
|
+
#
|
551
|
+
# See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
|
552
|
+
def to_ary
|
553
|
+
nil
|
554
|
+
end
|
538
555
|
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
556
|
+
def init_internals
|
557
|
+
@readonly = false
|
558
|
+
@destroyed = false
|
559
|
+
@marked_for_destruction = false
|
560
|
+
@destroyed_by_association = nil
|
561
|
+
@new_record = true
|
562
|
+
@txn = nil
|
563
|
+
@_start_transaction_state = {}
|
564
|
+
@transaction_state = nil
|
565
|
+
end
|
549
566
|
|
550
|
-
|
551
|
-
|
567
|
+
def initialize_internals_callback
|
568
|
+
end
|
552
569
|
|
553
|
-
|
554
|
-
|
555
|
-
|
570
|
+
def thaw
|
571
|
+
if frozen?
|
572
|
+
@attributes = @attributes.dup
|
573
|
+
end
|
556
574
|
end
|
557
|
-
end
|
558
575
|
|
559
|
-
|
560
|
-
|
561
|
-
|
576
|
+
def custom_inspect_method_defined?
|
577
|
+
self.class.instance_method(:inspect).owner != ActiveRecord::Base.instance_method(:inspect).owner
|
578
|
+
end
|
562
579
|
end
|
563
580
|
end
|