activerecord 3.1.12 → 3.2.0.rc1

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 (99) hide show
  1. data/CHANGELOG.md +6263 -103
  2. data/README.rdoc +2 -2
  3. data/examples/performance.rb +55 -31
  4. data/lib/active_record.rb +28 -2
  5. data/lib/active_record/aggregations.rb +2 -2
  6. data/lib/active_record/associations.rb +82 -69
  7. data/lib/active_record/associations/association.rb +2 -37
  8. data/lib/active_record/associations/association_scope.rb +3 -30
  9. data/lib/active_record/associations/builder/association.rb +6 -4
  10. data/lib/active_record/associations/builder/belongs_to.rb +3 -3
  11. data/lib/active_record/associations/builder/collection_association.rb +2 -2
  12. data/lib/active_record/associations/builder/has_many.rb +4 -4
  13. data/lib/active_record/associations/builder/has_one.rb +5 -6
  14. data/lib/active_record/associations/builder/singular_association.rb +3 -16
  15. data/lib/active_record/associations/collection_association.rb +55 -28
  16. data/lib/active_record/associations/collection_proxy.rb +1 -35
  17. data/lib/active_record/associations/has_many_association.rb +5 -1
  18. data/lib/active_record/associations/has_many_through_association.rb +11 -8
  19. data/lib/active_record/associations/join_dependency.rb +1 -1
  20. data/lib/active_record/associations/preloader/association.rb +3 -1
  21. data/lib/active_record/attribute_assignment.rb +221 -0
  22. data/lib/active_record/attribute_methods.rb +212 -32
  23. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
  24. data/lib/active_record/attribute_methods/dirty.rb +3 -3
  25. data/lib/active_record/attribute_methods/primary_key.rb +62 -25
  26. data/lib/active_record/attribute_methods/read.rb +69 -80
  27. data/lib/active_record/attribute_methods/serialization.rb +89 -0
  28. data/lib/active_record/attribute_methods/time_zone_conversion.rb +9 -14
  29. data/lib/active_record/attribute_methods/write.rb +27 -5
  30. data/lib/active_record/autosave_association.rb +23 -8
  31. data/lib/active_record/base.rb +223 -1712
  32. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +98 -132
  33. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +82 -29
  34. data/lib/active_record/connection_adapters/abstract/database_statements.rb +13 -42
  35. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  36. data/lib/active_record/connection_adapters/abstract/quoting.rb +7 -4
  37. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +36 -25
  38. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +41 -13
  39. data/lib/active_record/connection_adapters/abstract_adapter.rb +78 -43
  40. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +653 -0
  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 +1 -1
  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.rb +31 -76
  53. data/lib/active_record/fixtures/file.rb +65 -0
  54. data/lib/active_record/identity_map.rb +1 -7
  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 +19 -11
  58. data/lib/active_record/locking/pessimistic.rb +1 -1
  59. data/lib/active_record/log_subscriber.rb +3 -3
  60. data/lib/active_record/migration.rb +38 -29
  61. data/lib/active_record/migration/command_recorder.rb +7 -7
  62. data/lib/active_record/model_schema.rb +362 -0
  63. data/lib/active_record/nested_attributes.rb +3 -2
  64. data/lib/active_record/persistence.rb +51 -1
  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 +133 -77
  69. data/lib/active_record/readonly_attributes.rb +26 -0
  70. data/lib/active_record/reflection.rb +7 -15
  71. data/lib/active_record/relation.rb +78 -35
  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 +5 -4
  76. data/lib/active_record/relation/predicate_builder.rb +13 -16
  77. data/lib/active_record/relation/query_methods.rb +59 -4
  78. data/lib/active_record/result.rb +1 -1
  79. data/lib/active_record/sanitization.rb +194 -0
  80. data/lib/active_record/schema_dumper.rb +5 -2
  81. data/lib/active_record/scoping.rb +152 -0
  82. data/lib/active_record/scoping/default.rb +140 -0
  83. data/lib/active_record/scoping/named.rb +202 -0
  84. data/lib/active_record/serialization.rb +1 -43
  85. data/lib/active_record/serializers/xml_serializer.rb +2 -44
  86. data/lib/active_record/session_store.rb +11 -11
  87. data/lib/active_record/store.rb +50 -0
  88. data/lib/active_record/test_case.rb +11 -7
  89. data/lib/active_record/timestamp.rb +16 -3
  90. data/lib/active_record/transactions.rb +5 -5
  91. data/lib/active_record/translation.rb +22 -0
  92. data/lib/active_record/validations.rb +1 -1
  93. data/lib/active_record/validations/associated.rb +5 -4
  94. data/lib/active_record/validations/uniqueness.rb +4 -4
  95. data/lib/active_record/version.rb +3 -3
  96. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
  97. metadata +48 -38
  98. checksums.yaml +0 -7
  99. data/lib/active_record/named_scope.rb +0 -200
@@ -0,0 +1,140 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveRecord
4
+ module Scoping
5
+ module Default
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ # Stores the default scope for the class
10
+ class_attribute :default_scopes, :instance_writer => false
11
+ self.default_scopes = []
12
+ end
13
+
14
+ module ClassMethods
15
+ # Returns a scope for this class without taking into account the default_scope.
16
+ #
17
+ # class Post < ActiveRecord::Base
18
+ # def self.default_scope
19
+ # where :published => true
20
+ # end
21
+ # end
22
+ #
23
+ # Post.all # Fires "SELECT * FROM posts WHERE published = true"
24
+ # Post.unscoped.all # Fires "SELECT * FROM posts"
25
+ #
26
+ # This method also accepts a block meaning that all queries inside the block will
27
+ # not use the default_scope:
28
+ #
29
+ # Post.unscoped {
30
+ # Post.limit(10) # Fires "SELECT * FROM posts LIMIT 10"
31
+ # }
32
+ #
33
+ # It is recommended to use block form of unscoped because chaining unscoped with <tt>scope</tt>
34
+ # does not work. Assuming that <tt>published</tt> is a <tt>scope</tt> following two statements are same.
35
+ #
36
+ # Post.unscoped.published
37
+ # Post.published
38
+ def unscoped #:nodoc:
39
+ block_given? ? relation.scoping { yield } : relation
40
+ end
41
+
42
+ def before_remove_const #:nodoc:
43
+ self.current_scope = nil
44
+ end
45
+
46
+ protected
47
+
48
+ # Use this macro in your model to set a default scope for all operations on
49
+ # the model.
50
+ #
51
+ # class Article < ActiveRecord::Base
52
+ # default_scope where(:published => true)
53
+ # end
54
+ #
55
+ # Article.all # => SELECT * FROM articles WHERE published = true
56
+ #
57
+ # The <tt>default_scope</tt> is also applied while creating/building a record. It is not
58
+ # applied while updating a record.
59
+ #
60
+ # Article.new.published # => true
61
+ # Article.create.published # => true
62
+ #
63
+ # You can also use <tt>default_scope</tt> with a block, in order to have it lazily evaluated:
64
+ #
65
+ # class Article < ActiveRecord::Base
66
+ # default_scope { where(:published_at => Time.now - 1.week) }
67
+ # end
68
+ #
69
+ # (You can also pass any object which responds to <tt>call</tt> to the <tt>default_scope</tt>
70
+ # macro, and it will be called when building the default scope.)
71
+ #
72
+ # If you use multiple <tt>default_scope</tt> declarations in your model then they will
73
+ # be merged together:
74
+ #
75
+ # class Article < ActiveRecord::Base
76
+ # default_scope where(:published => true)
77
+ # default_scope where(:rating => 'G')
78
+ # end
79
+ #
80
+ # Article.all # => SELECT * FROM articles WHERE published = true AND rating = 'G'
81
+ #
82
+ # This is also the case with inheritance and module includes where the parent or module
83
+ # defines a <tt>default_scope</tt> and the child or including class defines a second one.
84
+ #
85
+ # If you need to do more complex things with a default scope, you can alternatively
86
+ # define it as a class method:
87
+ #
88
+ # class Article < ActiveRecord::Base
89
+ # def self.default_scope
90
+ # # Should return a scope, you can call 'super' here etc.
91
+ # end
92
+ # end
93
+ def default_scope(scope = {})
94
+ scope = Proc.new if block_given?
95
+ self.default_scopes = default_scopes + [scope]
96
+ end
97
+
98
+ def build_default_scope #:nodoc:
99
+ if method(:default_scope).owner != ActiveRecord::Scoping::Default::ClassMethods
100
+ evaluate_default_scope { default_scope }
101
+ elsif default_scopes.any?
102
+ evaluate_default_scope do
103
+ default_scopes.inject(relation) do |default_scope, scope|
104
+ if scope.is_a?(Hash)
105
+ default_scope.apply_finder_options(scope)
106
+ elsif !scope.is_a?(Relation) && scope.respond_to?(:call)
107
+ default_scope.merge(scope.call)
108
+ else
109
+ default_scope.merge(scope)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def ignore_default_scope? #:nodoc:
117
+ Thread.current["#{self}_ignore_default_scope"]
118
+ end
119
+
120
+ def ignore_default_scope=(ignore) #:nodoc:
121
+ Thread.current["#{self}_ignore_default_scope"] = ignore
122
+ end
123
+
124
+ # The ignore_default_scope flag is used to prevent an infinite recursion situation where
125
+ # a default scope references a scope which has a default scope which references a scope...
126
+ def evaluate_default_scope
127
+ return if ignore_default_scope?
128
+
129
+ begin
130
+ self.ignore_default_scope = true
131
+ yield
132
+ ensure
133
+ self.ignore_default_scope = false
134
+ end
135
+ end
136
+
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,202 @@
1
+ require 'active_support/core_ext/array'
2
+ require 'active_support/core_ext/hash/except'
3
+ require 'active_support/core_ext/kernel/singleton_class'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/core_ext/class/attribute'
6
+
7
+ module ActiveRecord
8
+ # = Active Record Named \Scopes
9
+ module Scoping
10
+ module Named
11
+ extend ActiveSupport::Concern
12
+
13
+ module ClassMethods
14
+ # Returns an anonymous \scope.
15
+ #
16
+ # posts = Post.scoped
17
+ # posts.size # Fires "select count(*) from posts" and returns the count
18
+ # posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
19
+ #
20
+ # fruits = Fruit.scoped
21
+ # fruits = fruits.where(:color => 'red') if options[:red_only]
22
+ # fruits = fruits.limit(10) if limited?
23
+ #
24
+ # Anonymous \scopes tend to be useful when procedurally generating complex
25
+ # queries, where passing intermediate values (\scopes) around as first-class
26
+ # objects is convenient.
27
+ #
28
+ # You can define a \scope that applies to all finders using
29
+ # ActiveRecord::Base.default_scope.
30
+ def scoped(options = nil)
31
+ if options
32
+ scoped.apply_finder_options(options)
33
+ else
34
+ if current_scope
35
+ current_scope.clone
36
+ else
37
+ scope = relation.clone
38
+ scope.default_scoped = true
39
+ scope
40
+ end
41
+ end
42
+ end
43
+
44
+ ##
45
+ # Collects attributes from scopes that should be applied when creating
46
+ # an AR instance for the particular class this is called on.
47
+ def scope_attributes # :nodoc:
48
+ if current_scope
49
+ current_scope.scope_for_create
50
+ else
51
+ scope = relation.clone
52
+ scope.default_scoped = true
53
+ scope.scope_for_create
54
+ end
55
+ end
56
+
57
+ ##
58
+ # Are there default attributes associated with this scope?
59
+ def scope_attributes? # :nodoc:
60
+ current_scope || default_scopes.any?
61
+ end
62
+
63
+ # Adds a class method for retrieving and querying objects. A \scope represents a narrowing of a database query,
64
+ # such as <tt>where(:color => :red).select('shirts.*').includes(:washing_instructions)</tt>.
65
+ #
66
+ # class Shirt < ActiveRecord::Base
67
+ # scope :red, where(:color => 'red')
68
+ # scope :dry_clean_only, joins(:washing_instructions).where('washing_instructions.dry_clean_only = ?', true)
69
+ # end
70
+ #
71
+ # The above calls to <tt>scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
72
+ # in effect, represents the query <tt>Shirt.where(:color => 'red')</tt>.
73
+ #
74
+ # Note that this is simply 'syntactic sugar' for defining an actual class method:
75
+ #
76
+ # class Shirt < ActiveRecord::Base
77
+ # def self.red
78
+ # where(:color => 'red')
79
+ # end
80
+ # end
81
+ #
82
+ # Unlike <tt>Shirt.find(...)</tt>, however, the object returned by Shirt.red is not an Array; it
83
+ # resembles the association object constructed by a <tt>has_many</tt> declaration. For instance,
84
+ # you can invoke <tt>Shirt.red.first</tt>, <tt>Shirt.red.count</tt>, <tt>Shirt.red.where(:size => 'small')</tt>.
85
+ # Also, just as with the association objects, named \scopes act like an Array, implementing Enumerable;
86
+ # <tt>Shirt.red.each(&block)</tt>, <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt>
87
+ # all behave as if Shirt.red really was an Array.
88
+ #
89
+ # These named \scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce
90
+ # all shirts that are both red and dry clean only.
91
+ # Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt>
92
+ # returns the number of garments for which these criteria obtain. Similarly with
93
+ # <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
94
+ #
95
+ # All \scopes are available as class methods on the ActiveRecord::Base descendant upon which
96
+ # the \scopes were defined. But they are also available to <tt>has_many</tt> associations. If,
97
+ #
98
+ # class Person < ActiveRecord::Base
99
+ # has_many :shirts
100
+ # end
101
+ #
102
+ # then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
103
+ # only shirts.
104
+ #
105
+ # Named \scopes can also be procedural:
106
+ #
107
+ # class Shirt < ActiveRecord::Base
108
+ # scope :colored, lambda { |color| where(:color => color) }
109
+ # end
110
+ #
111
+ # In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
112
+ #
113
+ # On Ruby 1.9 you can use the 'stabby lambda' syntax:
114
+ #
115
+ # scope :colored, ->(color) { where(:color => color) }
116
+ #
117
+ # Note that scopes defined with \scope will be evaluated when they are defined, rather than
118
+ # when they are used. For example, the following would be incorrect:
119
+ #
120
+ # class Post < ActiveRecord::Base
121
+ # scope :recent, where('published_at >= ?', Time.now - 1.week)
122
+ # end
123
+ #
124
+ # The example above would be 'frozen' to the <tt>Time.now</tt> value when the <tt>Post</tt>
125
+ # class was defined, and so the resultant SQL query would always be the same. The correct
126
+ # way to do this would be via a lambda, which will re-evaluate the scope each time
127
+ # it is called:
128
+ #
129
+ # class Post < ActiveRecord::Base
130
+ # scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
131
+ # end
132
+ #
133
+ # Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations:
134
+ #
135
+ # class Shirt < ActiveRecord::Base
136
+ # scope :red, where(:color => 'red') do
137
+ # def dom_id
138
+ # 'red_shirts'
139
+ # end
140
+ # end
141
+ # end
142
+ #
143
+ # Scopes can also be used while creating/building a record.
144
+ #
145
+ # class Article < ActiveRecord::Base
146
+ # scope :published, where(:published => true)
147
+ # end
148
+ #
149
+ # Article.published.new.published # => true
150
+ # Article.published.create.published # => true
151
+ #
152
+ # Class methods on your model are automatically available
153
+ # on scopes. Assuming the following setup:
154
+ #
155
+ # class Article < ActiveRecord::Base
156
+ # scope :published, where(:published => true)
157
+ # scope :featured, where(:featured => true)
158
+ #
159
+ # def self.latest_article
160
+ # order('published_at desc').first
161
+ # end
162
+ #
163
+ # def self.titles
164
+ # map(&:title)
165
+ # end
166
+ #
167
+ # end
168
+ #
169
+ # We are able to call the methods like this:
170
+ #
171
+ # Article.published.featured.latest_article
172
+ # Article.featured.titles
173
+
174
+ def scope(name, scope_options = {})
175
+ name = name.to_sym
176
+ valid_scope_name?(name)
177
+ extension = Module.new(&Proc.new) if block_given?
178
+
179
+ scope_proc = lambda do |*args|
180
+ options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
181
+ options = scoped.apply_finder_options(options) if options.is_a?(Hash)
182
+
183
+ relation = scoped.merge(options)
184
+
185
+ extension ? relation.extending(extension) : relation
186
+ end
187
+
188
+ singleton_class.send(:redefine_method, name, &scope_proc)
189
+ end
190
+
191
+ protected
192
+
193
+ def valid_scope_name?(name)
194
+ if respond_to?(name, true)
195
+ logger.warn "Creating scope :#{name}. " \
196
+ "Overwriting existing method #{self.name}.#{name}."
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+ 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: