activerecord 5.1.0 → 5.2.3

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 (261) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +596 -450
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -5
  5. data/examples/performance.rb +2 -0
  6. data/examples/simple.rb +2 -0
  7. data/lib/active_record.rb +11 -4
  8. data/lib/active_record/aggregations.rb +6 -5
  9. data/lib/active_record/association_relation.rb +7 -5
  10. data/lib/active_record/associations.rb +77 -85
  11. data/lib/active_record/associations/alias_tracker.rb +23 -32
  12. data/lib/active_record/associations/association.rb +49 -35
  13. data/lib/active_record/associations/association_scope.rb +55 -55
  14. data/lib/active_record/associations/belongs_to_association.rb +30 -11
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
  16. data/lib/active_record/associations/builder/association.rb +4 -7
  17. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  18. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  20. data/lib/active_record/associations/builder/has_many.rb +2 -0
  21. data/lib/active_record/associations/builder/has_one.rb +2 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  23. data/lib/active_record/associations/collection_association.rb +66 -53
  24. data/lib/active_record/associations/collection_proxy.rb +30 -73
  25. data/lib/active_record/associations/foreign_association.rb +2 -0
  26. data/lib/active_record/associations/has_many_association.rb +13 -2
  27. data/lib/active_record/associations/has_many_through_association.rb +37 -19
  28. data/lib/active_record/associations/has_one_association.rb +14 -1
  29. data/lib/active_record/associations/has_one_through_association.rb +13 -8
  30. data/lib/active_record/associations/join_dependency.rb +52 -96
  31. data/lib/active_record/associations/join_dependency/join_association.rb +22 -75
  32. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  33. data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/preloader/association.rb +53 -92
  36. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  37. data/lib/active_record/associations/singular_association.rb +14 -16
  38. data/lib/active_record/associations/through_association.rb +27 -12
  39. data/lib/active_record/attribute_assignment.rb +2 -5
  40. data/lib/active_record/attribute_decorators.rb +3 -2
  41. data/lib/active_record/attribute_methods.rb +65 -24
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +33 -216
  44. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  45. data/lib/active_record/attribute_methods/query.rb +2 -0
  46. data/lib/active_record/attribute_methods/read.rb +9 -3
  47. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  49. data/lib/active_record/attribute_methods/write.rb +22 -19
  50. data/lib/active_record/attributes.rb +7 -6
  51. data/lib/active_record/autosave_association.rb +15 -13
  52. data/lib/active_record/base.rb +2 -0
  53. data/lib/active_record/callbacks.rb +12 -6
  54. data/lib/active_record/coders/json.rb +2 -0
  55. data/lib/active_record/coders/yaml_column.rb +2 -0
  56. data/lib/active_record/collection_cache_key.rb +15 -11
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +120 -39
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +192 -37
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +13 -2
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -25
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +65 -7
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -87
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -98
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +126 -189
  70. data/lib/active_record/connection_adapters/column.rb +4 -2
  71. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
  73. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -15
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +258 -129
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +75 -87
  118. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +90 -96
  127. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  128. data/lib/active_record/connection_handling.rb +4 -2
  129. data/lib/active_record/core.rb +41 -61
  130. data/lib/active_record/counter_cache.rb +20 -15
  131. data/lib/active_record/define_callbacks.rb +5 -3
  132. data/lib/active_record/dynamic_matchers.rb +9 -9
  133. data/lib/active_record/enum.rb +18 -13
  134. data/lib/active_record/errors.rb +60 -15
  135. data/lib/active_record/explain.rb +3 -1
  136. data/lib/active_record/explain_registry.rb +2 -0
  137. data/lib/active_record/explain_subscriber.rb +2 -0
  138. data/lib/active_record/fixture_set/file.rb +2 -0
  139. data/lib/active_record/fixtures.rb +67 -60
  140. data/lib/active_record/gem_version.rb +4 -2
  141. data/lib/active_record/inheritance.rb +49 -19
  142. data/lib/active_record/integration.rb +58 -19
  143. data/lib/active_record/internal_metadata.rb +2 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  145. data/lib/active_record/locking/optimistic.rb +30 -42
  146. data/lib/active_record/locking/pessimistic.rb +10 -7
  147. data/lib/active_record/log_subscriber.rb +46 -4
  148. data/lib/active_record/migration.rb +189 -139
  149. data/lib/active_record/migration/command_recorder.rb +11 -9
  150. data/lib/active_record/migration/compatibility.rb +81 -29
  151. data/lib/active_record/migration/join_table.rb +2 -0
  152. data/lib/active_record/model_schema.rb +74 -58
  153. data/lib/active_record/nested_attributes.rb +18 -6
  154. data/lib/active_record/no_touching.rb +3 -1
  155. data/lib/active_record/null_relation.rb +2 -0
  156. data/lib/active_record/persistence.rb +199 -54
  157. data/lib/active_record/query_cache.rb +8 -10
  158. data/lib/active_record/querying.rb +5 -3
  159. data/lib/active_record/railtie.rb +62 -6
  160. data/lib/active_record/railties/console_sandbox.rb +2 -0
  161. data/lib/active_record/railties/controller_runtime.rb +2 -0
  162. data/lib/active_record/railties/databases.rake +48 -38
  163. data/lib/active_record/readonly_attributes.rb +3 -2
  164. data/lib/active_record/reflection.rb +137 -207
  165. data/lib/active_record/relation.rb +132 -207
  166. data/lib/active_record/relation/batches.rb +32 -17
  167. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  168. data/lib/active_record/relation/calculations.rb +66 -25
  169. data/lib/active_record/relation/delegation.rb +45 -29
  170. data/lib/active_record/relation/finder_methods.rb +76 -85
  171. data/lib/active_record/relation/from_clause.rb +2 -8
  172. data/lib/active_record/relation/merger.rb +53 -23
  173. data/lib/active_record/relation/predicate_builder.rb +60 -79
  174. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  175. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  176. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  177. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  178. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  179. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  180. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  181. data/lib/active_record/relation/query_attribute.rb +28 -2
  182. data/lib/active_record/relation/query_methods.rb +135 -103
  183. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  184. data/lib/active_record/relation/spawn_methods.rb +4 -2
  185. data/lib/active_record/relation/where_clause.rb +65 -67
  186. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  187. data/lib/active_record/result.rb +2 -0
  188. data/lib/active_record/runtime_registry.rb +2 -0
  189. data/lib/active_record/sanitization.rb +129 -121
  190. data/lib/active_record/schema.rb +4 -2
  191. data/lib/active_record/schema_dumper.rb +36 -26
  192. data/lib/active_record/schema_migration.rb +2 -0
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/scoping/default.rb +10 -7
  195. data/lib/active_record/scoping/named.rb +40 -12
  196. data/lib/active_record/secure_token.rb +2 -0
  197. data/lib/active_record/serialization.rb +2 -0
  198. data/lib/active_record/statement_cache.rb +22 -12
  199. data/lib/active_record/store.rb +3 -1
  200. data/lib/active_record/suppressor.rb +2 -0
  201. data/lib/active_record/table_metadata.rb +12 -3
  202. data/lib/active_record/tasks/database_tasks.rb +38 -26
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  206. data/lib/active_record/timestamp.rb +13 -6
  207. data/lib/active_record/touch_later.rb +2 -0
  208. data/lib/active_record/transactions.rb +32 -27
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type.rb +4 -1
  211. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  212. data/lib/active_record/type/date.rb +2 -0
  213. data/lib/active_record/type/date_time.rb +2 -0
  214. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  215. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  216. data/lib/active_record/type/internal/timezone.rb +2 -0
  217. data/lib/active_record/type/json.rb +30 -0
  218. data/lib/active_record/type/serialized.rb +6 -0
  219. data/lib/active_record/type/text.rb +2 -0
  220. data/lib/active_record/type/time.rb +2 -0
  221. data/lib/active_record/type/type_map.rb +2 -0
  222. data/lib/active_record/type/unsigned_integer.rb +2 -0
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/type_caster/connection.rb +2 -0
  225. data/lib/active_record/type_caster/map.rb +3 -1
  226. data/lib/active_record/validations.rb +2 -0
  227. data/lib/active_record/validations/absence.rb +2 -0
  228. data/lib/active_record/validations/associated.rb +2 -0
  229. data/lib/active_record/validations/length.rb +2 -0
  230. data/lib/active_record/validations/presence.rb +2 -0
  231. data/lib/active_record/validations/uniqueness.rb +36 -6
  232. data/lib/active_record/version.rb +2 -0
  233. data/lib/rails/generators/active_record.rb +3 -1
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration.rb +2 -0
  237. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  238. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  239. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  241. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  243. metadata +24 -36
  244. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  245. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  246. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  248. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  249. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  250. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  251. data/lib/active_record/attribute.rb +0 -240
  252. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  254. data/lib/active_record/attribute_set.rb +0 -113
  255. data/lib/active_record/attribute_set/builder.rb +0 -124
  256. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  257. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  258. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  259. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  260. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  261. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # = Active Record \Schema
