activerecord 4.2.11.3 → 5.0.7.2
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 +1638 -1132
- data/MIT-LICENSE +2 -2
- data/README.rdoc +7 -8
- data/examples/performance.rb +2 -3
- data/examples/simple.rb +0 -1
- data/lib/active_record.rb +7 -2
- data/lib/active_record/aggregations.rb +34 -21
- data/lib/active_record/association_relation.rb +7 -4
- data/lib/active_record/associations.rb +347 -218
- data/lib/active_record/associations/alias_tracker.rb +19 -16
- data/lib/active_record/associations/association.rb +22 -10
- data/lib/active_record/associations/association_scope.rb +75 -104
- data/lib/active_record/associations/belongs_to_association.rb +21 -32
- data/lib/active_record/associations/builder/association.rb +28 -34
- data/lib/active_record/associations/builder/belongs_to.rb +43 -18
- data/lib/active_record/associations/builder/collection_association.rb +7 -19
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +16 -11
- data/lib/active_record/associations/builder/has_many.rb +4 -4
- data/lib/active_record/associations/builder/has_one.rb +11 -6
- data/lib/active_record/associations/builder/singular_association.rb +13 -11
- data/lib/active_record/associations/collection_association.rb +85 -69
- data/lib/active_record/associations/collection_proxy.rb +104 -46
- data/lib/active_record/associations/foreign_association.rb +1 -1
- data/lib/active_record/associations/has_many_association.rb +21 -78
- data/lib/active_record/associations/has_many_through_association.rb +6 -47
- data/lib/active_record/associations/has_one_association.rb +12 -5
- data/lib/active_record/associations/join_dependency.rb +38 -22
- data/lib/active_record/associations/join_dependency/join_association.rb +15 -14
- data/lib/active_record/associations/join_dependency/join_part.rb +2 -2
- data/lib/active_record/associations/preloader.rb +14 -4
- data/lib/active_record/associations/preloader/association.rb +52 -71
- data/lib/active_record/associations/preloader/collection_association.rb +0 -7
- data/lib/active_record/associations/preloader/has_many_through.rb +1 -1
- data/lib/active_record/associations/preloader/has_one.rb +0 -8
- data/lib/active_record/associations/preloader/singular_association.rb +0 -1
- data/lib/active_record/associations/preloader/through_association.rb +36 -17
- data/lib/active_record/associations/singular_association.rb +13 -1
- data/lib/active_record/associations/through_association.rb +12 -4
- data/lib/active_record/attribute.rb +69 -19
- data/lib/active_record/attribute/user_provided_default.rb +28 -0
- data/lib/active_record/attribute_assignment.rb +19 -140
- data/lib/active_record/attribute_decorators.rb +6 -5
- data/lib/active_record/attribute_methods.rb +69 -44
- data/lib/active_record/attribute_methods/before_type_cast.rb +1 -1
- data/lib/active_record/attribute_methods/dirty.rb +46 -86
- data/lib/active_record/attribute_methods/primary_key.rb +16 -3
- data/lib/active_record/attribute_methods/query.rb +2 -2
- data/lib/active_record/attribute_methods/read.rb +31 -59
- data/lib/active_record/attribute_methods/serialization.rb +13 -16
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -14
- data/lib/active_record/attribute_methods/write.rb +13 -37
- data/lib/active_record/attribute_mutation_tracker.rb +70 -0
- data/lib/active_record/attribute_set.rb +32 -3
- data/lib/active_record/attribute_set/builder.rb +42 -16
- data/lib/active_record/attributes.rb +199 -81
- data/lib/active_record/autosave_association.rb +54 -17
- data/lib/active_record/base.rb +32 -23
- data/lib/active_record/callbacks.rb +39 -43
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +20 -8
- data/lib/active_record/collection_cache_key.rb +50 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +467 -189
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +66 -62
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +39 -4
- data/lib/active_record/connection_adapters/abstract/quoting.rb +86 -13
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +61 -39
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +236 -188
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +72 -17
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +407 -156
- data/lib/active_record/connection_adapters/abstract/transaction.rb +51 -34
- data/lib/active_record/connection_adapters/abstract_adapter.rb +177 -71
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +433 -399
- data/lib/active_record/connection_adapters/column.rb +28 -43
- data/lib/active_record/connection_adapters/connection_specification.rb +15 -27
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +22 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +50 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +108 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +70 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +51 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +67 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +93 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +25 -166
- data/lib/active_record/connection_adapters/postgresql/column.rb +33 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +18 -72
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +42 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +1 -6
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +37 -57
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +7 -22
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +13 -3
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +1 -26
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +0 -4
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +4 -4
- data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +31 -17
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +0 -4
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +56 -19
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +29 -10
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -79
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +47 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +250 -154
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +264 -170
- data/lib/active_record/connection_adapters/schema_cache.rb +36 -23
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +32 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +48 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +151 -194
- data/lib/active_record/connection_adapters/statement_pool.rb +31 -12
- data/lib/active_record/connection_handling.rb +37 -14
- data/lib/active_record/core.rb +92 -108
- data/lib/active_record/counter_cache.rb +13 -24
- data/lib/active_record/dynamic_matchers.rb +1 -20
- data/lib/active_record/enum.rb +116 -76
- data/lib/active_record/errors.rb +87 -48
- data/lib/active_record/explain.rb +20 -9
- data/lib/active_record/explain_registry.rb +1 -1
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +26 -5
- data/lib/active_record/fixtures.rb +77 -41
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +32 -40
- data/lib/active_record/integration.rb +17 -14
- data/lib/active_record/internal_metadata.rb +56 -0
- data/lib/active_record/legacy_yaml_adapter.rb +18 -2
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +15 -15
- data/lib/active_record/locking/pessimistic.rb +1 -1
- data/lib/active_record/log_subscriber.rb +48 -24
- data/lib/active_record/migration.rb +362 -111
- data/lib/active_record/migration/command_recorder.rb +59 -18
- data/lib/active_record/migration/compatibility.rb +126 -0
- data/lib/active_record/model_schema.rb +270 -73
- data/lib/active_record/nested_attributes.rb +58 -29
- data/lib/active_record/no_touching.rb +4 -0
- data/lib/active_record/null_relation.rb +16 -8
- data/lib/active_record/persistence.rb +152 -90
- data/lib/active_record/query_cache.rb +18 -23
- data/lib/active_record/querying.rb +12 -11
- data/lib/active_record/railtie.rb +23 -16
- data/lib/active_record/railties/controller_runtime.rb +1 -1
- data/lib/active_record/railties/databases.rake +52 -41
- data/lib/active_record/readonly_attributes.rb +1 -1
- data/lib/active_record/reflection.rb +302 -115
- data/lib/active_record/relation.rb +187 -120
- data/lib/active_record/relation/batches.rb +141 -36
- data/lib/active_record/relation/batches/batch_enumerator.rb +67 -0
- data/lib/active_record/relation/calculations.rb +92 -117
- data/lib/active_record/relation/delegation.rb +8 -20
- data/lib/active_record/relation/finder_methods.rb +173 -89
- data/lib/active_record/relation/from_clause.rb +32 -0
- data/lib/active_record/relation/merger.rb +16 -42
- data/lib/active_record/relation/predicate_builder.rb +120 -107
- data/lib/active_record/relation/predicate_builder/array_handler.rb +11 -16
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +88 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +17 -0
- data/lib/active_record/relation/predicate_builder/class_handler.rb +27 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +57 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +33 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
- data/lib/active_record/relation/query_attribute.rb +19 -0
- data/lib/active_record/relation/query_methods.rb +308 -244
- data/lib/active_record/relation/record_fetch_warning.rb +49 -0
- data/lib/active_record/relation/spawn_methods.rb +4 -7
- data/lib/active_record/relation/where_clause.rb +174 -0
- data/lib/active_record/relation/where_clause_factory.rb +38 -0
- data/lib/active_record/result.rb +11 -4
- data/lib/active_record/runtime_registry.rb +1 -1
- data/lib/active_record/sanitization.rb +105 -66
- data/lib/active_record/schema.rb +26 -22
- data/lib/active_record/schema_dumper.rb +54 -37
- data/lib/active_record/schema_migration.rb +11 -14
- data/lib/active_record/scoping.rb +34 -16
- data/lib/active_record/scoping/default.rb +28 -10
- data/lib/active_record/scoping/named.rb +59 -26
- data/lib/active_record/secure_token.rb +38 -0
- data/lib/active_record/serialization.rb +3 -5
- data/lib/active_record/statement_cache.rb +17 -15
- data/lib/active_record/store.rb +8 -3
- data/lib/active_record/suppressor.rb +58 -0
- data/lib/active_record/table_metadata.rb +69 -0
- data/lib/active_record/tasks/database_tasks.rb +66 -49
- data/lib/active_record/tasks/mysql_database_tasks.rb +6 -14
- data/lib/active_record/tasks/postgresql_database_tasks.rb +12 -3
- data/lib/active_record/tasks/sqlite_database_tasks.rb +5 -1
- data/lib/active_record/timestamp.rb +20 -9
- data/lib/active_record/touch_later.rb +63 -0
- data/lib/active_record/transactions.rb +139 -57
- data/lib/active_record/type.rb +66 -17
- data/lib/active_record/type/adapter_specific_registry.rb +130 -0
- data/lib/active_record/type/date.rb +2 -45
- data/lib/active_record/type/date_time.rb +2 -49
- data/lib/active_record/type/internal/abstract_json.rb +33 -0
- data/lib/active_record/type/internal/timezone.rb +15 -0
- data/lib/active_record/type/serialized.rb +15 -14
- data/lib/active_record/type/time.rb +10 -16
- data/lib/active_record/type/type_map.rb +4 -4
- data/lib/active_record/type_caster.rb +7 -0
- data/lib/active_record/type_caster/connection.rb +29 -0
- data/lib/active_record/type_caster/map.rb +19 -0
- data/lib/active_record/validations.rb +33 -32
- data/lib/active_record/validations/absence.rb +23 -0
- data/lib/active_record/validations/associated.rb +10 -3
- data/lib/active_record/validations/length.rb +24 -0
- data/lib/active_record/validations/presence.rb +11 -12
- data/lib/active_record/validations/uniqueness.rb +33 -33
- data/lib/rails/generators/active_record/migration.rb +15 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +8 -5
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +8 -3
- data/lib/rails/generators/active_record/migration/templates/migration.rb +8 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +33 -16
- data/lib/rails/generators/active_record/model/templates/application_record.rb +5 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +3 -0
- metadata +58 -34
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -498
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +0 -13
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -31
- data/lib/active_record/type/decimal.rb +0 -64
- data/lib/active_record/type/decimal_without_scale.rb +0 -11
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -59
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -40
- data/lib/active_record/type/text.rb +0 -11
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/unsigned_integer.rb +0 -15
- data/lib/active_record/type/value.rb +0 -110
@@ -37,23 +37,22 @@ module ActiveRecord
|
|
37
37
|
reflection = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
|
38
38
|
counter_name = reflection.counter_cache_column
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
connection.update stmt
|
40
|
+
unscoped.where(primary_key => object.id).update_all(
|
41
|
+
counter_name => object.send(counter_association).count(:all)
|
42
|
+
)
|
44
43
|
end
|
45
44
|
return true
|
46
45
|
end
|
47
46
|
|
48
47
|
# A generic "counter updater" implementation, intended primarily to be
|
49
|
-
# used by increment_counter and decrement_counter, but which may also
|
48
|
+
# used by #increment_counter and #decrement_counter, but which may also
|
50
49
|
# be useful on its own. It simply does a direct SQL update for the record
|
51
50
|
# with the given ID, altering the given hash of counters by the amount
|
52
51
|
# given by the corresponding value:
|
53
52
|
#
|
54
53
|
# ==== Parameters
|
55
54
|
#
|
56
|
-
# * +id+ - The id of the object you wish to update a counter on or an
|
55
|
+
# * +id+ - The id of the object you wish to update a counter on or an array of ids.
|
57
56
|
# * +counters+ - A Hash containing the names of the fields
|
58
57
|
# to update as keys and the amount to update the field by as values.
|
59
58
|
#
|
@@ -87,52 +86,42 @@ module ActiveRecord
|
|
87
86
|
# Increment a numeric field by one, via a direct SQL update.
|
88
87
|
#
|
89
88
|
# This method is used primarily for maintaining counter_cache columns that are
|
90
|
-
# used to store aggregate values. For example, a DiscussionBoard may cache
|
89
|
+
# used to store aggregate values. For example, a +DiscussionBoard+ may cache
|
91
90
|
# posts_count and comments_count to avoid running an SQL query to calculate the
|
92
91
|
# number of posts and comments there are, each time it is displayed.
|
93
92
|
#
|
94
93
|
# ==== Parameters
|
95
94
|
#
|
96
95
|
# * +counter_name+ - The name of the field that should be incremented.
|
97
|
-
# * +id+ - The id of the object that should be incremented or an
|
96
|
+
# * +id+ - The id of the object that should be incremented or an array of ids.
|
98
97
|
#
|
99
98
|
# ==== Examples
|
100
99
|
#
|
101
|
-
# # Increment the
|
102
|
-
# DiscussionBoard.increment_counter(:
|
100
|
+
# # Increment the posts_count column for the record with an id of 5
|
101
|
+
# DiscussionBoard.increment_counter(:posts_count, 5)
|
103
102
|
def increment_counter(counter_name, id)
|
104
103
|
update_counters(id, counter_name => 1)
|
105
104
|
end
|
106
105
|
|
107
106
|
# Decrement a numeric field by one, via a direct SQL update.
|
108
107
|
#
|
109
|
-
# This works the same as increment_counter but reduces the column value by
|
108
|
+
# This works the same as #increment_counter but reduces the column value by
|
110
109
|
# 1 instead of increasing it.
|
111
110
|
#
|
112
111
|
# ==== Parameters
|
113
112
|
#
|
114
113
|
# * +counter_name+ - The name of the field that should be decremented.
|
115
|
-
# * +id+ - The id of the object that should be decremented or an
|
114
|
+
# * +id+ - The id of the object that should be decremented or an array of ids.
|
116
115
|
#
|
117
116
|
# ==== Examples
|
118
117
|
#
|
119
|
-
# # Decrement the
|
120
|
-
# DiscussionBoard.decrement_counter(:
|
118
|
+
# # Decrement the posts_count column for the record with an id of 5
|
119
|
+
# DiscussionBoard.decrement_counter(:posts_count, 5)
|
121
120
|
def decrement_counter(counter_name, id)
|
122
121
|
update_counters(id, counter_name => -1)
|
123
122
|
end
|
124
123
|
end
|
125
124
|
|
126
|
-
protected
|
127
|
-
|
128
|
-
def actually_destroyed?
|
129
|
-
@_actually_destroyed
|
130
|
-
end
|
131
|
-
|
132
|
-
def clear_destroy_state
|
133
|
-
@_actually_destroyed = nil
|
134
|
-
end
|
135
|
-
|
136
125
|
private
|
137
126
|
|
138
127
|
def _create_record(*)
|
@@ -1,10 +1,5 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module DynamicMatchers #:nodoc:
|
3
|
-
# This code in this file seems to have a lot of indirection, but the indirection
|
4
|
-
# is there to provide extension points for the activerecord-deprecated_finders
|
5
|
-
# gem. When we stop supporting activerecord-deprecated_finders (from Rails 5),
|
6
|
-
# then we can remove the indirection.
|
7
|
-
|
8
3
|
def respond_to?(name, include_private = false)
|
9
4
|
if self == Base
|
10
5
|
super
|
@@ -72,26 +67,14 @@ module ActiveRecord
|
|
72
67
|
CODE
|
73
68
|
end
|
74
69
|
|
75
|
-
|
76
|
-
raise NotImplementedError
|
77
|
-
end
|
78
|
-
end
|
70
|
+
private
|
79
71
|
|
80
|
-
module Finder
|
81
|
-
# Extended in activerecord-deprecated_finders
|
82
72
|
def body
|
83
|
-
result
|
84
|
-
end
|
85
|
-
|
86
|
-
# Extended in activerecord-deprecated_finders
|
87
|
-
def result
|
88
73
|
"#{finder}(#{attributes_hash})"
|
89
74
|
end
|
90
75
|
|
91
76
|
# The parameters in the signature may have reserved Ruby words, in order
|
92
77
|
# to prevent errors, we start each param name with `_`.
|
93
|
-
#
|
94
|
-
# Extended in activerecord-deprecated_finders
|
95
78
|
def signature
|
96
79
|
attribute_names.map { |name| "_#{name}" }.join(', ')
|
97
80
|
end
|
@@ -109,7 +92,6 @@ module ActiveRecord
|
|
109
92
|
|
110
93
|
class FindBy < Method
|
111
94
|
Method.matchers << self
|
112
|
-
include Finder
|
113
95
|
|
114
96
|
def self.prefix
|
115
97
|
"find_by"
|
@@ -122,7 +104,6 @@ module ActiveRecord
|
|
122
104
|
|
123
105
|
class FindByBang < Method
|
124
106
|
Method.matchers << self
|
125
|
-
include Finder
|
126
107
|
|
127
108
|
def self.prefix
|
128
109
|
"find_by"
|
data/lib/active_record/enum.rb
CHANGED
@@ -31,6 +31,12 @@ module ActiveRecord
|
|
31
31
|
# Conversation.active
|
32
32
|
# Conversation.archived
|
33
33
|
#
|
34
|
+
# Of course, you can also query them directly if the scopes don't fit your
|
35
|
+
# needs:
|
36
|
+
#
|
37
|
+
# Conversation.where(status: [:active, :archived])
|
38
|
+
# Conversation.where.not(status: :active)
|
39
|
+
#
|
34
40
|
# You can set the default value from the database declaration, like:
|
35
41
|
#
|
36
42
|
# create_table :conversations do |t|
|
@@ -40,13 +46,13 @@ module ActiveRecord
|
|
40
46
|
# Good practice is to let the first declared status be the default.
|
41
47
|
#
|
42
48
|
# Finally, it's also possible to explicitly map the relation between attribute and
|
43
|
-
# database integer with a
|
49
|
+
# database integer with a hash:
|
44
50
|
#
|
45
51
|
# class Conversation < ActiveRecord::Base
|
46
52
|
# enum status: { active: 0, archived: 1 }
|
47
53
|
# end
|
48
54
|
#
|
49
|
-
# Note that when an
|
55
|
+
# Note that when an array is used, the implicit mapping from the values to database
|
50
56
|
# integers is derived from the order the values appear in the array. In the example,
|
51
57
|
# <tt>:active</tt> is mapped to +0+ as it's the first element, and <tt>:archived</tt>
|
52
58
|
# is mapped to +1+. In general, the +i+-th element is mapped to <tt>i-1</tt> in the
|
@@ -54,19 +60,39 @@ module ActiveRecord
|
|
54
60
|
#
|
55
61
|
# Therefore, once a value is added to the enum array, its position in the array must
|
56
62
|
# be maintained, and new values should only be added to the end of the array. To
|
57
|
-
# remove unused values, the explicit
|
63
|
+
# remove unused values, the explicit hash syntax should be used.
|
58
64
|
#
|
59
65
|
# In rare circumstances you might need to access the mapping directly.
|
60
66
|
# The mappings are exposed through a class method with the pluralized attribute
|
61
|
-
# name
|
67
|
+
# name, which return the mapping in a +HashWithIndifferentAccess+:
|
62
68
|
#
|
63
|
-
# Conversation.statuses
|
69
|
+
# Conversation.statuses[:active] # => 0
|
70
|
+
# Conversation.statuses["archived"] # => 1
|
64
71
|
#
|
65
|
-
# Use that class method when you need to know the ordinal value of an enum
|
72
|
+
# Use that class method when you need to know the ordinal value of an enum.
|
73
|
+
# For example, you can use that when manually building SQL strings:
|
66
74
|
#
|
67
75
|
# Conversation.where("status <> ?", Conversation.statuses[:archived])
|
68
76
|
#
|
69
|
-
#
|
77
|
+
# You can use the +:_prefix+ or +:_suffix+ options when you need to define
|
78
|
+
# multiple enums with same values. If the passed value is +true+, the methods
|
79
|
+
# are prefixed/suffixed with the name of the enum. It is also possible to
|
80
|
+
# supply a custom value:
|
81
|
+
#
|
82
|
+
# class Conversation < ActiveRecord::Base
|
83
|
+
# enum status: [:active, :archived], _suffix: true
|
84
|
+
# enum comments_status: [:active, :inactive], _prefix: :comments
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# With the above example, the bang and predicate methods along with the
|
88
|
+
# associated scopes are now prefixed and/or suffixed accordingly:
|
89
|
+
#
|
90
|
+
# conversation.active_status!
|
91
|
+
# conversation.archived_status? # => false
|
92
|
+
#
|
93
|
+
# conversation.comments_inactive!
|
94
|
+
# conversation.comments_active? # => false
|
95
|
+
|
70
96
|
module Enum
|
71
97
|
def self.extended(base) # :nodoc:
|
72
98
|
base.class_attribute(:defined_enums, instance_writer: false)
|
@@ -78,56 +104,96 @@ module ActiveRecord
|
|
78
104
|
super
|
79
105
|
end
|
80
106
|
|
107
|
+
class EnumType < Type::Value # :nodoc:
|
108
|
+
delegate :type, to: :subtype
|
109
|
+
|
110
|
+
def initialize(name, mapping, subtype)
|
111
|
+
@name = name
|
112
|
+
@mapping = mapping
|
113
|
+
@subtype = subtype
|
114
|
+
end
|
115
|
+
|
116
|
+
def cast(value)
|
117
|
+
return if value.blank?
|
118
|
+
|
119
|
+
if mapping.has_key?(value)
|
120
|
+
value.to_s
|
121
|
+
elsif mapping.has_value?(value)
|
122
|
+
mapping.key(value)
|
123
|
+
else
|
124
|
+
assert_valid_value(value)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def deserialize(value)
|
129
|
+
return if value.nil?
|
130
|
+
mapping.key(subtype.deserialize(value))
|
131
|
+
end
|
132
|
+
|
133
|
+
def serialize(value)
|
134
|
+
mapping.fetch(value, value)
|
135
|
+
end
|
136
|
+
|
137
|
+
def assert_valid_value(value)
|
138
|
+
unless value.blank? || mapping.has_key?(value) || mapping.has_value?(value)
|
139
|
+
raise ArgumentError, "'#{value}' is not a valid #{name}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
protected
|
144
|
+
|
145
|
+
attr_reader :name, :mapping, :subtype
|
146
|
+
end
|
147
|
+
|
81
148
|
def enum(definitions)
|
82
149
|
klass = self
|
150
|
+
enum_prefix = definitions.delete(:_prefix)
|
151
|
+
enum_suffix = definitions.delete(:_suffix)
|
83
152
|
definitions.each do |name, values|
|
84
153
|
# statuses = { }
|
85
154
|
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
86
155
|
name = name.to_sym
|
87
156
|
|
88
|
-
# def self.statuses statuses end
|
157
|
+
# def self.statuses() statuses end
|
89
158
|
detect_enum_conflict!(name, name.to_s.pluralize, true)
|
90
159
|
klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values }
|
91
160
|
|
92
|
-
|
93
|
-
|
94
|
-
klass.send(:detect_enum_conflict!, name, "#{name}=")
|
95
|
-
define_method("#{name}=") { |value|
|
96
|
-
if enum_values.has_key?(value) || value.blank?
|
97
|
-
self[name] = enum_values[value]
|
98
|
-
elsif enum_values.has_value?(value)
|
99
|
-
# Assigning a value directly is not a end-user feature, hence it's not documented.
|
100
|
-
# This is used internally to make building objects from the generated scopes work
|
101
|
-
# as expected, i.e. +Conversation.archived.build.archived?+ should be true.
|
102
|
-
self[name] = value
|
103
|
-
else
|
104
|
-
raise ArgumentError, "'#{value}' is not a valid #{name}"
|
105
|
-
end
|
106
|
-
}
|
107
|
-
|
108
|
-
# def status() statuses.key self[:status] end
|
109
|
-
klass.send(:detect_enum_conflict!, name, name)
|
110
|
-
define_method(name) { enum_values.key self[name] }
|
161
|
+
detect_enum_conflict!(name, name)
|
162
|
+
detect_enum_conflict!(name, "#{name}=")
|
111
163
|
|
112
|
-
|
113
|
-
|
114
|
-
|
164
|
+
attr = attribute_alias?(name) ? attribute_alias(name) : name
|
165
|
+
decorate_attribute_type(attr, :enum) do |subtype|
|
166
|
+
EnumType.new(attr, enum_values, subtype)
|
167
|
+
end
|
115
168
|
|
169
|
+
_enum_methods_module.module_eval do
|
116
170
|
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
117
171
|
pairs.each do |value, i|
|
172
|
+
if enum_prefix == true
|
173
|
+
prefix = "#{name}_"
|
174
|
+
elsif enum_prefix
|
175
|
+
prefix = "#{enum_prefix}_"
|
176
|
+
end
|
177
|
+
if enum_suffix == true
|
178
|
+
suffix = "_#{name}"
|
179
|
+
elsif enum_suffix
|
180
|
+
suffix = "_#{enum_suffix}"
|
181
|
+
end
|
182
|
+
|
183
|
+
value_method_name = "#{prefix}#{value}#{suffix}"
|
118
184
|
enum_values[value] = i
|
119
185
|
|
120
186
|
# def active?() status == 0 end
|
121
|
-
klass.send(:detect_enum_conflict!, name, "#{
|
122
|
-
define_method("#{
|
187
|
+
klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
|
188
|
+
define_method("#{value_method_name}?") { self[attr] == value.to_s }
|
123
189
|
|
124
190
|
# def active!() update! status: :active end
|
125
|
-
klass.send(:detect_enum_conflict!, name, "#{
|
126
|
-
define_method("#{
|
191
|
+
klass.send(:detect_enum_conflict!, name, "#{value_method_name}!")
|
192
|
+
define_method("#{value_method_name}!") { update!(attr => value) }
|
127
193
|
|
128
194
|
# scope :active, -> { where status: 0 }
|
129
|
-
klass.send(:detect_enum_conflict!, name,
|
130
|
-
klass.scope
|
195
|
+
klass.send(:detect_enum_conflict!, name, value_method_name, true)
|
196
|
+
klass.scope value_method_name, -> { where(attr => value) }
|
131
197
|
end
|
132
198
|
end
|
133
199
|
defined_enums[name.to_s] = enum_values
|
@@ -137,25 +203,7 @@ module ActiveRecord
|
|
137
203
|
private
|
138
204
|
def _enum_methods_module
|
139
205
|
@_enum_methods_module ||= begin
|
140
|
-
mod = Module.new
|
141
|
-
private
|
142
|
-
def save_changed_attribute(attr_name, old)
|
143
|
-
if (mapping = self.class.defined_enums[attr_name.to_s])
|
144
|
-
value = _read_attribute(attr_name)
|
145
|
-
if attribute_changed?(attr_name)
|
146
|
-
if mapping[old] == value
|
147
|
-
clear_attribute_changes([attr_name])
|
148
|
-
end
|
149
|
-
else
|
150
|
-
if old != value
|
151
|
-
set_attribute_was(attr_name, mapping.key(old))
|
152
|
-
end
|
153
|
-
end
|
154
|
-
else
|
155
|
-
super
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
206
|
+
mod = Module.new
|
159
207
|
include mod
|
160
208
|
mod
|
161
209
|
end
|
@@ -168,30 +216,22 @@ module ActiveRecord
|
|
168
216
|
|
169
217
|
def detect_enum_conflict!(enum_name, method_name, klass_method = false)
|
170
218
|
if klass_method && dangerous_class_method?(method_name)
|
171
|
-
|
172
|
-
enum: enum_name,
|
173
|
-
klass: self.name,
|
174
|
-
type: 'class',
|
175
|
-
method: method_name,
|
176
|
-
source: 'Active Record'
|
177
|
-
}
|
219
|
+
raise_conflict_error(enum_name, method_name, type: 'class')
|
178
220
|
elsif !klass_method && dangerous_attribute_method?(method_name)
|
179
|
-
|
180
|
-
enum: enum_name,
|
181
|
-
klass: self.name,
|
182
|
-
type: 'instance',
|
183
|
-
method: method_name,
|
184
|
-
source: 'Active Record'
|
185
|
-
}
|
221
|
+
raise_conflict_error(enum_name, method_name)
|
186
222
|
elsif !klass_method && method_defined_within?(method_name, _enum_methods_module, Module)
|
187
|
-
|
188
|
-
enum: enum_name,
|
189
|
-
klass: self.name,
|
190
|
-
type: 'instance',
|
191
|
-
method: method_name,
|
192
|
-
source: 'another enum'
|
193
|
-
}
|
223
|
+
raise_conflict_error(enum_name, method_name, source: 'another enum')
|
194
224
|
end
|
195
225
|
end
|
226
|
+
|
227
|
+
def raise_conflict_error(enum_name, method_name, type: 'instance', source: 'Active Record')
|
228
|
+
raise ArgumentError, ENUM_CONFLICT_MESSAGE % {
|
229
|
+
enum: enum_name,
|
230
|
+
klass: self.name,
|
231
|
+
type: type,
|
232
|
+
method: method_name,
|
233
|
+
source: source
|
234
|
+
}
|
235
|
+
end
|
196
236
|
end
|
197
237
|
end
|
data/lib/active_record/errors.rb
CHANGED
@@ -7,8 +7,10 @@ module ActiveRecord
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Raised when the single-table inheritance mechanism fails to locate the subclass
|
10
|
-
# (for example due to improper usage of column that
|
11
|
-
|
10
|
+
# (for example due to improper usage of column that
|
11
|
+
# {ActiveRecord::Base.inheritance_column}[rdoc-ref:ModelSchema::ClassMethods#inheritance_column]
|
12
|
+
# points to).
|
13
|
+
class SubclassNotFound < ActiveRecordError
|
12
14
|
end
|
13
15
|
|
14
16
|
# Raised when an object assigned to an association has an incorrect type.
|
@@ -40,27 +42,40 @@ module ActiveRecord
|
|
40
42
|
class AdapterNotFound < ActiveRecordError
|
41
43
|
end
|
42
44
|
|
43
|
-
# Raised when connection to the database could not been established (for
|
44
|
-
#
|
45
|
+
# Raised when connection to the database could not been established (for example when
|
46
|
+
# {ActiveRecord::Base.connection=}[rdoc-ref:ConnectionHandling#connection]
|
47
|
+
# is given a nil object).
|
45
48
|
class ConnectionNotEstablished < ActiveRecordError
|
46
49
|
end
|
47
50
|
|
48
|
-
# Raised when Active Record cannot find record by given id or set of ids.
|
51
|
+
# Raised when Active Record cannot find a record by given id or set of ids.
|
49
52
|
class RecordNotFound < ActiveRecordError
|
53
|
+
attr_reader :model, :primary_key, :id
|
54
|
+
|
55
|
+
def initialize(message = nil, model = nil, primary_key = nil, id = nil)
|
56
|
+
@primary_key = primary_key
|
57
|
+
@model = model
|
58
|
+
@id = id
|
59
|
+
|
60
|
+
super(message)
|
61
|
+
end
|
50
62
|
end
|
51
63
|
|
52
|
-
# Raised by ActiveRecord::Base
|
53
|
-
#
|
64
|
+
# Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
|
65
|
+
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!]
|
66
|
+
# methods when a record is invalid and can not be saved.
|
54
67
|
class RecordNotSaved < ActiveRecordError
|
55
68
|
attr_reader :record
|
56
69
|
|
57
|
-
def initialize(message, record = nil)
|
70
|
+
def initialize(message = nil, record = nil)
|
58
71
|
@record = record
|
59
72
|
super(message)
|
60
73
|
end
|
61
74
|
end
|
62
75
|
|
63
|
-
# Raised by ActiveRecord::Base
|
76
|
+
# Raised by {ActiveRecord::Base#destroy!}[rdoc-ref:Persistence#destroy!]
|
77
|
+
# when a call to {#destroy}[rdoc-ref:Persistence#destroy!]
|
78
|
+
# would return false.
|
64
79
|
#
|
65
80
|
# begin
|
66
81
|
# complex_operation_that_internally_calls_destroy!
|
@@ -71,7 +86,7 @@ module ActiveRecord
|
|
71
86
|
class RecordNotDestroyed < ActiveRecordError
|
72
87
|
attr_reader :record
|
73
88
|
|
74
|
-
def initialize(message, record = nil)
|
89
|
+
def initialize(message = nil, record = nil)
|
75
90
|
@record = record
|
76
91
|
super(message)
|
77
92
|
end
|
@@ -79,18 +94,26 @@ module ActiveRecord
|
|
79
94
|
|
80
95
|
# Superclass for all database execution errors.
|
81
96
|
#
|
82
|
-
# Wraps the underlying database error as +
|
97
|
+
# Wraps the underlying database error as +cause+.
|
83
98
|
class StatementInvalid < ActiveRecordError
|
84
|
-
attr_reader :original_exception
|
85
99
|
|
86
|
-
def initialize(message, original_exception = nil)
|
87
|
-
|
88
|
-
|
100
|
+
def initialize(message = nil, original_exception = nil)
|
101
|
+
if original_exception
|
102
|
+
ActiveSupport::Deprecation.warn("Passing #original_exception is deprecated and has no effect. " \
|
103
|
+
"Exceptions will automatically capture the original exception.", caller)
|
104
|
+
end
|
105
|
+
|
106
|
+
super(message || $!.try(:message))
|
107
|
+
end
|
108
|
+
|
109
|
+
def original_exception
|
110
|
+
ActiveSupport::Deprecation.warn("#original_exception is deprecated. Use #cause instead.", caller)
|
111
|
+
cause
|
89
112
|
end
|
90
113
|
end
|
91
114
|
|
92
115
|
# Defunct wrapper class kept for compatibility.
|
93
|
-
#
|
116
|
+
# StatementInvalid wraps the original exception now.
|
94
117
|
class WrappedDatabaseException < StatementInvalid
|
95
118
|
end
|
96
119
|
|
@@ -102,9 +125,13 @@ module ActiveRecord
|
|
102
125
|
class InvalidForeignKey < WrappedDatabaseException
|
103
126
|
end
|
104
127
|
|
128
|
+
# Raised when a record cannot be inserted or updated because a value too long for a column type.
|
129
|
+
class ValueTooLong < StatementInvalid
|
130
|
+
end
|
131
|
+
|
105
132
|
# Raised when number of bind variables in statement given to +:condition+ key
|
106
|
-
# (for example, when using
|
107
|
-
# values supplied.
|
133
|
+
# (for example, when using {ActiveRecord::Base.find}[rdoc-ref:FinderMethods#find] method)
|
134
|
+
# does not match number of expected values supplied.
|
108
135
|
#
|
109
136
|
# For example, when there are two placeholders with only one value supplied:
|
110
137
|
#
|
@@ -116,6 +143,11 @@ module ActiveRecord
|
|
116
143
|
class NoDatabaseError < StatementInvalid
|
117
144
|
end
|
118
145
|
|
146
|
+
# Raised when Postgres returns 'cached plan must not change result type' and
|
147
|
+
# we cannot retry gracefully (e.g. inside a transaction)
|
148
|
+
class PreparedStatementCacheExpired < StatementInvalid
|
149
|
+
end
|
150
|
+
|
119
151
|
# Raised on attempt to save stale record. Record is stale when it's being saved in another query after
|
120
152
|
# instantiation, for example, when two users edit the same wiki page and one starts editing and saves
|
121
153
|
# the page before the other.
|
@@ -125,16 +157,22 @@ module ActiveRecord
|
|
125
157
|
class StaleObjectError < ActiveRecordError
|
126
158
|
attr_reader :record, :attempted_action
|
127
159
|
|
128
|
-
def initialize(record, attempted_action)
|
129
|
-
|
130
|
-
|
131
|
-
|
160
|
+
def initialize(record = nil, attempted_action = nil)
|
161
|
+
if record && attempted_action
|
162
|
+
@record = record
|
163
|
+
@attempted_action = attempted_action
|
164
|
+
super("Attempted to #{attempted_action} a stale object: #{record.class.name}.")
|
165
|
+
else
|
166
|
+
super("Stale object error.")
|
167
|
+
end
|
132
168
|
end
|
133
169
|
|
134
170
|
end
|
135
171
|
|
136
172
|
# Raised when association is being configured improperly or user tries to use
|
137
|
-
# offset and limit together with
|
173
|
+
# offset and limit together with
|
174
|
+
# {ActiveRecord::Base.has_many}[rdoc-ref:Associations::ClassMethods#has_many] or
|
175
|
+
# {ActiveRecord::Base.has_and_belongs_to_many}[rdoc-ref:Associations::ClassMethods#has_and_belongs_to_many]
|
138
176
|
# associations.
|
139
177
|
class ConfigurationError < ActiveRecordError
|
140
178
|
end
|
@@ -143,9 +181,10 @@ module ActiveRecord
|
|
143
181
|
class ReadOnlyRecord < ActiveRecordError
|
144
182
|
end
|
145
183
|
|
146
|
-
# ActiveRecord::Transactions::ClassMethods
|
147
|
-
# to distinguish a deliberate rollback from other exceptional situations.
|
148
|
-
# Normally, raising an exception will cause the
|
184
|
+
# {ActiveRecord::Base.transaction}[rdoc-ref:Transactions::ClassMethods#transaction]
|
185
|
+
# uses this exception to distinguish a deliberate rollback from other exceptional situations.
|
186
|
+
# Normally, raising an exception will cause the
|
187
|
+
# {.transaction}[rdoc-ref:Transactions::ClassMethods#transaction] method to rollback
|
149
188
|
# the database transaction *and* pass on the exception. But if you raise an
|
150
189
|
# ActiveRecord::Rollback exception, then the database transaction will be rolled back,
|
151
190
|
# without passing on the exception.
|
@@ -179,38 +218,29 @@ module ActiveRecord
|
|
179
218
|
end
|
180
219
|
|
181
220
|
# Raised when unknown attributes are supplied via mass assignment.
|
182
|
-
|
183
|
-
|
184
|
-
attr_reader :record, :attribute
|
185
|
-
|
186
|
-
def initialize(record, attribute)
|
187
|
-
@record = record
|
188
|
-
@attribute = attribute.to_s
|
189
|
-
super("unknown attribute '#{attribute}' for #{@record.class}.")
|
190
|
-
end
|
191
|
-
|
192
|
-
end
|
221
|
+
UnknownAttributeError = ActiveModel::UnknownAttributeError
|
193
222
|
|
194
223
|
# Raised when an error occurred while doing a mass assignment to an attribute through the
|
195
|
-
#
|
196
|
-
# offending attribute.
|
224
|
+
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=] method.
|
225
|
+
# The exception has an +attribute+ property that is the name of the offending attribute.
|
197
226
|
class AttributeAssignmentError < ActiveRecordError
|
198
227
|
attr_reader :exception, :attribute
|
199
228
|
|
200
|
-
def initialize(message, exception, attribute)
|
229
|
+
def initialize(message = nil, exception = nil, attribute = nil)
|
201
230
|
super(message)
|
202
231
|
@exception = exception
|
203
232
|
@attribute = attribute
|
204
233
|
end
|
205
234
|
end
|
206
235
|
|
207
|
-
# Raised when there are multiple errors while doing a mass assignment through the
|
236
|
+
# Raised when there are multiple errors while doing a mass assignment through the
|
237
|
+
# {ActiveRecord::Base#attributes=}[rdoc-ref:AttributeAssignment#attributes=]
|
208
238
|
# method. The exception has an +errors+ property that contains an array of AttributeAssignmentError
|
209
239
|
# objects, each corresponding to the error while assigning to an attribute.
|
210
240
|
class MultiparameterAssignmentErrors < ActiveRecordError
|
211
241
|
attr_reader :errors
|
212
242
|
|
213
|
-
def initialize(errors)
|
243
|
+
def initialize(errors = nil)
|
214
244
|
@errors = errors
|
215
245
|
end
|
216
246
|
end
|
@@ -219,11 +249,15 @@ module ActiveRecord
|
|
219
249
|
class UnknownPrimaryKey < ActiveRecordError
|
220
250
|
attr_reader :model
|
221
251
|
|
222
|
-
def initialize(model, description = nil)
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
252
|
+
def initialize(model = nil, description = nil)
|
253
|
+
if model
|
254
|
+
message = "Unknown primary key for table #{model.table_name} in model #{model}."
|
255
|
+
message += "\n#{description}" if description
|
256
|
+
@model = model
|
257
|
+
super(message)
|
258
|
+
else
|
259
|
+
super("Unknown primary key.")
|
260
|
+
end
|
227
261
|
end
|
228
262
|
end
|
229
263
|
|
@@ -247,7 +281,12 @@ module ActiveRecord
|
|
247
281
|
# * You are joining an existing open transaction
|
248
282
|
# * You are creating a nested (savepoint) transaction
|
249
283
|
#
|
250
|
-
# The
|
284
|
+
# The mysql2 and postgresql adapters support setting the transaction isolation level.
|
251
285
|
class TransactionIsolationError < ActiveRecordError
|
252
286
|
end
|
287
|
+
|
288
|
+
# IrreversibleOrderError is raised when a relation's order is too complex for
|
289
|
+
# +reverse_order+ to automatically reverse.
|
290
|
+
class IrreversibleOrderError < ActiveRecordError
|
291
|
+
end
|
253
292
|
end
|