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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.12
4
+ version: 3.2.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-18 00:00:00.000000000 Z
11
+ date: 2016-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,54 +16,54 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.1.12
19
+ version: 3.2.22.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.1.12
26
+ version: 3.2.22.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 3.1.12
33
+ version: 3.2.22.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 3.1.12
40
+ version: 3.2.22.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: arel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.2.3
47
+ version: 3.0.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.2.3
54
+ version: 3.0.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: tzinfo
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.3.29
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.3.29
69
69
  description: Databases on Rails. Build a persistent domain model by mapping database
@@ -81,7 +81,9 @@ files:
81
81
  - examples/associations.png
82
82
  - examples/performance.rb
83
83
  - examples/simple.rb
84
+ - lib/active_record.rb
84
85
  - lib/active_record/aggregations.rb
86
+ - lib/active_record/associations.rb
85
87
  - lib/active_record/associations/alias_tracker.rb
86
88
  - lib/active_record/associations/association.rb
87
89
  - lib/active_record/associations/association_scope.rb
@@ -101,11 +103,12 @@ files:
101
103
  - lib/active_record/associations/has_many_through_association.rb
102
104
  - lib/active_record/associations/has_one_association.rb
103
105
  - lib/active_record/associations/has_one_through_association.rb
106
+ - lib/active_record/associations/join_dependency.rb
104
107
  - lib/active_record/associations/join_dependency/join_association.rb
105
108
  - lib/active_record/associations/join_dependency/join_base.rb
106
109
  - lib/active_record/associations/join_dependency/join_part.rb
107
- - lib/active_record/associations/join_dependency.rb
108
110
  - lib/active_record/associations/join_helper.rb
111
+ - lib/active_record/associations/preloader.rb
109
112
  - lib/active_record/associations/preloader/association.rb
110
113
  - lib/active_record/associations/preloader/belongs_to.rb
111
114
  - lib/active_record/associations/preloader/collection_association.rb
@@ -116,18 +119,19 @@ files:
116
119
  - lib/active_record/associations/preloader/has_one_through.rb
117
120
  - lib/active_record/associations/preloader/singular_association.rb
118
121
  - lib/active_record/associations/preloader/through_association.rb
119
- - lib/active_record/associations/preloader.rb
120
122
  - lib/active_record/associations/singular_association.rb
121
123
  - lib/active_record/associations/through_association.rb
122
- - lib/active_record/associations.rb
124
+ - lib/active_record/attribute_assignment.rb
125
+ - lib/active_record/attribute_methods.rb
123
126
  - lib/active_record/attribute_methods/before_type_cast.rb
127
+ - lib/active_record/attribute_methods/deprecated_underscore_read.rb
124
128
  - lib/active_record/attribute_methods/dirty.rb
125
129
  - lib/active_record/attribute_methods/primary_key.rb
126
130
  - lib/active_record/attribute_methods/query.rb
127
131
  - lib/active_record/attribute_methods/read.rb
132
+ - lib/active_record/attribute_methods/serialization.rb
128
133
  - lib/active_record/attribute_methods/time_zone_conversion.rb
129
134
  - lib/active_record/attribute_methods/write.rb
130
- - lib/active_record/attribute_methods.rb
131
135
  - lib/active_record/autosave_association.rb
132
136
  - lib/active_record/base.rb
133
137
  - lib/active_record/callbacks.rb
@@ -141,60 +145,77 @@ files:
141
145
  - lib/active_record/connection_adapters/abstract/schema_definitions.rb
142
146
  - lib/active_record/connection_adapters/abstract/schema_statements.rb
143
147
  - lib/active_record/connection_adapters/abstract_adapter.rb
148
+ - lib/active_record/connection_adapters/abstract_mysql_adapter.rb
144
149
  - lib/active_record/connection_adapters/column.rb
145
150
  - lib/active_record/connection_adapters/mysql2_adapter.rb
146
151
  - lib/active_record/connection_adapters/mysql_adapter.rb
147
152
  - lib/active_record/connection_adapters/postgresql_adapter.rb