3
5
  #
@@ -37,7 +39,7 @@ module ActiveRecord
37
39
  # The +info+ hash is optional, and if given is used to define metadata
38
40
  # about the current schema (currently, only the schema's version):
39
41
  #
40
- # ActiveRecord::Schema.define(version: 20380119000001) do
42
+ # ActiveRecord::Schema.define(version: 2038_01_19_000001) do
41
43
  # ...
42
44
  # end
43
45
  def self.define(info = {}, &block)
@@ -53,7 +55,7 @@ module ActiveRecord
53
55
  end
54
56
 
55
57
  ActiveRecord::InternalMetadata.create_table
56
- ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
58
+ ActiveRecord::InternalMetadata[:environment] = connection.migration_context.current_environment
57
59
  end
58
60
 
59
61
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "stringio"
2
4
 
3
5
  module ActiveRecord
@@ -11,14 +13,13 @@ module ActiveRecord
11
13
  ##
12
14
  # :singleton-method:
13
15
  # A list of tables which should not be dumped to the schema.
14
- # Acceptable values are strings as well as regexp.
15
- # This setting is only used if ActiveRecord::Base.schema_format == :ruby
16
- cattr_accessor :ignore_tables
17
- @@ignore_tables = []
16
+ # Acceptable values are strings as well as regexp if ActiveRecord::Base.schema_format == :ruby.
17
+ # Only strings are accepted if ActiveRecord::Base.schema_format == :sql.
18
+ cattr_accessor :ignore_tables, default: []
18
19
 
19
20
  class << self
20
21
  def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = ActiveRecord::Base)
21
- new(connection, generate_options(config)).dump(stream)
22
+ connection.create_schema_dumper(generate_options(config)).dump(stream)
22
23
  stream
23
24
  end
24
25
 
@@ -43,13 +44,22 @@ module ActiveRecord
43
44
 
44
45
  def initialize(connection, options = {})
45
46
  @connection = connection
46
- @version = Migrator::current_version rescue nil
47
+ @version = connection.migration_context.current_version rescue nil
47
48
  @options = options
48
49
  end
49
50
 
50
- def header(stream)
51
- define_params = @version ? "version: #{@version}" : ""
51
+ # turns 20170404131909 into "2017_04_04_131909"
52
+ def formatted_version
53
+ stringified = @version.to_s
54
+ return stringified unless stringified.length == 14
55
+ stringified.insert(4, "_").insert(7, "_").insert(10, "_")
56
+ end
52
57
 
58
+ def define_params
59
+ @version ? "version: #{formatted_version}" : ""
60
+ end
61
+
62
+ def header(stream)
53
63
  stream.puts <<HEADER
54
64
  # This file is auto-generated from the current state of the database. Instead
55
65
  # of editing this file, please use the migrations feature of Active Record to
@@ -72,16 +82,8 @@ HEADER
72
82
  stream.puts "end"
73
83
  end
74
84
 
85
+ # extensions are only supported by PostgreSQL
75
86
  def extensions(stream)
76
- return unless @connection.supports_extensions?
77
- extensions = @connection.extensions
78
- if extensions.any?
79
- stream.puts " # These are extensions that must be enabled in order to support this database"
80
- extensions.each do |extension|
81
- stream.puts " enable_extension #{extension.inspect}"
82
- end
83
- stream.puts
84
- end
85
87
  end
86
88
 
87
89
  def tables(stream)
@@ -113,7 +115,7 @@ HEADER
113
115
  when String
114
116
  tbl.print ", primary_key: #{pk.inspect}" unless pk == "id"
115
117
  pkcol = columns.detect { |c| c.name == pk }
