activerecord 3.2.19 → 5.0.0

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.

Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1715 -604
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +40 -45
  5. data/examples/performance.rb +33 -22
  6. data/examples/simple.rb +3 -4
  7. data/lib/active_record/aggregations.rb +76 -51
  8. data/lib/active_record/association_relation.rb +35 -0
  9. data/lib/active_record/associations/alias_tracker.rb +54 -40
  10. data/lib/active_record/associations/association.rb +76 -56
  11. data/lib/active_record/associations/association_scope.rb +125 -93
  12. data/lib/active_record/associations/belongs_to_association.rb +57 -28
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
  14. data/lib/active_record/associations/builder/association.rb +120 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +115 -62
  16. data/lib/active_record/associations/builder/collection_association.rb +61 -53
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +117 -43
  18. data/lib/active_record/associations/builder/has_many.rb +9 -65
  19. data/lib/active_record/associations/builder/has_one.rb +18 -52
  20. data/lib/active_record/associations/builder/singular_association.rb +18 -19
  21. data/lib/active_record/associations/collection_association.rb +268 -186
  22. data/lib/active_record/associations/collection_proxy.rb +1003 -63
  23. data/lib/active_record/associations/foreign_association.rb +11 -0
  24. data/lib/active_record/associations/has_many_association.rb +81 -41
  25. data/lib/active_record/associations/has_many_through_association.rb +76 -55
  26. data/lib/active_record/associations/has_one_association.rb +51 -21
  27. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  28. data/lib/active_record/associations/join_dependency/join_association.rb +83 -108
  29. data/lib/active_record/associations/join_dependency/join_base.rb +7 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +30 -37
  31. data/lib/active_record/associations/join_dependency.rb +239 -155
  32. data/lib/active_record/associations/preloader/association.rb +97 -62
  33. data/lib/active_record/associations/preloader/collection_association.rb +2 -8
  34. data/lib/active_record/associations/preloader/has_many_through.rb +7 -3
  35. data/lib/active_record/associations/preloader/has_one.rb +0 -8
  36. data/lib/active_record/associations/preloader/singular_association.rb +3 -3
  37. data/lib/active_record/associations/preloader/through_association.rb +75 -33
  38. data/lib/active_record/associations/preloader.rb +111 -79
  39. data/lib/active_record/associations/singular_association.rb +35 -13
  40. data/lib/active_record/associations/through_association.rb +41 -19
  41. data/lib/active_record/associations.rb +727 -501
  42. data/lib/active_record/attribute/user_provided_default.rb +28 -0
  43. data/lib/active_record/attribute.rb +213 -0
  44. data/lib/active_record/attribute_assignment.rb +32 -162
  45. data/lib/active_record/attribute_decorators.rb +67 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +52 -7
  47. data/lib/active_record/attribute_methods/dirty.rb +101 -61
  48. data/lib/active_record/attribute_methods/primary_key.rb +50 -36
  49. data/lib/active_record/attribute_methods/query.rb +7 -6
  50. data/lib/active_record/attribute_methods/read.rb +56 -117
  51. data/lib/active_record/attribute_methods/serialization.rb +43 -96
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +93 -42
  53. data/lib/active_record/attribute_methods/write.rb +34 -45
  54. data/lib/active_record/attribute_methods.rb +333 -144
  55. data/lib/active_record/attribute_mutation_tracker.rb +70 -0
  56. data/lib/active_record/attribute_set/builder.rb +108 -0
  57. data/lib/active_record/attribute_set.rb +108 -0
  58. data/lib/active_record/attributes.rb +265 -0
  59. data/lib/active_record/autosave_association.rb +285 -223
  60. data/lib/active_record/base.rb +95 -490
  61. data/lib/active_record/callbacks.rb +95 -61
  62. data/lib/active_record/coders/json.rb +13 -0
  63. data/lib/active_record/coders/yaml_column.rb +28 -19
  64. data/lib/active_record/collection_cache_key.rb +40 -0
  65. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +724 -277
  66. data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
  67. data/lib/active_record/connection_adapters/abstract/database_statements.rb +199 -192
  68. data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -26
  69. data/lib/active_record/connection_adapters/abstract/quoting.rb +140 -57
  70. data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
  71. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +147 -0
  72. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +419 -276
  73. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +105 -0
  74. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +963 -276
  75. data/lib/active_record/connection_adapters/abstract/transaction.rb +232 -0
  76. data/lib/active_record/connection_adapters/abstract_adapter.rb +397 -106
  77. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +643 -342
  78. data/lib/active_record/connection_adapters/column.rb +30 -259
  79. data/lib/active_record/connection_adapters/connection_specification.rb +263 -0
  80. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +22 -0
  81. data/lib/active_record/connection_adapters/mysql/column.rb +50 -0
  82. data/lib/active_record/connection_adapters/mysql/database_statements.rb +125 -0
  83. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +70 -0
  84. data/lib/active_record/connection_adapters/mysql/quoting.rb +51 -0
  85. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +67 -0
  86. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +93 -0
  87. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -0
  88. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +32 -0
  89. data/lib/active_record/connection_adapters/mysql2_adapter.rb +47 -196
  90. data/lib/active_record/connection_adapters/postgresql/column.rb +15 -0
  91. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +170 -0
  92. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +42 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +70 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +48 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +21 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +10 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +39 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +50 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +93 -0
  109. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
  110. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
  111. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  112. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  113. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  114. data/lib/active_record/connection_adapters/postgresql/oid.rb +31 -0
  115. data/lib/active_record/connection_adapters/postgresql/quoting.rb +116 -0
  116. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +49 -0
  117. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +180 -0
  118. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +47 -0
  119. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +682 -0
  120. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +35 -0
  121. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  122. data/lib/active_record/connection_adapters/postgresql_adapter.rb +558 -1039
  123. data/lib/active_record/connection_adapters/schema_cache.rb +74 -36
  124. data/lib/active_record/connection_adapters/sql_type_metadata.rb +32 -0
  125. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +19 -0
  126. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +48 -0
  127. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
  128. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +538 -24
  129. data/lib/active_record/connection_adapters/statement_pool.rb +31 -12
  130. data/lib/active_record/connection_handling.rb +155 -0
  131. data/lib/active_record/core.rb +561 -0
  132. data/lib/active_record/counter_cache.rb +146 -105
  133. data/lib/active_record/dynamic_matchers.rb +101 -64
  134. data/lib/active_record/enum.rb +234 -0
  135. data/lib/active_record/errors.rb +153 -56
  136. data/lib/active_record/explain.rb +15 -63
  137. data/lib/active_record/explain_registry.rb +30 -0
  138. data/lib/active_record/explain_subscriber.rb +10 -6
  139. data/lib/active_record/fixture_set/file.rb +77 -0
  140. data/lib/active_record/fixtures.rb +355 -232
  141. data/lib/active_record/gem_version.rb +15 -0
  142. data/lib/active_record/inheritance.rb +144 -79
  143. data/lib/active_record/integration.rb +66 -13
  144. data/lib/active_record/internal_metadata.rb +56 -0
  145. data/lib/active_record/legacy_yaml_adapter.rb +46 -0
  146. data/lib/active_record/locale/en.yml +9 -1
  147. data/lib/active_record/locking/optimistic.rb +77 -56
  148. data/lib/active_record/locking/pessimistic.rb +6 -6
  149. data/lib/active_record/log_subscriber.rb +53 -28
  150. data/lib/active_record/migration/command_recorder.rb +166 -33
  151. data/lib/active_record/migration/compatibility.rb +126 -0
  152. data/lib/active_record/migration/join_table.rb +15 -0
  153. data/lib/active_record/migration.rb +792 -264
  154. data/lib/active_record/model_schema.rb +192 -130
  155. data/lib/active_record/nested_attributes.rb +238 -145
  156. data/lib/active_record/no_touching.rb +52 -0
  157. data/lib/active_record/null_relation.rb +89 -0
  158. data/lib/active_record/persistence.rb +357 -157
  159. data/lib/active_record/query_cache.rb +22 -43
  160. data/lib/active_record/querying.rb +34 -23
  161. data/lib/active_record/railtie.rb +88 -48
  162. data/lib/active_record/railties/console_sandbox.rb +3 -4
  163. data/lib/active_record/railties/controller_runtime.rb +5 -4
  164. data/lib/active_record/railties/databases.rake +170 -422
  165. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  166. data/lib/active_record/readonly_attributes.rb +2 -5
  167. data/lib/active_record/reflection.rb +715 -189
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +67 -0
  169. data/lib/active_record/relation/batches.rb +203 -50
  170. data/lib/active_record/relation/calculations.rb +203 -194
  171. data/lib/active_record/relation/delegation.rb +103 -25
  172. data/lib/active_record/relation/finder_methods.rb +457 -261
  173. data/lib/active_record/relation/from_clause.rb +32 -0
  174. data/lib/active_record/relation/merger.rb +167 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +43 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +88 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +17 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +17 -0
  179. data/lib/active_record/relation/predicate_builder/class_handler.rb +27 -0
  180. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +57 -0
  181. data/lib/active_record/relation/predicate_builder/range_handler.rb +33 -0
  182. data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
  183. data/lib/active_record/relation/predicate_builder.rb +153 -48
  184. data/lib/active_record/relation/query_attribute.rb +19 -0
  185. data/lib/active_record/relation/query_methods.rb +1019 -194
  186. data/lib/active_record/relation/record_fetch_warning.rb +49 -0
  187. data/lib/active_record/relation/spawn_methods.rb +46 -150
  188. data/lib/active_record/relation/where_clause.rb +174 -0
  189. data/lib/active_record/relation/where_clause_factory.rb +38 -0
  190. data/lib/active_record/relation.rb +450 -245
  191. data/lib/active_record/result.rb +104 -12
  192. data/lib/active_record/runtime_registry.rb +22 -0
  193. data/lib/active_record/sanitization.rb +120 -94
  194. data/lib/active_record/schema.rb +28 -18
  195. data/lib/active_record/schema_dumper.rb +141 -74
  196. data/lib/active_record/schema_migration.rb +50 -0
  197. data/lib/active_record/scoping/default.rb +64 -57
  198. data/lib/active_record/scoping/named.rb +93 -108
  199. data/lib/active_record/scoping.rb +73 -121
  200. data/lib/active_record/secure_token.rb +38 -0
  201. data/lib/active_record/serialization.rb +7 -5
  202. data/lib/active_record/statement_cache.rb +113 -0
  203. data/lib/active_record/store.rb +173 -15
  204. data/lib/active_record/suppressor.rb +58 -0
  205. data/lib/active_record/table_metadata.rb +68 -0
  206. data/lib/active_record/tasks/database_tasks.rb +313 -0
  207. data/lib/active_record/tasks/mysql_database_tasks.rb +151 -0
  208. data/lib/active_record/tasks/postgresql_database_tasks.rb +110 -0
  209. data/lib/active_record/tasks/sqlite_database_tasks.rb +59 -0
  210. data/lib/active_record/timestamp.rb +42 -24
  211. data/lib/active_record/touch_later.rb +58 -0
  212. data/lib/active_record/transactions.rb +233 -105
  213. data/lib/active_record/type/adapter_specific_registry.rb +130 -0
  214. data/lib/active_record/type/date.rb +7 -0
  215. data/lib/active_record/type/date_time.rb +7 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
  217. data/lib/active_record/type/internal/abstract_json.rb +29 -0
  218. data/lib/active_record/type/internal/timezone.rb +15 -0
  219. data/lib/active_record/type/serialized.rb +63 -0
  220. data/lib/active_record/type/time.rb +20 -0
  221. data/lib/active_record/type/type_map.rb +64 -0
  222. data/lib/active_record/type.rb +72 -0
  223. data/lib/active_record/type_caster/connection.rb +29 -0
  224. data/lib/active_record/type_caster/map.rb +19 -0
  225. data/lib/active_record/type_caster.rb +7 -0
  226. data/lib/active_record/validations/absence.rb +23 -0
  227. data/lib/active_record/validations/associated.rb +33 -18
  228. data/lib/active_record/validations/length.rb +24 -0
  229. data/lib/active_record/validations/presence.rb +66 -0
  230. data/lib/active_record/validations/uniqueness.rb +128 -68
  231. data/lib/active_record/validations.rb +48 -40
  232. data/lib/active_record/version.rb +5 -7
  233. data/lib/active_record.rb +71 -47
  234. data/lib/rails/generators/active_record/migration/migration_generator.rb +56 -8
  235. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +24 -0
  236. data/lib/rails/generators/active_record/migration/templates/migration.rb +28 -16
  237. data/lib/rails/generators/active_record/migration.rb +18 -8
  238. data/lib/rails/generators/active_record/model/model_generator.rb +38 -16
  239. data/lib/rails/generators/active_record/model/templates/application_record.rb +5 -0
  240. data/lib/rails/generators/active_record/model/templates/model.rb +7 -6
  241. data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
  242. data/lib/rails/generators/active_record.rb +3 -11
  243. metadata +188 -134
  244. data/examples/associations.png +0 -0
  245. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  246. data/lib/active_record/associations/join_helper.rb +0 -55
  247. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  248. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  249. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
  250. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
  251. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
  252. data/lib/active_record/dynamic_finder_match.rb +0 -68
  253. data/lib/active_record/dynamic_scope_match.rb +0 -23
  254. data/lib/active_record/fixtures/file.rb +0 -65
  255. data/lib/active_record/identity_map.rb +0 -162
  256. data/lib/active_record/observer.rb +0 -121
  257. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  258. data/lib/active_record/session_store.rb +0 -360
  259. data/lib/active_record/test_case.rb +0 -73
  260. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  261. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  262. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  263. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  264. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,42 +1,40 @@
