activerecord 3.1.11 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/CHANGELOG.md +6294 -97
  2. data/README.rdoc +2 -2
  3. data/examples/performance.rb +55 -31
  4. data/lib/active_record/aggregations.rb +2 -2
  5. data/lib/active_record/associations/association.rb +2 -42
  6. data/lib/active_record/associations/association_scope.rb +3 -30
  7. data/lib/active_record/associations/builder/association.rb +6 -4
  8. data/lib/active_record/associations/builder/belongs_to.rb +3 -3
  9. data/lib/active_record/associations/builder/collection_association.rb +2 -2
  10. data/lib/active_record/associations/builder/has_many.rb +4 -4
  11. data/lib/active_record/associations/builder/has_one.rb +5 -6
  12. data/lib/active_record/associations/builder/singular_association.rb +3 -16
  13. data/lib/active_record/associations/collection_association.rb +55 -28
  14. data/lib/active_record/associations/collection_proxy.rb +1 -35
  15. data/lib/active_record/associations/has_many_association.rb +5 -1
  16. data/lib/active_record/associations/has_many_through_association.rb +11 -8
  17. data/lib/active_record/associations/join_dependency.rb +1 -1
  18. data/lib/active_record/associations/preloader/association.rb +3 -1
  19. data/lib/active_record/associations.rb +82 -69
  20. data/lib/active_record/attribute_assignment.rb +221 -0
  21. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
  22. data/lib/active_record/attribute_methods/dirty.rb +3 -3
  23. data/lib/active_record/attribute_methods/primary_key.rb +62 -25
  24. data/lib/active_record/attribute_methods/read.rb +72 -83
  25. data/lib/active_record/attribute_methods/serialization.rb +93 -0
  26. data/lib/active_record/attribute_methods/time_zone_conversion.rb +9 -14
  27. data/lib/active_record/attribute_methods/write.rb +27 -5
  28. data/lib/active_record/attribute_methods.rb +209 -30
  29. data/lib/active_record/autosave_association.rb +23 -8
  30. data/lib/active_record/base.rb +217 -1709
  31. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +98 -132
  32. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +82 -29
  33. data/lib/active_record/connection_adapters/abstract/database_statements.rb +13 -42
  34. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  35. data/lib/active_record/connection_adapters/abstract/quoting.rb +9 -12
  36. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +36 -25
  37. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +43 -22
  38. data/lib/active_record/connection_adapters/abstract_adapter.rb +78 -43
  39. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +653 -0
  40. data/lib/active_record/connection_adapters/column.rb +2 -2
  41. data/lib/active_record/connection_adapters/mysql2_adapter.rb +138 -578
  42. data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -658
  43. data/lib/active_record/connection_adapters/postgresql_adapter.rb +144 -94
  44. data/lib/active_record/connection_adapters/schema_cache.rb +50 -0
  45. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
  46. data/lib/active_record/connection_adapters/sqlite_adapter.rb +43 -22
  47. data/lib/active_record/counter_cache.rb +4 -3
  48. data/lib/active_record/dynamic_matchers.rb +79 -0
  49. data/lib/active_record/errors.rb +11 -1
  50. data/lib/active_record/explain.rb +83 -0
  51. data/lib/active_record/explain_subscriber.rb +21 -0
  52. data/lib/active_record/fixtures/file.rb +65 -0
  53. data/lib/active_record/fixtures.rb +31 -76
  54. data/lib/active_record/identity_map.rb +4 -11
  55. data/lib/active_record/inheritance.rb +167 -0
  56. data/lib/active_record/integration.rb +49 -0
  57. data/lib/active_record/locking/optimistic.rb +30 -25
  58. data/lib/active_record/locking/pessimistic.rb +23 -1
  59. data/lib/active_record/log_subscriber.rb +3 -3
  60. data/lib/active_record/migration/command_recorder.rb +8 -8
  61. data/lib/active_record/migration.rb +47 -30
  62. data/lib/active_record/model_schema.rb +366 -0
  63. data/lib/active_record/nested_attributes.rb +3 -2
  64. data/lib/active_record/persistence.rb +51 -9
  65. data/lib/active_record/querying.rb +58 -0
  66. data/lib/active_record/railtie.rb +24 -28
  67. data/lib/active_record/railties/controller_runtime.rb +3 -1
  68. data/lib/active_record/railties/databases.rake +134 -77
  69. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  70. data/lib/active_record/readonly_attributes.rb +26 -0
  71. data/lib/active_record/reflection.rb +7 -15
  72. data/lib/active_record/relation/batches.rb +5 -2
  73. data/lib/active_record/relation/calculations.rb +27 -6
  74. data/lib/active_record/relation/delegation.rb +49 -0
  75. data/lib/active_record/relation/finder_methods.rb +6 -5
  76. data/lib/active_record/relation/predicate_builder.rb +12 -19
  77. data/lib/active_record/relation/query_methods.rb +76 -10
  78. data/lib/active_record/relation/spawn_methods.rb +11 -2
  79. data/lib/active_record/relation.rb +77 -34
  80. data/lib/active_record/result.rb +1 -1
  81. data/lib/active_record/sanitization.rb +194 -0
  82. data/lib/active_record/schema_dumper.rb +5 -2
  83. data/lib/active_record/scoping/default.rb +142 -0
  84. data/lib/active_record/scoping/named.rb +202 -0
  85. data/lib/active_record/scoping.rb +152 -0
  86. data/lib/active_record/serialization.rb +1 -43
  87. data/lib/active_record/serializers/xml_serializer.rb +2 -44
  88. data/lib/active_record/session_store.rb +15 -15
  89. data/lib/active_record/store.rb +50 -0
  90. data/lib/active_record/test_case.rb +11 -7
  91. data/lib/active_record/timestamp.rb +16 -3
  92. data/lib/active_record/transactions.rb +5 -5
  93. data/lib/active_record/translation.rb +22 -0
  94. data/lib/active_record/validations/associated.rb +5 -4
  95. data/lib/active_record/validations/uniqueness.rb +4 -4
  96. data/lib/active_record/validations.rb +1 -1
  97. data/lib/active_record/version.rb +2 -2
  98. data/lib/active_record.rb +28 -2
  99. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
  100. data/lib/rails/generators/active_record/migration/templates/migration.rb +9 -3
  101. data/lib/rails/generators/active_record/model/model_generator.rb +5 -1
  102. data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
  103. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
  104. metadata +50 -40
  105. checksums.yaml +0 -7
  106. data/lib/active_record/named_scope.rb +0 -200