116
- pkcolspec = @connection.column_spec_for_primary_key(pkcol)
118
+ pkcolspec = column_spec_for_primary_key(pkcol)
117
119
  if pkcolspec.present?
118
120
  tbl.print ", #{format_colspec(pkcolspec)}"
119
121
  end
@@ -122,20 +124,19 @@ HEADER
122
124
  else
123
125
  tbl.print ", id: false"
124
126
  end
125
- tbl.print ", force: :cascade"
126
127
 
127
128
  table_options = @connection.table_options(table)
128
129
  if table_options.present?
129
130
  tbl.print ", #{format_options(table_options)}"
130
131
  end
131
132
 
132
- tbl.puts " do |t|"
133
+ tbl.puts ", force: :cascade do |t|"
133
134
 
134
135
  # then dump all non-primary key columns
135
136
  columns.each do |column|
136
137
  raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
137
138
  next if column.name == pk
138
- type, colspec = @connection.column_spec(column)
139
+ type, colspec = column_spec(column)
139
140
  tbl.print " t.#{type} #{column.name.inspect}"
140
141
  tbl.print ", #{format_colspec(colspec)}" if colspec.present?
141
142
  tbl.puts
@@ -153,8 +154,6 @@ HEADER
153
154
  stream.puts "# #{e.message}"
154
155
  stream.puts
155
156
  end
156
-
157
- stream
158
157
  end
159
158
 
160
159
  # Keep it for indexing materialized views
@@ -185,8 +184,9 @@ HEADER
185
184
  "name: #{index.name.inspect}",
186
185
  ]
187
186
  index_parts << "unique: true" if index.unique
188
- index_parts << "length: { #{format_options(index.lengths)} }" if index.lengths.present?
189
- index_parts << "order: { #{format_options(index.orders)} }" if index.orders.present?
187
+ index_parts << "length: #{format_index_parts(index.lengths)}" if index.lengths.present?
188
+ index_parts << "order: #{format_index_parts(index.orders)}" if index.orders.present?
189
+ index_parts << "opclass: #{format_index_parts(index.opclasses)}" if index.opclasses.present?
190
190
  index_parts << "where: #{index.where.inspect}" if index.where
191
191
  index_parts << "using: #{index.using.inspect}" if !@connection.default_index_type?(index)
192
192
  index_parts << "type: #{index.type.inspect}" if index.type
@@ -232,8 +232,18 @@ HEADER
232
232
  options.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
233
233
  end
234
234
 
235
+ def format_index_parts(options)
236
+ if options.is_a?(Hash)
237
+ "{ #{format_options(options)} }"
238
+ else
239
+ options.inspect
240
+ end
241
+ end
242
+
235
243
  def remove_prefix_and_suffix(table)