1
- require 'active_support/core_ext/array/wrap'
2
-
3
1
  module ActiveRecord
4
2
  module Validations
5
- class UniquenessValidator < ActiveModel::EachValidator
3
+ class UniquenessValidator < ActiveModel::EachValidator # :nodoc:
6
4
  def initialize(options)
7
- super(options.reverse_merge(:case_sensitive => true))
8
- end
9
-
10
- # Unfortunately, we have to tie Uniqueness validators to a class.
11
- def setup(klass)
12
- @klass = klass
5
+ if options[:conditions] && !options[:conditions].respond_to?(:call)
6
+ raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
7
+ "Pass a callable instead: `conditions: -> { where(approved: true) }`"
8
+ end
9
+ super({ case_sensitive: true }.merge!(options))
10
+ @klass = options[:class]
13
11
  end
14
12
 
15
13
  def validate_each(record, attribute, value)
16
14
  finder_class = find_finder_class_for(record)
17
15
  table = finder_class.arel_table
18
-
19
- coder = record.class.serialized_attributes[attribute.to_s]
20
-
21
- if value && coder
22
- value = coder.dump value
23
- end
16
+ value = map_enum_attribute(finder_class, attribute, value)
24
17
 
25
18
  relation = build_relation(finder_class, table, attribute, value)
26
- relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
27
-
28
- Array.wrap(options[:scope]).each do |scope_item|
29
- scope_value = record.read_attribute(scope_item)
30
- relation = relation.and(table[scope_item].eq(scope_value))
19
+ if record.persisted?
20
+ if finder_class.primary_key
21
+ relation = relation.where.not(finder_class.primary_key => record.id_was || record.id)
22
+ else
23
+ raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
24
+ end
31
25
  end
26
+ relation = scope_relation(record, table, relation)
27
+ relation = relation.merge(options[:conditions]) if options[:conditions]
28
+
29
+ if relation.exists?
30
+ error_options = options.except(:case_sensitive, :scope, :conditions)
31
+ error_options[:value] = value
32
32
 
33
- if finder_class.unscoped.where(relation).exists?
34
- record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
33
+ record.errors.add(attribute, :taken, error_options)
35
34
  end
36
35
  end
37
36
 
38
37
  protected
39
-
40
38
  # The check for an existing value should be run from a class that
41
39
  # isn't abstract. This means working down from the current class
42
40
  # (self), to the first non-abstract class. Since classes don't know
@@ -46,71 +44,134 @@ module ActiveRecord
46
44
  class_hierarchy = [record.class]
47
45
 
48
46
  while class_hierarchy.first != @klass
49
- class_hierarchy.insert(0, class_hierarchy.first.superclass)
47
+ class_hierarchy.unshift(class_hierarchy.first.superclass)
50
48
  end
51
49
 
52
50
  class_hierarchy.detect { |klass| !klass.abstract_class? }
53
51
  end
54
52
 
55
53
  def build_relation(klass, table, attribute, value) #:nodoc:
56
- column = klass.columns_hash[attribute.to_s]
57
- value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if value && column.text?
54
+ if reflection = klass._reflect_on_association(attribute)
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
58
65
 
59
- if !options[:case_sensitive] && value && column.text?
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?
60
72
  # will use SQL LOWER function before comparison, unless it detects a case insensitive collation
61
- relation = klass.connection.case_insensitive_comparison(table, attribute, column, value)
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)
62
79
  else
63
- value = klass.connection.case_sensitive_modifier(value) if value
64
- relation = table[attribute].eq(value)
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
85
+ end
86
+
87
+ def scope_relation(record, table, relation)
88
+ Array(options[:scope]).each do |scope_item|
89
+ if reflection = record.class._reflect_on_association(scope_item)
90
+ scope_value = record.send(reflection.foreign_key)
91
+ scope_item = reflection.foreign_key
92
+ else
93
+ scope_value = record._read_attribute(scope_item)
94
+ end
95
+ relation = relation.where(scope_item => scope_value)
65
96
  end
66
97
 
67
98
  relation
68
99
  end
100
+
101
+ def map_enum_attribute(klass, attribute, value)
102
+ mapping = klass.defined_enums[attribute.to_s]
103
+ value = mapping[value] if value && mapping
104
+ value
105
+ end
69
106
  end
70
107
 
71
108
  module ClassMethods
72
- # Validates whether the value of the specified attributes are unique across the system.
73
- # Useful for making sure that only one user
109
+ # Validates whether the value of the specified attributes are unique
110
+ # across the system. Useful for making sure that only one user
74
111
  # can be named "davidhh".
75
112
  #
76
113
  # class Person < ActiveRecord::Base
77
114
  # validates_uniqueness_of :user_name
78
115
  # end
79
116
  #
80
- # It can also validate whether the value of the specified attributes are unique based on a scope parameter:
117
+ # It can also validate whether the value of the specified attributes are
118
+ # unique based on a <tt>:scope</tt> parameter:
81
119
  #
82
120
  # class Person < ActiveRecord::Base
83
- # validates_uniqueness_of :user_name, :scope => :account_id
121
+ # validates_uniqueness_of :user_name, scope: :account_id
84
122
  # end
85
123
  #
86
- # Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
87
- # per semester for a particular class.
124
+ # Or even multiple scope parameters. For example, making sure that a
125
+ # teacher can only be on the schedule once per semester for a particular
126
+ # class.
88
127
  #
89
128
  # class TeacherSchedule < ActiveRecord::Base
90
- # validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]
129
+ # validates_uniqueness_of :teacher_id, scope: [:semester_id, :class_id]
130
+ # end
131
+ #
132
+ # It is also possible to limit the uniqueness constraint to a set of
133
+ # records matching certain conditions. In this example archived articles
134
+ # are not being taken into consideration when validating uniqueness
135
+ # of the title attribute:
136
+ #
137
+ # class Article < ActiveRecord::Base
138
+ # validates_uniqueness_of :title, conditions: -> { where.not(status: 'archived') }
91
139
  # end
92
140
  #
93
- # When the record is created, a check is performed to make sure that no record exists in the database
94
- # with the given value for the specified attribute (that maps to a column). When the record is updated,
141
+ # When the record is created, a check is performed to make sure that no
142
+ # record exists in the database with the given value for the specified
143
+ # attribute (that maps to a column). When the record is updated,
95
144
  # the same check is made but disregarding the record itself.
96
145
  #
97
146
  # Configuration options:
98
- # * <tt>:message</tt> - Specifies a custom error message (default is: "has already been taken").
99
- # * <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
100
- # * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (+true+ by default).
101
- # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
102
- # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
103
- # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
104
- # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
105
- # The method, proc or string should return or evaluate to a true or false value.
106
- # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
107
- # not occur (e.g. <tt>:unless => :skip_validation</tt>, or
108
- # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, proc or string should
109
- # return or evaluate to a true or false value.
147
+ #
148
+ # * <tt>:message</tt> - Specifies a custom error message (default is:
149
+ # "has already been taken").
150
+ # * <tt>:scope</tt> - One or more columns by which to limit the scope of
151
+ # the uniqueness constraint.
152
+ # * <tt>:conditions</tt> - Specify the conditions to be included as a
153
+ # <tt>WHERE</tt> SQL fragment to limit the uniqueness constraint lookup
154
+ # (e.g. <tt>conditions: -> { where(status: 'active') }</tt>).
155
+ # * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by
156
+ # non-text columns (+true+ by default).
157
+ # * <tt>:allow_nil</tt> - If set to +true+, skips this validation if the
158
+ # attribute is +nil+ (default is +false+).
159
+ # * <tt>:allow_blank</tt> - If set to +true+, skips this validation if the
160
+ # attribute is blank (default is +false+).
161
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
162
+ # if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
163
+ # or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
164
+ # proc or string should return or evaluate to a +true+ or +false+ value.
165
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to
166
+ # determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
167
+ # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
168
+ # method, proc or string should return or evaluate to a +true+ or +false+
169
+ # value.
110
170
  #
111
171
  # === Concurrency and integrity
112
172
  #
113
- # Using this validation method in conjunction with ActiveRecord::Base#save
173
+ # Using this validation method in conjunction with
174
+ # {ActiveRecord::Base#save}[rdoc-ref:Persistence#save]
114
175
  # does not guarantee the absence of duplicate record insertions, because
115
176
  # uniqueness checks on the application level are inherently prone to race
116
177
  # conditions. For example, suppose that two users try to post a Comment at
@@ -126,11 +187,11 @@ module ActiveRecord
126
187
  # WHERE title = 'My Post' |
127
188
  # |
128
189
  # | # User 2 does the same thing and also
129
- # | # infers that his title is unique.
190
+ # | # infers that their title is unique.
130
191
  # | SELECT * FROM comments
131
192
  # | WHERE title = 'My Post'
132
193
  # |
133
- # # User 1 inserts his comment. |
194
+ # # User 1 inserts their comment. |
134
195
  # INSERT INTO comments |
135
196
  # (title, content) VALUES |
136
197
  # ('My Post', 'hi!') |
@@ -147,31 +208,30 @@ module ActiveRecord
147
208
  # This could even happen if you use transactions with the 'serializable'
148
209
  # isolation level. The best way to work around this problem is to add a unique
149
210
  # index to the database table using
150
- # ActiveRecord::ConnectionAdapters::SchemaStatements#add_index. In the
151
- # rare case that a race condition occurs, the database will guarantee
211
+ # {connection.add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index].
212
+ # In the rare case that a race condition occurs, the database will guarantee
152
213
  # the field's uniqueness.
153
214
  #
154
215
  # When the database catches such a duplicate insertion,
155
- # ActiveRecord::Base#save will raise an ActiveRecord::StatementInvalid
216
+ # {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will raise an ActiveRecord::StatementInvalid
156
217
  # exception. You can either choose to let this error propagate (which
157
218
  # will result in the default Rails exception page being shown), or you
158
219
  # can catch it and restart the transaction (e.g. by telling the user
159
- # that the title already exists, and asking him to re-enter the title).
160
- # This technique is also known as optimistic concurrency control:
161
- # http://en.wikipedia.org/wiki/Optimistic_concurrency_control
220
+ # that the title already exists, and asking them to re-enter the title).
221
+ # This technique is also known as
222
+ # {optimistic concurrency control}[http://en.wikipedia.org/wiki/Optimistic_concurrency_control].
162
223
  #
163
224
  # The bundled ActiveRecord::ConnectionAdapters distinguish unique index
164
225
  # constraint errors from other types of database errors by throwing an
165
- # ActiveRecord::RecordNotUnique exception.
166
- # For other adapters you will have to parse the (database-specific) exception
167
- # message to detect such a case.
226
+ # ActiveRecord::RecordNotUnique exception. For other adapters you will
227
+ # have to parse the (database-specific) exception message to detect such
228
+ # a case.
229
+ #
168
230
  # The following bundled adapters throw the ActiveRecord::RecordNotUnique exception:
169
- # * ActiveRecord::ConnectionAdapters::MysqlAdapter
170
- # * ActiveRecord::ConnectionAdapters::Mysql2Adapter
171
- # * ActiveRecord::ConnectionAdapters::SQLiteAdapter
172
- # * ActiveRecord::ConnectionAdapters::SQLite3Adapter
173
- # * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
174
231
  #
232
+ # * ActiveRecord::ConnectionAdapters::Mysql2Adapter.
233
+ # * ActiveRecord::ConnectionAdapters::SQLite3Adapter.
234
+ # * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.
175
235
  def validates_uniqueness_of(*attr_names)
176
236
  validates_with UniquenessValidator, _merge_attributes(attr_names)
177
237
  end
@@ -1,83 +1,91 @@
1
1
  module ActiveRecord
2
- # = Active Record RecordInvalid
2
+ # = Active Record \RecordInvalid
3
3
  #
4
- # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
5
- # +record+ method to retrieve the record which did not validate.
4
+ # Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
5
+ # {ActiveRecord::Base#create!}[rdoc-ref:Persistence::ClassMethods#create!] when the record is invalid.
6
+ # Use the #record method to retrieve the record which did not validate.
6
7
  #
7
8
  # begin
8
- # complex_operation_that_calls_save!_internally
9
+ # complex_operation_that_internally_calls_save!
9
10
  # rescue ActiveRecord::RecordInvalid => invalid
10
11
  # puts invalid.record.errors
11
12
  # end
12
13
  class RecordInvalid < ActiveRecordError
13
14
  attr_reader :record
14
- def initialize(record)
15
- @record = record
16
- errors = @record.errors.full_messages.join(", ")
17
- super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors))
15
+
16
+ def initialize(record = nil)
17
+ if record
18
+ @record = record
19
+ errors = @record.errors.full_messages.join(", ")
20
+ message = I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", errors: errors, default: :"errors.messages.record_invalid")
21
+ else
22
+ message = "Record invalid"
23
+ end
24
+
25
+ super(message)
18
26
  end
19
27
  end
20
28
 
21
- # = Active Record Validations
29
+ # = Active Record \Validations
22
30
  #
23
- # Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt>
31
+ # Active Record includes the majority of its validations from ActiveModel::Validations
24
32
  # all of which accept the <tt>:on</tt> argument to define the context where the
25
33
  # validations are active. Active Record will always supply either the context of
26
34
  # <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a
27
- # <tt>new_record?</tt>.
35
+ # {new_record?}[rdoc-ref:Persistence#new_record?].
28
36
  module Validations
29
37
  extend ActiveSupport::Concern
30
38
  include ActiveModel::Validations
31
39
 
32
- module ClassMethods
33
- # Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
34
- # so an exception is raised if the record is invalid.
35
- def create!(attributes = nil, options = {}, &block)
36
- if attributes.is_a?(Array)
37
- attributes.collect { |attr| create!(attr, options, &block) }
38
- else
39
- object = new(attributes, options)
40
- yield(object) if block_given?
41
- object.save!
42
- object
43
- end
44
- end
45
- end
46
-
47
- # The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
48
- # replaced with this when the validations module is mixed in, which it is by default.
40
+ # The validation process on save can be skipped by passing <tt>validate: false</tt>.
41
+ # The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced
42
+ # with this when the validations module is mixed in, which it is by default.
49
43
  def save(options={})
50
44
  perform_validations(options) ? super : false
51
45
  end
52
46
 
53
- # Attempts to save the record just like Base#save but will raise a +RecordInvalid+ exception instead of returning false
54
- # if the record is not valid.
47
+ # Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
48
+ # will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid.
55
49
  def save!(options={})
56
- perform_validations(options) ? super : raise(RecordInvalid.new(self))
50
+ perform_validations(options) ? super : raise_validation_error
57
51
  end
58
52
 
59
- # Runs all the validations within the specified context. Returns true if no errors are found,
60
- # false otherwise.
53
+ # Runs all the validations within the specified context. Returns +true+ if
54
+ # no errors are found, +false+ otherwise.
61
55
  #
62
- # If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
63
- # <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not.
56
+ # Aliased as #validate.
64
57
  #
65
- # Validations with no <tt>:on</tt> option will run no matter the context. Validations with
58
+ # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
59
+ # {new_record?}[rdoc-ref:Persistence#new_record?] is +true+, and to <tt>:update</tt> if it is not.
60
+ #
61
+ # \Validations with no <tt>:on</tt> option will run no matter the context. \Validations with
66
62
  # some <tt>:on</tt> option will only run in the specified context.
67
63
  def valid?(context = nil)
68
- context ||= (new_record? ? :create : :update)
64
+ context ||= default_validation_context
69
65
  output = super(context)
70
66
  errors.empty? && output
71
67
  end
72
68
 
69
+ alias_method :validate, :valid?
70
+
73
71
  protected
74
72
 
75
- def perform_validations(options={})
76
- perform_validation = options[:validate] != false
77
- perform_validation ? valid?(options[:context]) : true
73
+ def default_validation_context
74
+ new_record? ? :create : :update
75
+ end
76
+
77
+ def raise_validation_error
78
+ raise(RecordInvalid.new(self))
79
+ end
80
+
81
+ def perform_validations(options={}) # :nodoc:
82
+ options[:validate] == false || valid?(options[:context])
78
83
  end
79
84
  end
80
85
  end
81
86
 
82
87
  require "active_record/validations/associated"
83
88
  require "active_record/validations/uniqueness"
89
+ require "active_record/validations/presence"
90
+ require "active_record/validations/absence"
91
+ require "active_record/validations/length"
@@ -1,10 +1,8 @@
1
- module ActiveRecord
2
- module VERSION #:nodoc:
3
- MAJOR = 3
4
- MINOR = 2
5
- TINY = 19
6
- PRE = nil
1
+ require_relative 'gem_version'
7
2
 
8
- STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
3
+ module ActiveRecord
4
+ # Returns the version of the currently loaded ActiveRecord as a <tt>Gem::Version</tt>
5
+ def self.version
6
+ gem_version
9
7
  end
10
8
  end
data/lib/active_record.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2004-2011 David Heinemeier Hansson
2
+ # Copyright (c) 2004-2016 David Heinemeier Hansson
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -22,24 +22,58 @@
22
22
  #++
23
23
 
24
24
  require 'active_support'
25
- require 'active_support/i18n'
25
+ require 'active_support/rails'
26
26
  require 'active_model'
27
27
  require 'arel'
28
28
 
29
29
  require 'active_record/version'
30
+ require 'active_record/attribute_set'
30
31
 
31
32
  module ActiveRecord
32
33
  extend ActiveSupport::Autoload
33
34
 
34
- # ActiveRecord::SessionStore depends on the abstract store in Action Pack.
35
- # Eager loading this class would break client code that eager loads Active
36
- # Record standalone.
37
- #
38
- # Note that the Rails application generator creates an initializer specific
39
- # for setting the session store. Thus, albeit in theory this autoload would
40
- # not be thread-safe, in practice it is because if the application uses this
41
- # session store its autoload happens at boot time.
42
- autoload :SessionStore
35
+ autoload :Attribute
36
+ autoload :Base
37
+ autoload :Callbacks
38
+ autoload :Core
39
+ autoload :ConnectionHandling
40
+ autoload :CounterCache
41
+ autoload :DynamicMatchers
42
+ autoload :Enum
43
+ autoload :InternalMetadata
44
+ autoload :Explain
45
+ autoload :Inheritance
46
+ autoload :Integration
47
+ autoload :LegacyYamlAdapter
48
+ autoload :Migration
49
+ autoload :Migrator, 'active_record/migration'
50
+ autoload :ModelSchema
51
+ autoload :NestedAttributes
52
+ autoload :NoTouching
53
+ autoload :TouchLater
54
+ autoload :Persistence
55
+ autoload :QueryCache
56
+ autoload :Querying
57
+ autoload :CollectionCacheKey
58
+ autoload :ReadonlyAttributes
59
+ autoload :RecordInvalid, 'active_record/validations'
60
+ autoload :Reflection
61
+ autoload :RuntimeRegistry
62
+ autoload :Sanitization
63
+ autoload :Schema
64
+ autoload :SchemaDumper
65
+ autoload :SchemaMigration
66
+ autoload :Scoping
67
+ autoload :Serialization
68
+ autoload :StatementCache
69
+ autoload :Store
70
+ autoload :Suppressor
71
+ autoload :TableMetadata
72
+ autoload :Timestamp
73
+ autoload :Transactions
74
+ autoload :Translation
75
+ autoload :Validations
76
+ autoload :SecureToken
43
77
 
44
78
  eager_autoload do
45
79
  autoload :ActiveRecordError, 'active_record/errors'
@@ -48,11 +82,13 @@ module ActiveRecord
48
82
 
49
83
  autoload :Aggregations
50
84
  autoload :Associations
51
- autoload :AttributeMethods
52
85
  autoload :AttributeAssignment
86
+ autoload :AttributeMethods
53
87
  autoload :AutosaveAssociation
54
88
 
55
89
  autoload :Relation
90
+ autoload :AssociationRelation
91
+ autoload :NullRelation
56
92
 
57
93
  autoload_under 'relation' do
58
94
  autoload :QueryMethods
@@ -61,45 +97,15 @@ module ActiveRecord
61
97
  autoload :PredicateBuilder
62
98
  autoload :SpawnMethods
63
99
  autoload :Batches
64
- autoload :Explain
65
100
  autoload :Delegation
66
101
  end
67
102
 
68
- autoload :Base
69
- autoload :Callbacks
70
- autoload :CounterCache
71
- autoload :DynamicMatchers
72
- autoload :DynamicFinderMatch
73
- autoload :DynamicScopeMatch
74
- autoload :Explain
75
- autoload :IdentityMap
76
- autoload :Inheritance
77
- autoload :Integration
78
- autoload :Migration
79
- autoload :Migrator, 'active_record/migration'
80
- autoload :ModelSchema
81
- autoload :NestedAttributes
82
- autoload :Observer
83
- autoload :Persistence
84
- autoload :QueryCache
85
- autoload :Querying
86
- autoload :ReadonlyAttributes
87
- autoload :Reflection
88
103
  autoload :Result
89
- autoload :Sanitization
90
- autoload :Schema
91
- autoload :SchemaDumper
92
- autoload :Scoping
93
- autoload :Serialization
94
- autoload :Store
95
- autoload :Timestamp
96
- autoload :Transactions
97
- autoload :Translation
98
- autoload :Validations
99
104
  end
100
105
 
101
106
  module Coders
102
107
  autoload :YAMLColumn, 'active_record/coders/yaml_column'
108
+ autoload :JSON, 'active_record/coders/json'
103
109
  end
104
110
 
105
111
  module AttributeMethods
@@ -114,7 +120,6 @@ module ActiveRecord
114
120
  autoload :TimeZoneConversion
115
121
  autoload :Write
116
122
  autoload :Serialization
117
- autoload :DeprecatedUnderscoreRead
118
123
  end
119
124
  end
120
125
 
@@ -132,7 +137,6 @@ module ActiveRecord
132
137
 
133
138
  eager_autoload do
134
139
  autoload :AbstractAdapter
135
- autoload :ConnectionManagement, "active_record/connection_adapters/abstract/connection_pool"
136
140
  end
137
141
  end
138
142
 
@@ -145,12 +149,32 @@ module ActiveRecord
145
149
  end
146
150
  end
147
151
 
148
- autoload :TestCase
152
+ module Tasks
153
+ extend ActiveSupport::Autoload
154
+
155
+ autoload :DatabaseTasks
156
+ autoload :SQLiteDatabaseTasks, 'active_record/tasks/sqlite_database_tasks'
157
+ autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
158
+ autoload :PostgreSQLDatabaseTasks,
159
+ 'active_record/tasks/postgresql_database_tasks'
160
+ end
161
+
149
162
  autoload :TestFixtures, 'active_record/fixtures'
163
+
164
+ def self.eager_load!
165
+ super
166
+ ActiveRecord::Locking.eager_load!
167
+ ActiveRecord::Scoping.eager_load!
168
+ ActiveRecord::Associations.eager_load!
169
+ ActiveRecord::AttributeMethods.eager_load!
170
+ ActiveRecord::ConnectionAdapters.eager_load!
171
+ end
150
172
  end
151
173
 
152
174
  ActiveSupport.on_load(:active_record) do
153
175
  Arel::Table.engine = self
154
176
  end
155
177
 
156
- I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
178
+ ActiveSupport.on_load(:i18n) do
179
+ I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
180
+ end