153
+ - lib/active_record/connection_adapters/schema_cache.rb
148
154
  - lib/active_record/connection_adapters/sqlite3_adapter.rb
149
155
  - lib/active_record/connection_adapters/sqlite_adapter.rb
150
156
  - lib/active_record/connection_adapters/statement_pool.rb
151
157
  - lib/active_record/counter_cache.rb
152
158
  - lib/active_record/dynamic_finder_match.rb
159
+ - lib/active_record/dynamic_matchers.rb
153
160
  - lib/active_record/dynamic_scope_match.rb
154
161
  - lib/active_record/errors.rb
162
+ - lib/active_record/explain.rb
163
+ - lib/active_record/explain_subscriber.rb
155
164
  - lib/active_record/fixtures.rb
165
+ - lib/active_record/fixtures/file.rb
156
166
  - lib/active_record/identity_map.rb
167
+ - lib/active_record/inheritance.rb
168
+ - lib/active_record/integration.rb
157
169
  - lib/active_record/locale/en.yml
158
170
  - lib/active_record/locking/optimistic.rb
159
171
  - lib/active_record/locking/pessimistic.rb
160
172
  - lib/active_record/log_subscriber.rb
161
- - lib/active_record/migration/command_recorder.rb
162
173
  - lib/active_record/migration.rb
163
- - lib/active_record/named_scope.rb
174
+ - lib/active_record/migration/command_recorder.rb
175
+ - lib/active_record/model_schema.rb
164
176
  - lib/active_record/nested_attributes.rb
165
177
  - lib/active_record/observer.rb
166
178
  - lib/active_record/persistence.rb
167
179
  - lib/active_record/query_cache.rb
180
+ - lib/active_record/querying.rb
168
181
  - lib/active_record/railtie.rb
169
182
  - lib/active_record/railties/console_sandbox.rb
170
183
  - lib/active_record/railties/controller_runtime.rb
171
184
  - lib/active_record/railties/databases.rake
172
185
  - lib/active_record/railties/jdbcmysql_error.rb
186
+ - lib/active_record/readonly_attributes.rb
173
187
  - lib/active_record/reflection.rb
188
+ - lib/active_record/relation.rb
174
189
  - lib/active_record/relation/batches.rb
175
190
  - lib/active_record/relation/calculations.rb
191
+ - lib/active_record/relation/delegation.rb
176
192
  - lib/active_record/relation/finder_methods.rb
177
193
  - lib/active_record/relation/predicate_builder.rb
178
194
  - lib/active_record/relation/query_methods.rb
179
195
  - lib/active_record/relation/spawn_methods.rb
180
- - lib/active_record/relation.rb
181
196
  - lib/active_record/result.rb
197
+ - lib/active_record/sanitization.rb
182
198
  - lib/active_record/schema.rb
183
199
  - lib/active_record/schema_dumper.rb
200
+ - lib/active_record/scoping.rb
201
+ - lib/active_record/scoping/default.rb
202
+ - lib/active_record/scoping/named.rb
184
203
  - lib/active_record/serialization.rb
185
204
  - lib/active_record/serializers/xml_serializer.rb
186
205
  - lib/active_record/session_store.rb
206
+ - lib/active_record/store.rb
187
207
  - lib/active_record/test_case.rb
188
208
  - lib/active_record/timestamp.rb
189
209
  - lib/active_record/transactions.rb
210
+ - lib/active_record/translation.rb
211
+ - lib/active_record/validations.rb
190
212
  - lib/active_record/validations/associated.rb
191
213
  - lib/active_record/validations/uniqueness.rb
192
- - lib/active_record/validations.rb
193
214
  - lib/active_record/version.rb
194
- - lib/active_record.rb
215
+ - lib/rails/generators/active_record.rb
216
+ - lib/rails/generators/active_record/migration.rb
195
217
  - lib/rails/generators/active_record/migration/migration_generator.rb
196
218
  - lib/rails/generators/active_record/migration/templates/migration.rb
197
- - lib/rails/generators/active_record/migration.rb
198
219
  - lib/rails/generators/active_record/model/model_generator.rb
199
220
  - lib/rails/generators/active_record/model/templates/migration.rb
200
221
  - lib/rails/generators/active_record/model/templates/model.rb
@@ -203,29 +224,29 @@ files:
203
224
  - lib/rails/generators/active_record/observer/templates/observer.rb
204
225
  - lib/rails/generators/active_record/session_migration/session_migration_generator.rb
205
226
  - lib/rails/generators/active_record/session_migration/templates/migration.rb
206
- - lib/rails/generators/active_record.rb
207
227
  homepage: http://www.rubyonrails.org
208
- licenses: []
228
+ licenses:
229
+ - MIT
209
230
  metadata: {}
210
231
  post_install_message:
211
232
  rdoc_options:
212
- - --main
233
+ - "--main"
213
234
  - README.rdoc
214
235
  require_paths:
215
236
  - lib
216
237
  required_ruby_version: !ruby/object:Gem::Requirement
217
238
  requirements:
218
- - - '>='
239
+ - - ">="
219
240
  - !ruby/object:Gem::Version
220
241
  version: 1.8.7
221
242
  required_rubygems_version: !ruby/object:Gem::Requirement
222
243
  requirements:
223
- - - '>='
244
+ - - ">="
224
245
  - !ruby/object:Gem::Version
225
246
  version: '0'
226
247
  requirements: []
227
248
  rubyforge_project:
228
- rubygems_version: 2.0.2
249
+ rubygems_version: 2.5.1
229
250
  signing_key:
230
251
  specification_version: 4
231
252
  summary: Object-relational mapper framework (part of Rails).
@@ -1,200 +0,0 @@
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 NamedScope
10
- extend ActiveSupport::Concern
11
-
12
- module ClassMethods
13
- # Returns an anonymous \scope.
14
- #
15
- # posts = Post.scoped
16
- # posts.size # Fires "select count(*) from posts" and returns the count
17
- # posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
18
- #
19
- # fruits = Fruit.scoped
20
- # fruits = fruits.where(:colour => 'red') if options[:red_only]
21
- # fruits = fruits.limit(10) if limited?
22
- #
23
- # Anonymous \scopes tend to be useful when procedurally generating complex
24
- # queries, where passing intermediate values (\scopes) around as first-class
25
- # objects is convenient.
26
- #
27
- # You can define a \scope that applies to all finders using
28
- # ActiveRecord::Base.default_scope.
29
- def scoped(options = nil)
30
- if options
31
- scoped.apply_finder_options(options)
32
- else
33
- if current_scope
34
- current_scope.clone
35
- else
36
- scope = relation.clone
37
- scope.default_scoped = true
38
- scope
39
- end
40
- end
41
- end
42
-
43
- ##
44
- # Collects attributes from scopes that should be applied when creating
45
- # an AR instance for the particular class this is called on.
46
- def scope_attributes # :nodoc:
47
- if current_scope
48
- current_scope.scope_for_create
49
- else
50
- scope = relation.clone
51
- scope.default_scoped = true
52
- scope.scope_for_create
53
- end
54
- end
55
-
56
- ##
57
- # Are there default attributes associated with this scope?
58
- def scope_attributes? # :nodoc:
59
- current_scope || default_scopes.any?
60
- end
61
-
62
- # Adds a class method for retrieving and querying objects. A \scope represents a narrowing of a database query,
63
- # such as <tt>where(:color => :red).select('shirts.*').includes(:washing_instructions)</tt>.
64
- #
65
- # class Shirt < ActiveRecord::Base
66
- # scope :red, where(:color => 'red')
67
- # scope :dry_clean_only, joins(:washing_instructions).where('washing_instructions.dry_clean_only = ?', true)
68
- # end
69
- #
70
- # The above calls to <tt>scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
71
- # in effect, represents the query <tt>Shirt.where(:color => 'red')</tt>.
72
- #
73
- # Note that this is simply 'syntactic sugar' for defining an actual class method:
74
- #
75
- # class Shirt < ActiveRecord::Base
76
- # def self.red
77
- # where(:color => 'red')
78
- # end
79
- # end
80
- #
81
- # Unlike <tt>Shirt.find(...)</tt>, however, the object returned by Shirt.red is not an Array; it
82
- # resembles the association object constructed by a <tt>has_many</tt> declaration. For instance,
83
- # you can invoke <tt>Shirt.red.first</tt>, <tt>Shirt.red.count</tt>, <tt>Shirt.red.where(:size => 'small')</tt>.
84
- # Also, just as with the association objects, named \scopes act like an Array, implementing Enumerable;
85
- # <tt>Shirt.red.each(&block)</tt>, <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt>
86
- # all behave as if Shirt.red really was an Array.
87
- #
88
- # These named \scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce
89
- # all shirts that are both red and dry clean only.
90
- # Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt>
91
- # returns the number of garments for which these criteria obtain. Similarly with
92
- # <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
93
- #
94
- # All \scopes are available as class methods on the ActiveRecord::Base descendant upon which
95
- # the \scopes were defined. But they are also available to <tt>has_many</tt> associations. If,
96
- #
97
- # class Person < ActiveRecord::Base
98
- # has_many :shirts
99
- # end
100
- #
101
- # then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
102
- # only shirts.
103
- #
104
- # Named \scopes can also be procedural:
105
- #
106
- # class Shirt < ActiveRecord::Base
107
- # scope :colored, lambda { |color| where(:color => color) }
108
- # end
109
- #
110
- # In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
111
- #
112
- # On Ruby 1.9 you can use the 'stabby lambda' syntax:
113
- #
114
- # scope :colored, ->(color) { where(:color => color) }
115
- #
116
- # Note that scopes defined with \scope will be evaluated when they are defined, rather than
117
- # when they are used. For example, the following would be incorrect:
118
- #
119
- # class Post < ActiveRecord::Base
120
- # scope :recent, where('published_at >= ?', Time.now - 1.week)
121
- # end
122
- #
123
- # The example above would be 'frozen' to the <tt>Time.now</tt> value when the <tt>Post</tt>
124
- # class was defined, and so the resultant SQL query would always be the same. The correct
125
- # way to do this would be via a lambda, which will re-evaluate the scope each time
126
- # it is called:
127
- #
128
- # class Post < ActiveRecord::Base
129
- # scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
130
- # end
131
- #
132
- # Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations:
133
- #
134
- # class Shirt < ActiveRecord::Base
135
- # scope :red, where(:color => 'red') do
136
- # def dom_id
137
- # 'red_shirts'
138
- # end
139
- # end
140
- # end
141
- #
142
- # Scopes can also be used while creating/building a record.
143
- #
144
- # class Article < ActiveRecord::Base
145
- # scope :published, where(:published => true)
146
- # end
147
- #
148
- # Article.published.new.published # => true
149
- # Article.published.create.published # => true
150
- #
151
- # Class methods on your model are automatically available
152
- # on scopes. Assuming the following setup:
153
- #
154
- # class Article < ActiveRecord::Base
155
- # scope :published, where(:published => true)
156
- # scope :featured, where(:featured => true)
157
- #
158
- # def self.latest_article
159
- # order('published_at desc').first
160
- # end
161
- #
162
- # def self.titles
163
- # map(&:title)
164
- # end
165
- #
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
-
173
- def scope(name, scope_options = {})
174
- name = name.to_sym
175
- valid_scope_name?(name)
176
- extension = Module.new(&Proc.new) if block_given?
177
-
178
- scope_proc = lambda do |*args|
179
- options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
180
- options = scoped.apply_finder_options(options) if options.is_a?(Hash)
181
-
182
- relation = scoped.merge(options)
183
-
184
- extension ? relation.extending(extension) : relation
185
- end
186
-
187
- singleton_class.send(:redefine_method, name, &scope_proc)
188
- end
189
-
190
- protected
191
-
192
- def valid_scope_name?(name)
193
- if respond_to?(name, true)
194
- logger.warn "Creating scope :#{name}. " \
195
- "Overwriting existing method #{self.name}.#{name}."
196
- end
197
- end
198
- end
199
- end
200
- end