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
@@ -15,9 +15,9 @@ module ActiveRecord
|
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def perform_fetch(type, *args, &block)
|
19
|
+
@mapping.fetch(type, block).call(type, *args)
|
20
|
+
end
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -43,21 +43,21 @@ module ActiveRecord
|
|
43
43
|
|
44
44
|
def assert_valid_value(value)
|
45
45
|
if coder.respond_to?(:assert_valid_value)
|
46
|
-
coder.assert_valid_value(value)
|
46
|
+
coder.assert_valid_value(value, action: "serialize")
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
private
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
def default_value?(value)
|
53
|
+
value == coder.load(nil)
|
54
|
+
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
def encoded(value)
|
57
|
+
unless default_value?(value)
|
58
|
+
coder.dump(value)
|
59
|
+
end
|
59
60
|
end
|
60
|
-
end
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "concurrent/map"
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Type
|
@@ -11,7 +11,7 @@ module ActiveRecord
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def lookup(lookup_key, *args)
|
14
|
-
fetch(lookup_key, *args) { default_value }
|
14
|
+
fetch(lookup_key, *args) { Type.default_value }
|
15
15
|
end
|
16
16
|
|
17
17
|
def fetch(lookup_key, *args, &block)
|
@@ -44,21 +44,17 @@ module ActiveRecord
|
|
44
44
|
|
45
45
|
private
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
def perform_fetch(lookup_key, *args)
|
48
|
+
matching_pair = @mapping.reverse_each.detect do |key, _|
|
49
|
+
key === lookup_key
|
50
|
+
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
if matching_pair
|
53
|
+
matching_pair.last.call(lookup_key, *args)
|
54
|
+
else
|
55
|
+
yield lookup_key, *args
|
56
|
+
end
|
56
57
|
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def default_value
|
60
|
-
@default_value ||= ActiveModel::Type::Value.new
|
61
|
-
end
|
62
58
|
end
|
63
59
|
end
|
64
60
|
end
|
@@ -12,18 +12,20 @@ module ActiveRecord
|
|
12
12
|
connection.type_cast_from_column(column, value)
|
13
13
|
end
|
14
14
|
|
15
|
+
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
16
|
+
# Workaround for Ruby 2.2 "private attribute?" warning.
|
15
17
|
protected
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
attr_reader :table_name
|
20
|
+
delegate :connection, to: :@klass
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
def column_for(attribute_name)
|
25
|
+
if connection.schema_cache.data_source_exists?(table_name)
|
26
|
+
connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
|
27
|
+
end
|
25
28
|
end
|
26
|
-
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
@@ -11,9 +11,11 @@ module ActiveRecord
|
|
11
11
|
type.serialize(value)
|
12
12
|
end
|
13
13
|
|
14
|
+
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
15
|
+
# Workaround for Ruby 2.2 "private attribute?" warning.
|
14
16
|
protected
|
15
17
|
|
16
|
-
|
18
|
+
attr_reader :types
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -40,13 +40,13 @@ module ActiveRecord
|
|
40
40
|
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
41
41
|
# The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced
|
42
42
|
# with this when the validations module is mixed in, which it is by default.
|
43
|
-
def save(options={})
|
43
|
+
def save(options = {})
|
44
44
|
perform_validations(options) ? super : false
|
45
45
|
end
|
46
46
|
|
47
47
|
# Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
|
48
48
|
# will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid.
|
49
|
-
def save!(options={})
|
49
|
+
def save!(options = {})
|
50
50
|
perform_validations(options) ? super : raise_validation_error
|
51
51
|
end
|
52
52
|
|
@@ -68,7 +68,7 @@ module ActiveRecord
|
|
68
68
|
|
69
69
|
alias_method :validate, :valid?
|
70
70
|
|
71
|
-
|
71
|
+
private
|
72
72
|
|
73
73
|
def default_validation_context
|
74
74
|
new_record? ? :create : :update
|
@@ -78,7 +78,7 @@ module ActiveRecord
|
|
78
78
|
raise(RecordInvalid.new(self))
|
79
79
|
end
|
80
80
|
|
81
|
-
def perform_validations(options={})
|
81
|
+
def perform_validations(options = {})
|
82
82
|
options[:validate] == false || valid?(options[:context])
|
83
83
|
end
|
84
84
|
end
|
@@ -37,7 +37,7 @@ module ActiveRecord
|
|
37
37
|
#
|
38
38
|
# * <tt>:message</tt> - A custom error message (default is: "is invalid").
|
39
39
|
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
40
|
-
# Runs in all validation contexts by default
|
40
|
+
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
41
41
|
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
42
42
|
# <tt>on: :custom_validation_context</tt> or
|
43
43
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
@@ -44,7 +44,7 @@ module ActiveRecord
|
|
44
44
|
# Configuration options:
|
45
45
|
# * <tt>:message</tt> - A custom error message (default is: "can't be blank").
|
46
46
|
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
47
|
-
# Runs in all validation contexts by default
|
47
|
+
# Runs in all validation contexts by default +nil+. You can pass a symbol
|
48
48
|
# or an array of symbols. (e.g. <tt>on: :create</tt> or
|
49
49
|
# <tt>on: :custom_validation_context</tt> or
|
50
50
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
@@ -57,7 +57,7 @@ module ActiveRecord
|
|
57
57
|
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
58
58
|
# proc or string should return or evaluate to a +true+ or +false+ value.
|
59
59
|
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
60
|
-
# See ActiveModel::
|
60
|
+
# See ActiveModel::Validations#validates! for more information.
|
61
61
|
def validates_presence_of(*attr_names)
|
62
62
|
validates_with PresenceValidator, _merge_attributes(attr_names)
|
63
63
|
end
|
@@ -12,18 +12,17 @@ module ActiveRecord
|
|
12
12
|
|
13
13
|
def validate_each(record, attribute, value)
|
14
14
|
finder_class = find_finder_class_for(record)
|
15
|
-
table = finder_class.arel_table
|
16
15
|
value = map_enum_attribute(finder_class, attribute, value)
|
17
16
|
|
18
|
-
relation = build_relation(finder_class,
|
17
|
+
relation = build_relation(finder_class, attribute, value)
|
19
18
|
if record.persisted?
|
20
19
|
if finder_class.primary_key
|
21
|
-
relation = relation.where.not(finder_class.primary_key => record.
|
20
|
+
relation = relation.where.not(finder_class.primary_key => record.id_in_database || record.id)
|
22
21
|
else
|
23
22
|
raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
|
24
23
|
end
|
25
24
|
end
|
26
|
-
relation = scope_relation(record,
|
25
|
+
relation = scope_relation(record, relation)
|
27
26
|
relation = relation.merge(options[:conditions]) if options[:conditions]
|
28
27
|
|
29
28
|
if relation.exists?
|
@@ -34,13 +33,13 @@ module ActiveRecord
|
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
|
36
|
+
private
|
38
37
|
# The check for an existing value should be run from a class that
|
39
38
|
# isn't abstract. This means working down from the current class
|
40
39
|
# (self), to the first non-abstract class. Since classes don't know
|
41
40
|
# their subclasses, we have to build the hierarchy between self and
|
42
41
|
# the record's class.
|
43
|
-
def find_finder_class_for(record)
|
42
|
+
def find_finder_class_for(record)
|
44
43
|
class_hierarchy = [record.class]
|
45
44
|
|
46
45
|
while class_hierarchy.first != @klass
|
@@ -50,41 +49,11 @@ module ActiveRecord
|
|
50
49
|
class_hierarchy.detect { |klass| !klass.abstract_class? }
|
51
50
|
end
|
52
51
|
|
53
|
-
def build_relation(klass,
|
54
|
-
|
55
|
-
attribute = reflection.foreign_key
|
56
|
-
value = value.attributes[reflection.klass.primary_key] unless value.nil?
|
57
|
-
end
|
58
|
-
|
59
|
-
# the attribute may be an aliased attribute
|
60
|
-
if klass.attribute_alias?(attribute)
|
61
|
-
attribute = klass.attribute_alias(attribute)
|
62
|
-
end
|
63
|
-
|
64
|
-
attribute_name = attribute.to_s
|
65
|
-
|
66
|
-
column = klass.columns_hash[attribute_name]
|
67
|
-
cast_type = klass.type_for_attribute(attribute_name)
|
68
|
-
value = cast_type.serialize(value)
|
69
|
-
value = klass.connection.type_cast(value)
|
70
|
-
|
71
|
-
comparison = if !options[:case_sensitive] && !value.nil?
|
72
|
-
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
73
|
-
klass.connection.case_insensitive_comparison(table, attribute, column, value)
|
74
|
-
else
|
75
|
-
klass.connection.case_sensitive_comparison(table, attribute, column, value)
|
76
|
-
end
|
77
|
-
if value.nil?
|
78
|
-
klass.unscoped.where(comparison)
|
79
|
-
else
|
80
|
-
bind = Relation::QueryAttribute.new(attribute_name, value, Type::Value.new)
|
81
|
-
klass.unscoped.where(comparison, bind)
|
82
|
-
end
|
83
|
-
rescue RangeError
|
84
|
-
klass.none
|
52
|
+
def build_relation(klass, attribute, value)
|
53
|
+
klass.unscoped.where!({ attribute => value }, options)
|
85
54
|
end
|
86
55
|
|
87
|
-
def scope_relation(record,
|
56
|
+
def scope_relation(record, relation)
|
88
57
|
Array(options[:scope]).each do |scope_item|
|
89
58
|
scope_value = if record.class._reflect_on_association(scope_item)
|
90
59
|
record.association(scope_item).reader
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "rails/generators/named_base"
|
2
|
+
require "rails/generators/active_model"
|
3
|
+
require "rails/generators/active_record/migration"
|
4
|
+
require "active_record"
|
5
5
|
|
6
6
|
module ActiveRecord
|
7
7
|
module Generators # :nodoc:
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "rails/generators/migration"
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Generators # :nodoc:
|
@@ -22,7 +22,7 @@ module ActiveRecord
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def db_migrate_path
|
25
|
-
if defined?(Rails
|
25
|
+
if defined?(Rails) && Rails.application
|
26
26
|
Rails.application.config.paths["db/migrate"].to_ary.first
|
27
27
|
else
|
28
28
|
"db/migrate"
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require "rails/generators/active_record"
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Generators # :nodoc:
|
5
5
|
class MigrationGenerator < Base # :nodoc:
|
6
|
-
argument :attributes, :
|
6
|
+
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
|
7
7
|
|
8
8
|
class_option :primary_key_type, type: :string, desc: "The type for primary key"
|
9
9
|
|
@@ -13,46 +13,49 @@ module ActiveRecord
|
|
13
13
|
migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
|
14
14
|
end
|
15
15
|
|
16
|
+
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
17
|
+
# Workaround for Ruby 2.2 "private attribute?" warning.
|
16
18
|
protected
|
17
|
-
|
19
|
+
attr_reader :migration_action, :join_tables
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
21
|
+
private
|
22
|
+
|
23
|
+
# Sets the default migration template that is being used for the generation of the migration.
|
24
|
+
# Depending on command line arguments, the migration template and the table name instance
|
25
|
+
# variables are set up.
|
26
|
+
def set_local_assigns!
|
27
|
+
@migration_template = "migration.rb"
|
28
|
+
case file_name
|
29
|
+
when /^(add|remove)_.*_(?:to|from)_(.*)/
|
30
|
+
@migration_action = $1
|
31
|
+
@table_name = normalize_table_name($2)
|
32
|
+
when /join_table/
|
33
|
+
if attributes.length == 2
|
34
|
+
@migration_action = "join"
|
35
|
+
@join_tables = pluralize_table_names? ? attributes.map(&:plural_name) : attributes.map(&:singular_name)
|
32
36
|
|
33
|
-
|
37
|
+
set_index_names
|
38
|
+
end
|
39
|
+
when /^create_(.+)/
|
40
|
+
@table_name = normalize_table_name($1)
|
41
|
+
@migration_template = "create_table_migration.rb"
|
34
42
|
end
|
35
|
-
when /^create_(.+)/
|
36
|
-
@table_name = normalize_table_name($1)
|
37
|
-
@migration_template = "create_table_migration.rb"
|
38
43
|
end
|
39
|
-
end
|
40
44
|
|
41
|
-
|
42
|
-
|
43
|
-
|
45
|
+
def set_index_names
|
46
|
+
attributes.each_with_index do |attr, i|
|
47
|
+
attr.index_name = [attr, attributes[i - 1]].map { |a| index_name_for(a) }
|
48
|
+
end
|
44
49
|
end
|
45
|
-
end
|
46
50
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
def index_name_for(attribute)
|
52
|
+
if attribute.foreign_key?
|
53
|
+
attribute.name
|
54
|
+
else
|
55
|
+
attribute.name.singularize.foreign_key
|
56
|
+
end.to_sym
|
57
|
+
end
|
54
58
|
|
55
|
-
private
|
56
59
|
def attributes_with_index
|
57
60
|
attributes.select { |a| !a.reference? && a.has_index? }
|
58
61
|
end
|
@@ -60,7 +63,7 @@ module ActiveRecord
|
|
60
63
|
# A migration file name can only contain underscores (_), lowercase characters,
|
61
64
|
# and numbers 0-9. Any other file name will raise an IllegalMigrationNameError.
|
62
65
|
def validate_file_name!
|
63
|
-
unless
|
66
|
+
unless /^[_a-z0-9]+$/.match?(file_name)
|
64
67
|
raise IllegalMigrationNameError.new(file_name)
|
65
68
|
end
|
66
69
|
end
|