activerecord 3.1.12 → 3.2.22.1

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 (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +804 -338
  3. data/README.rdoc +3 -3
  4. data/examples/performance.rb +20 -1
  5. data/lib/active_record/aggregations.rb +1 -1
  6. data/lib/active_record/associations/alias_tracker.rb +3 -6
  7. data/lib/active_record/associations/association.rb +13 -45
  8. data/lib/active_record/associations/association_scope.rb +3 -15
  9. data/lib/active_record/associations/belongs_to_association.rb +1 -1
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +2 -1
  11. data/lib/active_record/associations/builder/association.rb +6 -4
  12. data/lib/active_record/associations/builder/belongs_to.rb +7 -4
  13. data/lib/active_record/associations/builder/collection_association.rb +2 -2
  14. data/lib/active_record/associations/builder/has_many.rb +4 -4
  15. data/lib/active_record/associations/builder/has_one.rb +5 -6
  16. data/lib/active_record/associations/builder/singular_association.rb +3 -16
  17. data/lib/active_record/associations/collection_association.rb +65 -32
  18. data/lib/active_record/associations/collection_proxy.rb +8 -41
  19. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +1 -0
  20. data/lib/active_record/associations/has_many_association.rb +11 -7
  21. data/lib/active_record/associations/has_many_through_association.rb +19 -9
  22. data/lib/active_record/associations/has_one_association.rb +23 -13
  23. data/lib/active_record/associations/join_dependency/join_association.rb +6 -1
  24. data/lib/active_record/associations/join_dependency.rb +3 -3
  25. data/lib/active_record/associations/preloader/through_association.rb +3 -3
  26. data/lib/active_record/associations/preloader.rb +14 -10
  27. data/lib/active_record/associations/through_association.rb +8 -4
  28. data/lib/active_record/associations.rb +92 -76
  29. data/lib/active_record/attribute_assignment.rb +221 -0
  30. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
  31. data/lib/active_record/attribute_methods/dirty.rb +21 -11
  32. data/lib/active_record/attribute_methods/primary_key.rb +62 -25
  33. data/lib/active_record/attribute_methods/read.rb +73 -83
  34. data/lib/active_record/attribute_methods/serialization.rb +120 -0
  35. data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -14
  36. data/lib/active_record/attribute_methods/write.rb +32 -6
  37. data/lib/active_record/attribute_methods.rb +231 -30
  38. data/lib/active_record/autosave_association.rb +44 -26
  39. data/lib/active_record/base.rb +227 -1708
  40. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +150 -148
  41. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +85 -29
  42. data/lib/active_record/connection_adapters/abstract/database_statements.rb +7 -34
  43. data/lib/active_record/connection_adapters/abstract/query_cache.rb +10 -2
  44. data/lib/active_record/connection_adapters/abstract/quoting.rb +7 -4
  45. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +39 -28
  46. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +48 -19
  47. data/lib/active_record/connection_adapters/abstract_adapter.rb +77 -42
  48. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +676 -0
  49. data/lib/active_record/connection_adapters/column.rb +37 -11
  50. data/lib/active_record/connection_adapters/mysql2_adapter.rb +133 -581
  51. data/lib/active_record/connection_adapters/mysql_adapter.rb +136 -693
  52. data/lib/active_record/connection_adapters/postgresql_adapter.rb +209 -97
  53. data/lib/active_record/connection_adapters/schema_cache.rb +69 -0
  54. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
  55. data/lib/active_record/connection_adapters/sqlite_adapter.rb +62 -35
  56. data/lib/active_record/counter_cache.rb +9 -4
  57. data/lib/active_record/dynamic_finder_match.rb +12 -0
  58. data/lib/active_record/dynamic_matchers.rb +84 -0
  59. data/lib/active_record/errors.rb +11 -1
  60. data/lib/active_record/explain.rb +86 -0
  61. data/lib/active_record/explain_subscriber.rb +25 -0
  62. data/lib/active_record/fixtures/file.rb +65 -0
  63. data/lib/active_record/fixtures.rb +57 -86
  64. data/lib/active_record/identity_map.rb +3 -4
  65. data/lib/active_record/inheritance.rb +174 -0
  66. data/lib/active_record/integration.rb +60 -0
  67. data/lib/active_record/locking/optimistic.rb +33 -26
  68. data/lib/active_record/locking/pessimistic.rb +23 -1
  69. data/lib/active_record/log_subscriber.rb +8 -4
  70. data/lib/active_record/migration/command_recorder.rb +8 -8
  71. data/lib/active_record/migration.rb +68 -35
  72. data/lib/active_record/model_schema.rb +368 -0
  73. data/lib/active_record/nested_attributes.rb +60 -24
  74. data/lib/active_record/persistence.rb +57 -11
  75. data/lib/active_record/query_cache.rb +6 -6
  76. data/lib/active_record/querying.rb +58 -0
  77. data/lib/active_record/railtie.rb +37 -29
  78. data/lib/active_record/railties/controller_runtime.rb +3 -1
  79. data/lib/active_record/railties/databases.rake +213 -117
  80. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  81. data/lib/active_record/readonly_attributes.rb +26 -0
  82. data/lib/active_record/reflection.rb +7 -15
  83. data/lib/active_record/relation/batches.rb +7 -4
  84. data/lib/active_record/relation/calculations.rb +55 -16
  85. data/lib/active_record/relation/delegation.rb +49 -0
  86. data/lib/active_record/relation/finder_methods.rb +16 -11
  87. data/lib/active_record/relation/predicate_builder.rb +8 -6
  88. data/lib/active_record/relation/query_methods.rb +75 -9
  89. data/lib/active_record/relation/spawn_methods.rb +48 -7
  90. data/lib/active_record/relation.rb +78 -32
  91. data/lib/active_record/result.rb +10 -4
  92. data/lib/active_record/sanitization.rb +194 -0
  93. data/lib/active_record/schema_dumper.rb +12 -5
  94. data/lib/active_record/scoping/default.rb +142 -0
  95. data/lib/active_record/scoping/named.rb +200 -0
  96. data/lib/active_record/scoping.rb +152 -0
  97. data/lib/active_record/serialization.rb +1 -43
  98. data/lib/active_record/serializers/xml_serializer.rb +4 -45
  99. data/lib/active_record/session_store.rb +18 -16
  100. data/lib/active_record/store.rb +52 -0
  101. data/lib/active_record/test_case.rb +11 -7
  102. data/lib/active_record/timestamp.rb +17 -3
  103. data/lib/active_record/transactions.rb +27 -6
  104. data/lib/active_record/translation.rb +22 -0
  105. data/lib/active_record/validations/associated.rb +5 -4
  106. data/lib/active_record/validations/uniqueness.rb +8 -8
  107. data/lib/active_record/validations.rb +1 -1
  108. data/lib/active_record/version.rb +3 -3
  109. data/lib/active_record.rb +38 -3
  110. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
  111. data/lib/rails/generators/active_record/migration/templates/migration.rb +12 -3
  112. data/lib/rails/generators/active_record/model/model_generator.rb +9 -1
  113. data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
  114. data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
  115. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
  116. metadata +49 -28
  117. data/lib/active_record/named_scope.rb +0 -200
@@ -0,0 +1,58 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ module ActiveRecord
4
+ module Querying
5
+ delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
6
+ delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped
7
+ delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
8
+ delegate :find_each, :find_in_batches, :to => :scoped
9
+ delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
10
+ :where, :preload, :eager_load, :includes, :from, :lock, :readonly,
11
+ :having, :create_with, :uniq, :to => :scoped
12
+ delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :to => :scoped
13
+
14
+ # Executes a custom SQL query against your database and returns all the results. The results will
15
+ # be returned as an array with columns requested encapsulated as attributes of the model you call
16
+ # this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in
17
+ # a Product object with the attributes you specified in the SQL query.
18
+ #
19
+ # If you call a complicated SQL query which spans multiple tables the columns specified by the
20
+ # SELECT will be attributes of the model, whether or not they are columns of the corresponding
21
+ # table.
22
+ #
23
+ # The +sql+ parameter is a full SQL query as a string. It will be called as is, there will be
24
+ # no database agnostic conversions performed. This should be a last resort because using, for example,
25
+ # MySQL specific terms will lock you to using that particular database engine or require you to
26
+ # change your call if you switch engines.
27
+ #
28
+ # ==== Examples
29
+ # # A simple SQL query spanning multiple tables
30
+ # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
31
+ # > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
32
+ #
33
+ # # You can use the same string replacement techniques as you can with ActiveRecord#find
34
+ # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
35
+ # > [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...]
36
+ def find_by_sql(sql, binds = [])
37
+ logging_query_plan do
38
+ connection.select_all(sanitize_sql(sql), "#{name} Load", binds).collect! { |record| instantiate(record) }
39
+ end
40
+ end
41
+
42
+ # Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
43
+ # The use of this method should be restricted to complicated SQL queries that can't be executed
44
+ # using the ActiveRecord::Calculations class methods. Look into those before using this.
45
+ #
46
+ # ==== Parameters
47
+ #
48
+ # * +sql+ - An SQL statement which should return a count query from the database, see the example below.
49
+ #
50
+ # ==== Examples
51
+ #
52
+ # Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
53
+ def count_by_sql(sql)
54
+ sql = sanitize_conditions(sql)
55
+ connection.select_value(sql, "#{name} Count").to_i
56
+ end
57
+ end
58
+ end
@@ -22,7 +22,15 @@ module ActiveRecord
22
22
  config.app_middleware.insert_after "::ActionDispatch::Callbacks",
23
23
  "ActiveRecord::ConnectionAdapters::ConnectionManagement"
24
24
 
25
+ config.action_dispatch.rescue_responses.merge!(
26
+ 'ActiveRecord::RecordNotFound' => :not_found,
27
+ 'ActiveRecord::StaleObjectError' => :conflict,
28
+ 'ActiveRecord::RecordInvalid' => :unprocessable_entity,
29
+ 'ActiveRecord::RecordNotSaved' => :unprocessable_entity
30
+ )
31
+
25
32
  rake_tasks do
33
+ require "active_record/base"
26
34
  load "active_record/railties/databases.rake"
27
35
  end
28
36
 
@@ -31,9 +39,14 @@ module ActiveRecord
31
39
  # first time. Also, make it output to STDERR.
32
40
  console do |app|
33
41
  require "active_record/railties/console_sandbox" if app.sandbox?
42
+ require "active_record/base"
34
43
  ActiveRecord::Base.logger = Logger.new(STDERR)
35
44
  end
36
45
 
46
+ runner do |app|
47
+ require "active_record/base"
48
+ end
49
+
37
50
  initializer "active_record.initialize_timezone" do
38
51
  ActiveSupport.on_load(:active_record) do
39
52
  self.time_zone_aware_attributes = true
@@ -65,7 +78,13 @@ module ActiveRecord
65
78
  # and then establishes the connection.
66
79
  initializer "active_record.initialize_database" do |app|
67
80
  ActiveSupport.on_load(:active_record) do
68
- self.configurations = app.config.database_configuration
81
+ db_connection_type = "DATABASE_URL"
82
+ unless ENV['DATABASE_URL']
83
+ db_connection_type = "database.yml"
84
+ self.configurations = app.config.database_configuration
85
+ end
86
+ Rails.logger.info "Connecting to database specified by #{db_connection_type}"
87
+
69
88
  establish_connection
70
89
  end
71
90
  end
@@ -78,15 +97,27 @@ module ActiveRecord
78
97
  end
79
98
  end
80
99
 
81
- initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app|
82
- ActiveSupport.on_load(:active_record) do
83
- ActionDispatch::Reloader.to_cleanup do
84
- ActiveRecord::Base.clear_reloadable_connections!
85
- ActiveRecord::Base.clear_cache!
100
+ initializer "active_record.set_reloader_hooks" do |app|
101
+ hook = lambda do
102
+ ActiveRecord::Base.clear_reloadable_connections!
103
+ ActiveRecord::Base.clear_cache!
104
+ end
105
+
106
+ if app.config.reload_classes_only_on_change
107
+ ActiveSupport.on_load(:active_record) do
108
+ ActionDispatch::Reloader.to_prepare(&hook)
109
+ end
110
+ else
111
+ ActiveSupport.on_load(:active_record) do
112
+ ActionDispatch::Reloader.to_cleanup(&hook)
86
113
  end
87
114
  end
88
115
  end
89
116
 
117
+ initializer "active_record.add_watchable_files" do |app|
118
+ config.watchable_files.concat ["#{app.root}/db/schema.rb", "#{app.root}/db/structure.sql"]
119
+ end
120
+
90
121
  config.after_initialize do
91
122
  ActiveSupport.on_load(:active_record) do
92
123
  instantiate_observers
@@ -96,28 +127,5 @@ module ActiveRecord
96
127
  end
97
128
  end
98
129
  end
99
-
100
- config.after_initialize do
101
- container = :"activerecord.attributes"
102
- lookup = I18n.t(container, :default => {})
103
- if lookup.is_a?(Hash)
104
- lookup.each do |key, value|
105
- if value.is_a?(Hash) && value.any? { |k,v| v.is_a?(Hash) }
106
- $stderr.puts "[DEPRECATION WARNING] Nested I18n namespace lookup under \"#{container}.#{key}\" is no longer supported"
107
- end
108
- end
109
- end
110
-
111
- container = :"activerecord.models"
112
- lookup = I18n.t(container, :default => {})
113
- if lookup.is_a?(Hash)
114
- lookup.each do |key, value|
115
- if value.is_a?(Hash) && !value.key?(:one)
116
- $stderr.puts "[DEPRECATION WARNING] Nested I18n namespace lookup under \"#{container}.#{key}\" is no longer supported"
117
- end
118
- end
119
- end
120
- end
121
-
122
130
  end
123
131
  end
@@ -32,7 +32,9 @@ module ActiveRecord
32
32
 
33
33
  def append_info_to_payload(payload)
34
34
  super
35
- payload[:db_runtime] = db_runtime
35
+ if ActiveRecord::Base.connected?
36
+ payload[:db_runtime] = (db_runtime || 0) + ActiveRecord::LogSubscriber.reset_runtime
37
+ end
36
38
  end
37
39
 
38
40
  module ClassMethods