236
- table.gsub(/^(#{@options[:table_name_prefix]})(.+)(#{@options[:table_name_suffix]})$/, "\\2")
244
+ prefix = Regexp.escape(@options[:table_name_prefix].to_s)
245
+ suffix = Regexp.escape(@options[:table_name_suffix].to_s)
246
+ table.sub(/\A#{prefix}(.+)#{suffix}\z/, "\\1")
237
247
  end
238
248
 
239
249
  def ignored?(table_name)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/scoping/default"
2
4
  require "active_record/scoping/named"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/per_thread_registry"
2
4
 
3
5
  module ActiveRecord
@@ -9,23 +11,23 @@ module ActiveRecord
9
11
  include Named
10
12
  end
11
13
 
12
- module ClassMethods
13
- def current_scope #:nodoc:
14
- ScopeRegistry.value_for(:current_scope, self)
14
+ module ClassMethods # :nodoc:
15
+ def current_scope(skip_inherited_scope = false)
16
+ ScopeRegistry.value_for(:current_scope, self, skip_inherited_scope)
15
17
  end
16
18
 
17
- def current_scope=(scope) #:nodoc:
19
+ def current_scope=(scope)
18
20
  ScopeRegistry.set_value_for(:current_scope, self, scope)
19
21
  end
20
22
 
21
23
  # Collects attributes from scopes that should be applied when creating
22
24
  # an AR instance for the particular class this is called on.
23
- def scope_attributes # :nodoc:
25
+ def scope_attributes
24
26
  all.scope_for_create
25
27
  end
26
28
 
27
29
  # Are there attributes associated with this scope?
28
- def scope_attributes? # :nodoc:
30
+ def scope_attributes?
29
31
  current_scope
30
32
  end
31
33
  end
@@ -33,9 +35,8 @@ module ActiveRecord
33
35
  def populate_with_current_scope_attributes # :nodoc:
34
36
  return unless self.class.scope_attributes?
35
37
 
36
- self.class.scope_attributes.each do |att, value|
37
- send("#{att}=", value) if respond_to?("#{att}=")
38
- end
38
+ attributes = self.class.scope_attributes
39
+ _assign_attributes(attributes) if attributes.any?
39
40
  end
40
41
 
41
42
  def initialize_internals_callback # :nodoc:
@@ -75,8 +76,9 @@ module ActiveRecord
75
76
  end
76
77
 
77
78
  # Obtains the value for a given +scope_type+ and +model+.
78
- def value_for(scope_type, model)
79
+ def value_for(scope_type, model, skip_inherited_scope = false)
79
80
  raise_invalid_scope_type!(scope_type)
81
+ return @registry[scope_type][model.name] if skip_inherited_scope
80
82
  klass = model
81
83
  base = model.base_class
82
84
  while klass <= base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module Scoping
3
5
  module Default
@@ -5,11 +7,8 @@ module ActiveRecord
5
7
 
6
8
  included do
7
9
  # Stores the default scope for the class.
8
- class_attribute :default_scopes, instance_writer: false, instance_predicate: false
9
- class_attribute :default_scope_override, instance_writer: false, instance_predicate: false
10
-
11
- self.default_scopes = []
12
- self.default_scope_override = nil
10
+ class_attribute :default_scopes, instance_writer: false, instance_predicate: false, default: []
11
+ class_attribute :default_scope_override, instance_writer: false, instance_predicate: false, default: nil
13
12
  end
14
13
 
15
14
  module ClassMethods
@@ -110,13 +109,17 @@ module ActiveRecord
110
109
 
111
110
  if default_scope_override
112
111
  # The user has defined their own default scope method, so call that
113
- evaluate_default_scope { default_scope }
112
+ evaluate_default_scope do
113
+ if scope = default_scope
114
+ (base_rel ||= relation).merge!(scope)
115
+ end
116
+ end
114
117
  elsif default_scopes.any?
115
118
  base_rel ||= relation
116
119
  evaluate_default_scope do
117
120
  default_scopes.inject(base_rel) do |default_scope, scope|
118
121
  scope = scope.respond_to?(:to_proc) ? scope : scope.method(:call)
119
- default_scope.merge(base_rel.instance_exec(&scope))
122
+ default_scope.merge!(base_rel.instance_exec(&scope))
120
123
  end
121
124
  end
122
125
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/array"
2
4
  require "active_support/core_ext/hash/except"
3
5
  require "active_support/core_ext/kernel/singleton_class"
@@ -22,20 +24,38 @@ module ActiveRecord
22
24
  # You can define a scope that applies to all finders using
23
25
  # {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope].
24
26
  def all
27
+ current_scope = self.current_scope
28
+
25
29
  if current_scope
26
- current_scope.clone
30
+ if self == current_scope.klass
31
+ current_scope.clone
32
+ else
33
+ relation.merge!(current_scope)
34
+ end
27
35
  else
28
36
  default_scoped
29
37
  end
30
38
  end
31
39
 
32
- def default_scoped # :nodoc:
33
- scope = build_default_scope
40
+ def scope_for_association(scope = relation) # :nodoc:
41
+ current_scope = self.current_scope
34
42
 
35
- if scope
36
- relation.spawn.merge!(scope)
43
+ if current_scope && current_scope.empty_scope?
44
+ scope
37
45
  else
38
- relation
46
+ default_scoped(scope)
47
+ end
48
+ end
49
+
50
+ def default_scoped(scope = relation) # :nodoc:
51
+ build_default_scope(scope) || scope
52
+ end
53
+
54
+ def default_extensions # :nodoc:
55
+ if scope = current_scope || build_default_scope
56
+ scope.extensions
57
+ else
58
+ []
39
59
  end
40
60
  end
41
61
 
@@ -151,24 +171,32 @@ module ActiveRecord
151
171
  "a class method with the same name."
152
172
  end
153
173
 
174
+ if method_defined_within?(name, Relation)
175
+ raise ArgumentError, "You tried to define a scope named \"#{name}\" " \
176
+ "on the model \"#{self.name}\", but ActiveRecord::Relation already defined " \
177
+ "an instance method with the same name."
178
+ end
179
+
154
180
  valid_scope_name?(name)
155
181
  extension = Module.new(&block) if block
156
182
 
157
183
  if body.respond_to?(:to_proc)
158
184
  singleton_class.send(:define_method, name) do |*args|
159
- scope = all.scoping { instance_exec(*args, &body) }
185
+ scope = all
186
+ scope = scope._exec_scope(*args, &body)
160
187
  scope = scope.extending(extension) if extension
161
-
162
- scope || all
188
+ scope
163
189
  end
164
190
  else
165
191
  singleton_class.send(:define_method, name) do |*args|
166
- scope = all.scoping { body.call(*args) }
192
+ scope = all
193
+ scope = scope.scoping { body.call(*args) || scope }
167
194
  scope = scope.extending(extension) if extension
168
-
169
- scope || all
195
+ scope
170
196
  end
171
197
  end
198
+
199
+ generate_relation_method(name)
172
200
  end
173
201
 
174
202
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module SecureToken
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord #:nodoc:
2
4
  # = Active Record \Serialization
3
5
  module Serialization
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # Statement cache is used to cache a single statement in order to avoid creating the AST again.
3
5
  # Initializing the cache is done by passing the statement in the create block:
@@ -9,7 +11,7 @@ module ActiveRecord
9
11
  # The cached statement is executed by using the
10
12
  # {connection.execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute] method:
11
13
  #
12
- # cache.execute([], Book, Book.connection)
14
+ # cache.execute([], Book.connection)
13
15
  #
14
16
  # The relation returned by the block is cached, and for each
15
17
  # {execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute]
@@ -24,7 +26,7 @@ module ActiveRecord
24
26
  #
25
27
  # And pass the bind values as the first argument of +execute+ call.
26
28
  #
27
- # cache.execute(["my book"], Book, Book.connection)
29
+ # cache.execute(["my book"], Book.connection)
28
30
  class StatementCache # :nodoc:
29
31
  class Substitute; end # :nodoc:
30
32
 
@@ -85,27 +87,35 @@ module ActiveRecord
85
87
  end
86
88
  end
87
89
 
88
- attr_reader :bind_map, :query_builder
89
-
90
90
  def self.create(connection, block = Proc.new)
91
- relation = block.call Params.new
92
- bind_map = BindMap.new relation.bound_attributes
93
- query_builder = connection.cacheable_query(self, relation.arel)
94
- new query_builder, bind_map
91
+ relation = block.call Params.new
92
+ query_builder, binds = connection.cacheable_query(self, relation.arel)
93
+ bind_map = BindMap.new(binds)
94
+ new(query_builder, bind_map, relation.klass)
95
95
  end
96
96
 
97
- def initialize(query_builder, bind_map)
97
+ def initialize(query_builder, bind_map, klass)
98
98
  @query_builder = query_builder
99
- @bind_map = bind_map
99
+ @bind_map = bind_map
100
+ @klass = klass
100
101
  end
101
102
 
102
- def execute(params, klass, connection, &block)
103
+ def execute(params, connection, &block)
103
104
  bind_values = bind_map.bind params
104
105
 
105
106
  sql = query_builder.sql_for bind_values, connection
106
107
 
107
108
  klass.find_by_sql(sql, bind_values, preparable: true, &block)
108
109
  end
109
- alias :call :execute
110
+
111
+ def self.unsupported_value?(value)
112
+ case value
113
+ when NilClass, Array, Range, Hash, Relation, Base then true
114
+ end
115
+ end
116
+
117
+ protected
118
+
119
+ attr_reader :query_builder, :bind_map, :klass
110
120
  end
111
121
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/hash/indifferent_access"
2
4
 
3
5
  module ActiveRecord
@@ -133,7 +135,7 @@ module ActiveRecord
133
135
  end
134
136
 
135
137
  def store_accessor_for(store_attribute)
136
- type_for_attribute(store_attribute.to_s).accessor
138
+ type_for_attribute(store_attribute).accessor
137
139
  end
138
140
 
139
141
  class HashAccessor # :nodoc: