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,200 @@
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
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
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.current - 1.week)
122
+ # end
123
+ #
124
+ # The example above would be 'frozen' to the <tt>Time.current</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.current - 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
+ # pluck(:title)
165
+ # end
166
+ # end
167
+ #
168
+ # We are able to call the methods like this:
169
+ #
170
+ # Article.published.featured.latest_article
171
+ # Article.featured.titles
172
+ def scope(name, scope_options = {})
173
+ name = name.to_sym
174
+ valid_scope_name?(name)
175
+ extension = Module.new(&Proc.new) if block_given?
176
+
177
+ scope_proc = lambda do |*args|
178
+ options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
179
+ options = scoped.apply_finder_options(options) if options.is_a?(Hash)
180
+
181
+ relation = scoped.merge(options)
182
+
183
+ extension ? relation.extending(extension) : relation
184
+ end
185
+
186
+ singleton_class.send(:redefine_method, name, &scope_proc)
187
+ end
188
+
189
+ protected
190
+
191
+ def valid_scope_name?(name)
192
+ if logger && respond_to?(name, true)
193
+ logger.warn "Creating scope :#{name}. " \
194
+ "Overwriting existing method #{self.name}.#{name}."
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -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
@@ -163,8 +163,9 @@ module ActiveRecord #:nodoc:
163
163
  #
164
164
  # class IHaveMyOwnXML < ActiveRecord::Base
165
165
  # def to_xml(options = {})
166
+ # require 'builder'
166
167
  # options[:indent] ||= 2
167
- # xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
168
+ # xml = options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])
168
169
  # xml.instruct! unless options[:skip_instruct]
169
170
  # xml.level_one do
170
171
  # xml.tag!(:second_level, 'content')
@@ -179,49 +180,7 @@ module ActiveRecord #:nodoc:
179
180
  class XmlSerializer < ActiveModel::Serializers::Xml::Serializer #:nodoc:
180
181
  def initialize(*args)
181
182
  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
183
+ options[:except] = Array.wrap(options[:except]) | Array.wrap(@serializable.class.inheritance_column)
225
184
  end
226
185
 
227
186
  class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc:
@@ -1,13 +1,15 @@
1
+ require 'action_dispatch/middleware/session/abstract_store'
2
+
1
3
  module ActiveRecord
2
4
  # = Active Record Session Store
3
5
  #
4
- # A session store backed by an Active Record class. A default class is
6
+ # A session store backed by an Active Record class. A default class is
5
7
  # provided, but any object duck-typing to an Active Record Session class
6
8
  # with text +session_id+ and +data+ attributes is sufficient.
7
9
  #
8
10
  # The default assumes a +sessions+ tables with columns:
9
11
  # +id+ (numeric primary key),
10
- # +session_id+ (text, or longtext if your session data exceeds 65K), and
12
+ # +session_id+ (string, :limit => 255), and
11
13
  # +data+ (text or longtext; careful if your session data exceeds 65KB).
12
14
  #
13
15
  # The +session_id+ column should always be indexed for speedy lookups.
@@ -23,7 +25,7 @@ module ActiveRecord
23
25
  # ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
24
26
  #
25
27
  # 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
28
+ # having a separate +id+ column if you don't want it. However, you must
27
29
  # set <tt>session.model.id = session.session_id</tt> by hand! A before filter
28
30
  # on ApplicationController is a good place.
29
31
  #
@@ -46,25 +48,25 @@ module ActiveRecord
46
48
  # save
47
49
  # destroy
48
50
  #
49
- # The example SqlBypass class is a generic SQL session store. You may
51
+ # The example SqlBypass class is a generic SQL session store. You may
50
52
  # use it as a basis for high-performance database-specific stores.
51
53
  class SessionStore < ActionDispatch::Session::AbstractStore
52
54
  module ClassMethods # :nodoc:
53
55
  def marshal(data)
54
- ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
56
+ ::Base64.encode64(Marshal.dump(data)) if data
55
57
  end
56
58
 
57
59
  def unmarshal(data)
58
- Marshal.load(ActiveSupport::Base64.decode64(data)) if data
60
+ Marshal.load(::Base64.decode64(data)) if data
59
61
  end
60
62
 
61
63
  def drop_table!
62
- connection_pool.clear_table_cache!(table_name)
64
+ connection.schema_cache.clear_table_cache!(table_name)
63
65
  connection.drop_table table_name
64
66
  end
65
67
 
66
68
  def create_table!
67
- connection_pool.clear_table_cache!(table_name)
69
+ connection.schema_cache.clear_table_cache!(table_name)
68
70
  connection.create_table(table_name) do |t|
69
71
  t.string session_id_column, :limit => 255
70
72
  t.text data_column_name
@@ -79,7 +81,7 @@ module ActiveRecord
79
81
 
80
82
  ##
81
83
  # :singleton-method:
82
- # Customizable data column name. Defaults to 'data'.
84
+ # Customizable data column name. Defaults to 'data'.
83
85
  cattr_accessor :data_column_name
84
86
  self.data_column_name = 'data'
85
87
 
@@ -161,22 +163,22 @@ module ActiveRecord
161
163
  end
162
164
 
163
165
  # A barebones session store which duck-types with the default session
164
- # store but bypasses Active Record and issues SQL directly. This is
166
+ # store but bypasses Active Record and issues SQL directly. This is
165
167
  # an example session model class meant as a basis for your own classes.
166
168
  #
167
169
  # 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,
170
+ # are configurable class attributes. Marshaling and unmarshaling
171
+ # are implemented as class methods that you may override. By default,
170
172
  # marshaling data is
171
173
  #
172
- # ActiveSupport::Base64.encode64(Marshal.dump(data))
174
+ # ::Base64.encode64(Marshal.dump(data))
173
175
  #
174
176
  # and unmarshaling data is
175
177
  #
176
- # Marshal.load(ActiveSupport::Base64.decode64(data))
178
+ # Marshal.load(::Base64.decode64(data))
177
179
  #
178
180
  # This marshaling behavior is intended to store the widest range of
179
- # binary session data in a +text+ column. For higher performance,
181
+ # binary session data in a +text+ column. For higher performance,
180
182
  # store in a +blob+ column instead and forgo the Base64 encoding.
181
183
  class SqlBypass
182
184
  extend ClassMethods
@@ -292,7 +294,7 @@ module ActiveRecord
292
294
  end
293
295
  end
294
296
 
295
- # The class used for session storage. Defaults to
297
+ # The class used for session storage. Defaults to
296
298
  # ActiveRecord::SessionStore::Session
297
299
  cattr_accessor :session_class
298
300
  self.session_class = Session
@@ -0,0 +1,52 @@
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}=", {}) unless send(store_attribute).is_a?(Hash)
40
+ send("#{store_attribute}_will_change!")
41
+ send(store_attribute)[key] = value
42
+ end
43
+
44
+ define_method(key) do
45
+ send("#{store_attribute}=", {}) unless send(store_attribute).is_a?(Hash)
46
+ send(store_attribute)[key]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ 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)