@@ -0,0 +1,152 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveRecord
4
+ module Scoping
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Default
9
+ include Named
10
+ end
11
+
12
+ module ClassMethods
13
+ # with_scope lets you apply options to inner block incrementally. It takes a hash and the keys must be
14
+ # <tt>:find</tt> or <tt>:create</tt>. <tt>:find</tt> parameter is <tt>Relation</tt> while
15
+ # <tt>:create</tt> parameters are an attributes hash.
16
+ #
17
+ # class Article < ActiveRecord::Base
18
+ # def self.create_with_scope
19
+ # with_scope(:find => where(:blog_id => 1), :create => { :blog_id => 1 }) do
20
+ # find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
21
+ # a = create(1)
22
+ # a.blog_id # => 1
23
+ # end
24
+ # end
25
+ # end
26
+ #
27
+ # In nested scopings, all previous parameters are overwritten by the innermost rule, with the exception of
28
+ # <tt>where</tt>, <tt>includes</tt>, and <tt>joins</tt> operations in <tt>Relation</tt>, which are merged.
29
+ #
30
+ # <tt>joins</tt> operations are uniqued so multiple scopes can join in the same table without table aliasing
31
+ # problems. If you need to join multiple tables, but still want one of the tables to be uniqued, use the
32
+ # array of strings format for your joins.
33
+ #
34
+ # class Article < ActiveRecord::Base
35
+ # def self.find_with_scope
36
+ # with_scope(:find => where(:blog_id => 1).limit(1), :create => { :blog_id => 1 }) do
37
+ # with_scope(:find => limit(10)) do
38
+ # all # => SELECT * from articles WHERE blog_id = 1 LIMIT 10
39
+ # end
40
+ # with_scope(:find => where(:author_id => 3)) do
41
+ # all # => SELECT * from articles WHERE blog_id = 1 AND author_id = 3 LIMIT 1
42
+ # end
43
+ # end
44
+ # end
45
+ # end
46
+ #
47
+ # You can ignore any previous scopings by using the <tt>with_exclusive_scope</tt> method.
48
+ #
49
+ # class Article < ActiveRecord::Base
50
+ # def self.find_with_exclusive_scope
51
+ # with_scope(:find => where(:blog_id => 1).limit(1)) do
52
+ # with_exclusive_scope(:find => limit(10)) do
53
+ # all # => SELECT * from articles LIMIT 10
54
+ # end
55
+ # end
56
+ # end
57
+ # end
58
+ #
59
+ # *Note*: the +:find+ scope also has effect on update and deletion methods, like +update_all+ and +delete_all+.
60
+ def with_scope(scope = {}, action = :merge, &block)
61
+ # If another Active Record class has been passed in, get its current scope
62
+ scope = scope.current_scope if !scope.is_a?(Relation) && scope.respond_to?(:current_scope)
63
+
64
+ previous_scope = self.current_scope
65
+
66
+ if scope.is_a?(Hash)
67
+ # Dup first and second level of hash (method and params).
68
+ scope = scope.dup
69
+ scope.each do |method, params|
70
+ scope[method] = params.dup unless params == true
71
+ end
72
+
73
+ scope.assert_valid_keys([ :find, :create ])
74
+ relation = construct_finder_arel(scope[:find] || {})
75
+ relation.default_scoped = true unless action == :overwrite
76
+
77
+ if previous_scope && previous_scope.create_with_value && scope[:create]
78
+ scope_for_create = if action == :merge
79
+ previous_scope.create_with_value.merge(scope[:create])
80
+ else
81
+ scope[:create]
82
+ end
83
+
84
+ relation = relation.create_with(scope_for_create)
85
+ else
86
+ scope_for_create = scope[:create]
87
+ scope_for_create ||= previous_scope.create_with_value if previous_scope
88
+ relation = relation.create_with(scope_for_create) if scope_for_create
89
+ end
90
+
91
+ scope = relation
92
+ end
93
+
94
+ scope = previous_scope.merge(scope) if previous_scope && action == :merge
95
+
96
+ self.current_scope = scope
97
+ begin
98
+ yield
99
+ ensure
100
+ self.current_scope = previous_scope
101
+ end
102
+ end
103
+
104
+ protected
105
+
106
+ # Works like with_scope, but discards any nested properties.
107
+ def with_exclusive_scope(method_scoping = {}, &block)
108
+ if method_scoping.values.any? { |e| e.is_a?(ActiveRecord::Relation) }
109
+ raise ArgumentError, <<-MSG
110
+ New finder API can not be used with_exclusive_scope. You can either call unscoped to get an anonymous scope not bound to the default_scope:
111
+
112
+ User.unscoped.where(:active => true)
113
+
114
+ Or call unscoped with a block:
115
+
116
+ User.unscoped do
117
+ User.where(:active => true).all
118
+ end
119
+
120
+ MSG
121
+ end
122
+ with_scope(method_scoping, :overwrite, &block)
123
+ end
124
+
125
+ def current_scope #:nodoc:
126
+ Thread.current["#{self}_current_scope"]
127
+ end
128
+
129
+ def current_scope=(scope) #:nodoc:
130
+ Thread.current["#{self}_current_scope"] = scope
131
+ end
132
+
133
+ private
134
+
135
+ def construct_finder_arel(options = {}, scope = nil)
136
+ relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : options
137
+ relation = scope.merge(relation) if scope
138
+ relation
139
+ end
140
+
141
+ end
142
+
143
+ def populate_with_current_scope_attributes
144
+ return unless self.class.scope_attributes?
145
+
146
+ self.class.scope_attributes.each do |att,value|
147
+ send("#{att}=", value) if respond_to?("#{att}=")
148
+ end
149
+ end
150
+
151
+ end
152
+ end
@@ -10,50 +10,8 @@ module ActiveRecord #:nodoc:
10
10
  options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
11
11
  options[:except] |= Array.wrap(self.class.inheritance_column)
12
12
 
13
- hash = super(options)
14
-
15
- serializable_add_includes(options) do |association, records, opts|
16
- hash[association] = records.is_a?(Enumerable) ?
17
- records.map { |r| r.serializable_hash(opts) } :
18
- records.serializable_hash(opts)
19
- end
20
-
21
- hash
13
+ super(options)
22
14
  end
23
-
24
- private
25
- # Add associations specified via the <tt>:include</tt> option.
26
- #
27
- # Expects a block that takes as arguments:
28
- # +association+ - name of the association
29
- # +records+ - the association record(s) to be serialized
30
- # +opts+ - options for the association records
31
- def serializable_add_includes(options = {})
32
- return unless include_associations = options.delete(:include)
33
-
34
- base_only_or_except = { :except => options[:except],
35
- :only => options[:only] }
36
-
37
- include_has_options = include_associations.is_a?(Hash)
38
- associations = include_has_options ? include_associations.keys : Array.wrap(include_associations)
39
-
40
- associations.each do |association|
41
- records = case self.class.reflect_on_association(association).macro
42
- when :has_many, :has_and_belongs_to_many
43
- send(association).to_a
44
- when :has_one, :belongs_to
45
- send(association)
46
- end
47
-
48
- if records
49
- association_options = include_has_options ? include_associations[association] : base_only_or_except
50
- opts = options.merge(association_options)
51
- yield(association, records, opts)
52
- end
53
- end
54
-
55
- options[:include] = include_associations
56
- end
57
15
  end
58
16
  end
59
17
 
@@ -75,7 +75,7 @@ module ActiveRecord #:nodoc:
75
75
  # </firm>
76
76
  #
77
77
  # Additionally, the record being serialized will be passed to a Proc's second
78
- # parameter. This allows for ad hoc additions to the resultant document that
78
+ # parameter. This allows for ad hoc additions to the resultant document that
79
79
  # incorporate the context of the record being serialized. And by leveraging the
80
80
  # closure created by a Proc, to_xml can be used to add elements that normally fall
81
81
  # outside of the scope of the model -- for example, generating and appending URLs
@@ -179,49 +179,7 @@ module ActiveRecord #:nodoc:
179
179
  class XmlSerializer < ActiveModel::Serializers::Xml::Serializer #:nodoc:
180
180
  def initialize(*args)
181
181
  super
182
- options[:except] |= Array.wrap(@serializable.class.inheritance_column)
183
- end
184
-
185
- def add_extra_behavior
186
- add_includes
187
- end
188
-
189
- def add_includes
190
- procs = options.delete(:procs)
191
- @serializable.send(:serializable_add_includes, options) do |association, records, opts|
192
- add_associations(association, records, opts)
193
- end
194
- options[:procs] = procs
195
- end
196
-
197
- # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well.
198
- def add_associations(association, records, opts)
199
- association_name = association.to_s.singularize
200
- merged_options = options.merge(opts).merge!(:root => association_name, :skip_instruct => true)
201
-
202
- if records.is_a?(Enumerable)
203
- tag = ActiveSupport::XmlMini.rename_key(association.to_s, options)
204
- type = options[:skip_types] ? { } : {:type => "array"}
205
-
206
- if records.empty?
207
- @builder.tag!(tag, type)
208
- else
209
- @builder.tag!(tag, type) do
210
- records.each do |record|
211
- if options[:skip_types]
212
- record_type = {}
213
- else
214
- record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
215
- record_type = {:type => record_class}
216
- end
217
-
218
- record.to_xml merged_options.merge(record_type)
219
- end
220
- end
221
- end
222
- elsif record = @serializable.send(association)
223
- record.to_xml(merged_options)
224
- end
182
+ options[:except] = Array.wrap(options[:except]) | Array.wrap(@serializable.class.inheritance_column)
225
183
  end
226
184
 
227
185
  class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc:
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  # = Active Record Session Store
3
3
  #
4
- # A session store backed by an Active Record class. A default class is
4
+ # A session store backed by an Active Record class. A default class is
5
5
  # provided, but any object duck-typing to an Active Record Session class
6
6
  # with text +session_id+ and +data+ attributes is sufficient.
7
7
  #
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
  # ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
24
24
  #
25
25
  # Note that setting the primary key to the +session_id+ frees you from
26
- # having a separate +id+ column if you don't want it. However, you must
26
+ # having a separate +id+ column if you don't want it. However, you must
27
27
  # set <tt>session.model.id = session.session_id</tt> by hand! A before filter
28
28
  # on ApplicationController is a good place.
29
29
  #
@@ -46,25 +46,25 @@ module ActiveRecord
46
46
  # save
47
47
  # destroy
48
48
  #
49
- # The example SqlBypass class is a generic SQL session store. You may
49
+ # The example SqlBypass class is a generic SQL session store. You may
50
50
  # use it as a basis for high-performance database-specific stores.
51
51
  class SessionStore < ActionDispatch::Session::AbstractStore
52
52
  module ClassMethods # :nodoc:
53
53
  def marshal(data)
54
- ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
54
+ ::Base64.encode64(Marshal.dump(data)) if data
55
55
  end
56
56
 
57
57
  def unmarshal(data)
58
- Marshal.load(ActiveSupport::Base64.decode64(data)) if data
58
+ Marshal.load(::Base64.decode64(data)) if data
59
59
  end
60
60
 
61
61
  def drop_table!
62
- connection_pool.clear_table_cache!(table_name)
62
+ connection.schema_cache.clear_table_cache!(table_name)
63
63
  connection.drop_table table_name
64
64
  end
65
65
 
66
66
  def create_table!
67
- connection_pool.clear_table_cache!(table_name)
67
+ connection.schema_cache.clear_table_cache!(table_name)
68
68
  connection.create_table(table_name) do |t|
69
69
  t.string session_id_column, :limit => 255
70
70
  t.text data_column_name
@@ -79,7 +79,7 @@ module ActiveRecord
79
79
 
80
80
  ##
81
81
  # :singleton-method:
82
- # Customizable data column name. Defaults to 'data'.
82
+ # Customizable data column name. Defaults to 'data'.
83
83
  cattr_accessor :data_column_name
84
84
  self.data_column_name = 'data'
85
85
 
@@ -161,22 +161,22 @@ module ActiveRecord
161
161
  end
162
162
 
163
163
  # A barebones session store which duck-types with the default session
164
- # store but bypasses Active Record and issues SQL directly. This is
164
+ # store but bypasses Active Record and issues SQL directly. This is
165
165
  # an example session model class meant as a basis for your own classes.
166
166
  #
167
167
  # The database connection, table name, and session id and data columns
168
- # are configurable class attributes. Marshaling and unmarshaling
169
- # are implemented as class methods that you may override. By default,
168
+ # are configurable class attributes. Marshaling and unmarshaling
169
+ # are implemented as class methods that you may override. By default,
170
170
  # marshaling data is
171
171
  #
172
- # ActiveSupport::Base64.encode64(Marshal.dump(data))
172
+ # ::Base64.encode64(Marshal.dump(data))
173
173
  #
174
174
  # and unmarshaling data is
175
175
  #
176
- # Marshal.load(ActiveSupport::Base64.decode64(data))
176
+ # Marshal.load(::Base64.decode64(data))
177
177
  #
178
178
  # This marshaling behavior is intended to store the widest range of
179
- # binary session data in a +text+ column. For higher performance,
179
+ # binary session data in a +text+ column. For higher performance,
180
180
  # store in a +blob+ column instead and forgo the Base64 encoding.
181
181
  class SqlBypass
182
182
  extend ClassMethods
@@ -292,7 +292,7 @@ module ActiveRecord
292
292
  end
293
293
  end
294
294
 
295
- # The class used for session storage. Defaults to
295
+ # The class used for session storage. Defaults to
296
296
  # ActiveRecord::SessionStore::Session
297
297
  cattr_accessor :session_class
298
298
  self.session_class = Session
@@ -0,0 +1,50 @@
1
+ module ActiveRecord
2
+ # Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column.
3
+ # It's like a simple key/value store backed into your record when you don't care about being able to
4
+ # query that store outside the context of a single record.
5
+ #
6
+ # You can then declare accessors to this store that are then accessible just like any other attribute
7
+ # of the model. This is very helpful for easily exposing store keys to a form or elsewhere that's
8
+ # already built around just accessing attributes on the model.
9
+ #
10
+ # Make sure that you declare the database column used for the serialized store as a text, so there's
11
+ # plenty of room.
12
+ #
13
+ # Examples:
14
+ #
15
+ # class User < ActiveRecord::Base
16
+ # store :settings, accessors: [ :color, :homepage ]
17
+ # end
18
+ #
19
+ # u = User.new(color: 'black', homepage: '37signals.com')
20
+ # u.color # Accessor stored attribute
21
+ # u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor
22
+ #
23
+ # # Add additional accessors to an existing store through store_accessor
24
+ # class SuperUser < User
25
+ # store_accessor :settings, :privileges, :servants
26
+ # end
27
+ module Store
28
+ extend ActiveSupport::Concern
29
+
30
+ module ClassMethods
31
+ def store(store_attribute, options = {})
32
+ serialize store_attribute, Hash
33
+ store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
34
+ end
35
+
36
+ def store_accessor(store_attribute, *keys)
37
+ Array(keys).flatten.each do |key|
38
+ define_method("#{key}=") do |value|
39
+ send(store_attribute)[key] = value
40
+ send("#{store_attribute}_will_change!")
41
+ end
42
+
43
+ define_method(key) do
44
+ send(store_attribute)[key]
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -13,7 +13,7 @@ module ActiveRecord
13
13
  ActiveRecord::IdentityMap.clear
14
14
  end
15
15
 
16
- # Backport skip to Ruby 1.8. test/unit doesn't support it, so just
16
+ # Backport skip to Ruby 1.8. test/unit doesn't support it, so just
17
17
  # make it a noop.
18
18
  unless instance_methods.map(&:to_s).include?("skip")
19
19
  def skip(message)
@@ -31,26 +31,30 @@ module ActiveRecord
31
31
  end
32
32
 
33
33
  def assert_sql(*patterns_to_match)
34
- $queries_executed = []
34
+ ActiveRecord::SQLCounter.log = []
35
35
  yield
36
- $queries_executed
36
+ ActiveRecord::SQLCounter.log
37
37
  ensure
38
38
  failed_patterns = []
39
39
  patterns_to_match.each do |pattern|
40
- failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
40
+ failed_patterns << pattern unless ActiveRecord::SQLCounter.log.any?{ |sql| pattern === sql }
41
41
  end
42
- assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map{ |p| p.inspect }.join(', ')} not found.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
42
+ assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map{ |p| p.inspect }.join(', ')} not found.#{ActiveRecord::SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{ActiveRecord::SQLCounter.log.join("\n")}"}"
43
43
  end
44
44
 
45
45
  def assert_queries(num = 1)
46
- $queries_executed = []
46
+ ActiveRecord::SQLCounter.log = []
47
47
  yield
48
48
  ensure
49
- assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
49
+ assert_equal num, ActiveRecord::SQLCounter.log.size, "#{ActiveRecord::SQLCounter.log.size} instead of #{num} queries were executed.#{ActiveRecord::SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{ActiveRecord::SQLCounter.log.join("\n")}"}"
50
50
  end
51
51
 
52
52
  def assert_no_queries(&block)
53
+ prev_ignored_sql = ActiveRecord::SQLCounter.ignored_sql
54
+ ActiveRecord::SQLCounter.ignored_sql = []
53
55
  assert_queries(0, &block)
56
+ ensure
57
+ ActiveRecord::SQLCounter.ignored_sql = prev_ignored_sql
54
58
  end
55
59
 
56
60
  def with_kcode(kcode)
@@ -33,10 +33,14 @@ module ActiveRecord
33
33
  extend ActiveSupport::Concern
34
34
 
35
35
  included do
36
- class_attribute :record_timestamps, :instance_writer => false
36
+ class_attribute :record_timestamps
37
37
  self.record_timestamps = true
38
38
  end
39
39
 
40
+ def initialize_dup(other)
41
+ clear_timestamp_attributes
42
+ end
43
+
40
44
  private
41
45
 
42
46
  def create #:nodoc:
@@ -44,7 +48,9 @@ module ActiveRecord
44
48
  current_time = current_time_from_proper_timezone
45
49
 
46
50
  all_timestamp_attributes.each do |column|
47
- write_attribute(column.to_s, current_time) if respond_to?(column) && self.send(column).nil?
51
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
52
+ write_attribute(column.to_s, current_time)
53
+ end
48
54
  end
49
55
  end
50
56
 
@@ -95,6 +101,13 @@ module ActiveRecord
95
101
  def current_time_from_proper_timezone #:nodoc:
96
102
  self.class.default_timezone == :utc ? Time.now.utc : Time.now
97
103
  end
104
+
105
+ # Clear attributes and changed_attributes
106
+ def clear_timestamp_attributes
107
+ all_timestamp_attributes_in_model.each do |attribute_name|
108
+ self[attribute_name] = nil
109
+ changed_attributes.delete(attribute_name)
110
+ end
111
+ end
98
112
  end
99
113
  end
100
-
@@ -301,7 +301,7 @@ module ActiveRecord
301
301
  protected
302
302
 
303
303
  # Save the new record state and id of a record so it can be restored later if a transaction fails.
304
- def remember_transaction_record_state #:nodoc
304
+ def remember_transaction_record_state #:nodoc:
305
305
  @_start_transaction_state ||= {}
306
306
  @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
307
307
  unless @_start_transaction_state.include?(:new_record)
@@ -314,7 +314,7 @@ module ActiveRecord
314
314
  end
315
315
 
316
316
  # Clear the new record state and id of a record.
317
- def clear_transaction_record_state #:nodoc
317
+ def clear_transaction_record_state #:nodoc:
318
318
  if defined?(@_start_transaction_state)
319
319
  @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
320
320
  remove_instance_variable(:@_start_transaction_state) if @_start_transaction_state[:level] < 1
@@ -322,7 +322,7 @@ module ActiveRecord
322
322
  end
323
323
 
324
324
  # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
325
- def restore_transaction_record_state(force = false) #:nodoc
325
+ def restore_transaction_record_state(force = false) #:nodoc:
326
326
  if defined?(@_start_transaction_state)
327
327
  @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
328
328
  if @_start_transaction_state[:level] < 1
@@ -341,12 +341,12 @@ module ActiveRecord
341
341
  end
342
342
 
343
343
  # Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.
344
- def transaction_record_state(state) #:nodoc
344
+ def transaction_record_state(state) #:nodoc:
345
345
  @_start_transaction_state[state] if defined?(@_start_transaction_state)
346
346
  end
347
347
 
348
348
  # Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
349
- def transaction_include_action?(action) #:nodoc
349
+ def transaction_include_action?(action) #:nodoc:
350
350
  case action
351
351
  when :create
352
352
  transaction_record_state(:new_record)
@@ -0,0 +1,22 @@
1
+ module ActiveRecord
2
+ module Translation
3
+ include ActiveModel::Translation
4
+
5
+ # Set the lookup ancestors for ActiveModel.
6
+ def lookup_ancestors #:nodoc:
7
+ klass = self
8
+ classes = [klass]
9
+ return classes if klass == ActiveRecord::Base
10
+
11
+ while klass != klass.base_class
12
+ classes << klass = klass.superclass
13
+ end
14
+ classes
15
+ end
16
+
17
+ # Set the i18n scope to overwrite ActiveModel.
18
+ def i18n_scope #:nodoc:
19
+ :activerecord
20
+ end
21
+ end
22
+ end
@@ -2,8 +2,9 @@ module ActiveRecord
2
2
  module Validations
3
3
  class AssociatedValidator < ActiveModel::EachValidator
4
4
  def validate_each(record, attribute, value)
5
- return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
6
- record.errors.add(attribute, :invalid, options.merge(:value => value))
5
+ if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
6
+ record.errors.add(attribute, :invalid, options.merge(:value => value))
7
+ end
7
8
  end
8
9
  end
9
10
 
@@ -29,10 +30,10 @@ module ActiveRecord
29
30
  # validation contexts by default (+nil+), other options are <tt>:create</tt>
30
31
  # and <tt>:update</tt>.
31
32
  # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
32
- # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
33
+ # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
33
34
  # method, proc or string should return or evaluate to a true or false value.
34
35
  # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
35
- # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
36
+ # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
36
37
  # method, proc or string should return or evaluate to a true or false value.
37
38
  def validates_associated(*attr_names)
38
39
  validates_with AssociatedValidator, _merge_attributes(attr_names)
@@ -57,8 +57,8 @@ module ActiveRecord
57
57
  value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
58
58
 
59
59
  if !options[:case_sensitive] && value && column.text?
60
- # will use SQL LOWER function before comparison
61
- relation = table[attribute].lower.eq(table.lower(value))
60
+ # 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)
62
62
  else
63
63
  value = klass.connection.case_sensitive_modifier(value)
64
64
  relation = table[attribute].eq(value)
@@ -83,7 +83,7 @@ module ActiveRecord
83
83
  # validates_uniqueness_of :user_name, :scope => :account_id
84
84
  # end
85
85
  #
86
- # Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
86
+ # Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
87
87
  # per semester for a particular class.
88
88
  #
89
89
  # class TeacherSchedule < ActiveRecord::Base
@@ -105,7 +105,7 @@ module ActiveRecord
105
105
  # The method, proc or string should return or evaluate to a true or false value.
106
106
  # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
107
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
108
+ # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, proc or string should
109
109
  # return or evaluate to a true or false value.
110
110
  #
111
111
  # === Concurrency and integrity
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  # = Active Record RecordInvalid
3
3
  #
4
- # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
4
+ # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
5
5
  # +record+ method to retrieve the record which did not validate.
6
6
  #
7
7
  # begin
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 1
5
- TINY = 11
4
+ MINOR = 2
5
+ TINY = 0
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')