aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/abstract_controller/rendering.rb8
-rw-r--r--actionpack/lib/action_controller/metal/rendering.rb11
-rw-r--r--actionpack/lib/action_controller/metal/streaming.rb10
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb101
-rw-r--r--actionpack/lib/action_dispatch/middleware/flash.rb42
-rw-r--r--actionpack/lib/action_view/base.rb6
-rw-r--r--actionpack/lib/action_view/renderer/streaming_template_renderer.rb11
-rw-r--r--actionpack/test/controller/flash_test.rb9
-rw-r--r--actionpack/test/controller/new_base/render_streaming_test.rb32
-rw-r--r--actionpack/test/dispatch/response_test.rb16
-rw-r--r--activerecord/CHANGELOG38
-rw-r--r--activerecord/lib/active_record/base.rb57
-rw-r--r--activerecord/test/cases/relation_scoping_test.rb193
-rw-r--r--activerecord/test/models/bulb.rb5
-rw-r--r--activerecord/test/models/car.rb8
-rw-r--r--activerecord/test/models/categorization.rb5
-rw-r--r--activerecord/test/models/developer.rb81
-rw-r--r--activerecord/test/models/post.rb5
-rw-r--r--activerecord/test/models/reference.rb5
-rw-r--r--activerecord/test/models/without_table.rb4
20 files changed, 278 insertions, 369 deletions
diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb
index d0dd730b06..306bd41e2d 100644
--- a/actionpack/lib/abstract_controller/rendering.rb
+++ b/actionpack/lib/abstract_controller/rendering.rb
@@ -114,13 +114,7 @@ module AbstractController
# :api: plugin
def render_to_string(*args, &block)
options = _normalize_render(*args, &block)
- if self.response_body = render_to_body(options)
- string = ""
- response_body.each { |r| string << r }
- string
- end
- ensure
- self.response_body = nil
+ render_to_body(options)
end
# Raw rendering of a template to a Rack-compatible body.
diff --git a/actionpack/lib/action_controller/metal/rendering.rb b/actionpack/lib/action_controller/metal/rendering.rb
index 32d52c84c4..70fd79bb8b 100644
--- a/actionpack/lib/action_controller/metal/rendering.rb
+++ b/actionpack/lib/action_controller/metal/rendering.rb
@@ -18,6 +18,17 @@ module ActionController
response_body
end
+ # Overwrite render_to_string because body can now be set to a rack body.
+ def render_to_string(*)
+ if self.response_body = super
+ string = ""
+ response_body.each { |r| string << r }
+ string
+ end
+ ensure
+ self.response_body = nil
+ end
+
private
# Normalize arguments by catching blocks and setting them on :update.
diff --git a/actionpack/lib/action_controller/metal/streaming.rb b/actionpack/lib/action_controller/metal/streaming.rb
index adb3e94134..b9bd49f670 100644
--- a/actionpack/lib/action_controller/metal/streaming.rb
+++ b/actionpack/lib/action_controller/metal/streaming.rb
@@ -38,9 +38,13 @@ module ActionController #:nodoc:
def _process_options(options) #:nodoc:
super
if options[:stream]
- headers["Cache-Control"] ||= "no-cache"
- headers["Transfer-Encoding"] = "chunked"
- headers.delete("Content-Length")
+ if env["HTTP_VERSION"] == "HTTP/1.0"
+ options.delete(:stream)
+ else
+ headers["Cache-Control"] ||= "no-cache"
+ headers["Transfer-Encoding"] = "chunked"
+ headers.delete("Content-Length")
+ end
end
end
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index 8e03a7879f..78ecf177be 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -32,24 +32,35 @@ module ActionDispatch # :nodoc:
# puts @response.body
# end
# end
- class Response < Rack::Response
- attr_accessor :request, :blank
+ class Response
+ attr_accessor :request, :header, :status
+ attr_writer :sending_file
- attr_writer :header, :sending_file
alias_method :headers=, :header=
+ alias_method :headers, :header
+
+ delegate :[], :[]=, :to => :@header
+ delegate :each, :to => :@body
+
+ # Sets the HTTP response's content MIME type. For example, in the controller
+ # you could write this:
+ #
+ # response.content_type = "text/plain"
+ #
+ # If a character set has been defined for this response (see charset=) then
+ # the character set information will also be included in the content type
+ # information.
+ attr_accessor :charset, :content_type
+
+ CONTENT_TYPE = "Content-Type"
+
+ cattr_accessor(:default_charset) { "utf-8" }
module Setup
def initialize(status = 200, header = {}, body = [])
- @writer = lambda { |x| @body << x }
- @block = nil
- @length = 0
-
- @header = header
- self.body, self.status = body, status
+ self.body, self.header, self.status = body, header, status
- @cookie = []
@sending_file = false
-
@blank = false
if content_type = self["Content-Type"]
@@ -62,6 +73,7 @@ module ActionDispatch # :nodoc:
end
end
+ include Rack::Response::Helpers
include Setup
include ActionDispatch::Http::Cache::Response
@@ -106,13 +118,21 @@ module ActionDispatch # :nodoc:
def body=(body)
@blank = true if body == EMPTY
- @body = body.respond_to?(:to_str) ? [body] : body
+ @body = body.respond_to?(:each) ? body : [body]
end
def body_parts
@body
end
+ def set_cookie(key, value)
+ ::Rack::Utils.set_cookie_header!(header, key, value)
+ end
+
+ def delete_cookie(key, value={})
+ ::Rack::Utils.delete_cookie_header!(header, key, value)
+ end
+
def location
headers['Location']
end
@@ -122,46 +142,21 @@ module ActionDispatch # :nodoc:
headers['Location'] = url
end
- # Sets the HTTP response's content MIME type. For example, in the controller
- # you could write this:
- #
- # response.content_type = "text/plain"
- #
- # If a character set has been defined for this response (see charset=) then
- # the character set information will also be included in the content type
- # information.
- attr_accessor :charset, :content_type
-
- CONTENT_TYPE = "Content-Type"
-
- cattr_accessor(:default_charset) { "utf-8" }
-
def to_a
assign_default_content_type_and_charset!
handle_conditional_get!
- self["Set-Cookie"] = self["Set-Cookie"].join("\n") if self["Set-Cookie"].respond_to?(:join)
- super
- end
- alias prepare! to_a
+ @header["Set-Cookie"] = @header["Set-Cookie"].join("\n") if @header["Set-Cookie"].respond_to?(:join)
- def each(&callback)
- if @body.respond_to?(:call)
- @writer = lambda { |x| callback.call(x) }
- @body.call(self, self)
+ if [204, 304].include?(@status)
+ @header.delete "Content-Type"
+ [@status, @header, []]
else
- @body.each { |part| callback.call(part.to_s) }
+ [@status, @header, self]
end
-
- @writer = callback
- @block.call(self) if @block
- end
-
- def write(str)
- str = str.to_s
- @writer.call str
- str
end
+ alias prepare! to_a
+ alias to_ary to_a # For implicit splat on 1.9.2
# Returns the response cookies, converted to a Hash of (name => value) pairs
#
@@ -180,18 +175,18 @@ module ActionDispatch # :nodoc:
cookies
end
- private
- def assign_default_content_type_and_charset!
- return if headers[CONTENT_TYPE].present?
+ private
- @content_type ||= Mime::HTML
- @charset ||= self.class.default_charset
+ def assign_default_content_type_and_charset!
+ return if headers[CONTENT_TYPE].present?
- type = @content_type.to_s.dup
- type << "; charset=#{@charset}" unless @sending_file
+ @content_type ||= Mime::HTML
+ @charset ||= self.class.default_charset
- headers[CONTENT_TYPE] = type
- end
+ type = @content_type.to_s.dup
+ type << "; charset=#{@charset}" unless @sending_file
+ headers[CONTENT_TYPE] = type
+ end
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb
index 027ff7f8ac..735c72d34a 100644
--- a/actionpack/lib/action_dispatch/middleware/flash.rb
+++ b/actionpack/lib/action_dispatch/middleware/flash.rb
@@ -4,7 +4,7 @@ module ActionDispatch
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash
- @env['action_dispatch.request.flash_hash'] ||= (session["flash"] || Flash::FlashHash.new)
+ @env[Flash::KEY] ||= (session["flash"] || Flash::FlashHash.new)
end
end
@@ -40,18 +40,14 @@ module ActionDispatch
#
# See docs on the FlashHash class for more details about the flash.
class Flash
+ KEY = 'action_dispatch.request.flash_hash'.freeze
+
class FlashNow #:nodoc:
def initialize(flash)
@flash = flash
- @closed = false
end
- attr_reader :closed
- alias :closed? :closed
- def close!; @closed = true end
-
def []=(k, v)
- raise ClosedError, :flash if closed?
@flash[k] = v
@flash.discard(k)
v
@@ -70,6 +66,10 @@ module ActionDispatch
def notice=(message)
self[:notice] = message
end
+
+ def close!(new_flash)
+ @flash = new_flash
+ end
end
class FlashHash
@@ -79,12 +79,9 @@ module ActionDispatch
@used = Set.new
@closed = false
@flashes = {}
+ @now = nil
end
- attr_reader :closed
- alias :closed? :closed
- def close!; @closed = true end
-
def []=(k, v) #:nodoc:
raise ClosedError, :flash if closed?
keep(k)
@@ -152,6 +149,14 @@ module ActionDispatch
@now ||= FlashNow.new(self)
end
+ attr_reader :closed
+ alias :closed? :closed
+
+ def close!
+ @closed = true
+ @now.close!(self) if @now
+ end
+
# Keeps either the entire current flash or a specific flash entry available for the next action:
#
# flash.keep # keeps the entire flash
@@ -231,13 +236,18 @@ module ActionDispatch
@app.call(env)
ensure
session = env['rack.session'] || {}
- flash_hash = env['action_dispatch.request.flash_hash']
+ flash_hash = env[KEY]
if flash_hash
- if !flash_hash.empty? || session.key?('flash')
- session["flash"] = flash_hash
- end
- flash_hash.close!
+ if !flash_hash.empty? || session.key?('flash')
+ session["flash"] = flash_hash
+ new_hash = flash_hash.dup
+ else
+ new_hash = flash_hash
+ end
+
+ env[KEY] = new_hash
+ new_hash.close!
end
if session.key?('flash') && session['flash'].empty?
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 9e8a3c51a3..87501d5b88 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -137,6 +137,12 @@ module ActionView #:nodoc:
cattr_accessor :field_error_proc
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
+ # How to complete the streaming when an exception occurs.
+ # This is our best guess: first try to close the attribute, then the tag.
+ # Currently this is private API and may be changed at *any* time.
+ cattr_accessor :streaming_completion_on_exception
+ @@streaming_completion_on_exception = %("><script type="text/javascript">window.location = "/500.html"</script></html>)
+
class_attribute :helpers
class_attribute :_routes
diff --git a/actionpack/lib/action_view/renderer/streaming_template_renderer.rb b/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
index 52f0e9f5bd..03aab444f8 100644
--- a/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
+++ b/actionpack/lib/action_view/renderer/streaming_template_renderer.rb
@@ -46,11 +46,8 @@ module ActionView
#
# == TODO
#
- # * Add streaming support in the controllers with no-cache settings
- # * What should happen when an error happens?
# * Support streaming from child templates, partials and so on.
- # * Support on sprockets async JS load?
- #
+ # * Integrate exceptions with exceptron
class StreamingTemplateRenderer < TemplateRenderer #:nodoc:
# A valid Rack::Body (i.e. it responds to each).
# It is initialized with a block that, when called, starts
@@ -61,7 +58,11 @@ module ActionView
end
def each(&block)
- @start.call(block)
+ begin
+ @start.call(block)
+ rescue
+ block.call ActionView::Base.streaming_completion_on_exception
+ end
self
end
end
diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb
index 9c89f1334d..7b5bf8b21a 100644
--- a/actionpack/test/controller/flash_test.rb
+++ b/actionpack/test/controller/flash_test.rb
@@ -264,6 +264,14 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
end
end
+ def test_setting_flash_does_not_raise_in_following_requests
+ with_test_route_set do
+ env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new }
+ get '/set_flash', nil, env
+ get '/set_flash', nil, env
+ end
+ end
+
def test_setting_flash_raises_after_stream_back_to_client_even_with_an_empty_flash
with_test_route_set do
env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new }
@@ -294,7 +302,6 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
end
end
-
private
# Overwrite get to send SessionSecret in env hash
diff --git a/actionpack/test/controller/new_base/render_streaming_test.rb b/actionpack/test/controller/new_base/render_streaming_test.rb
index ffc4b331ec..fed8d40b47 100644
--- a/actionpack/test/controller/new_base/render_streaming_test.rb
+++ b/actionpack/test/controller/new_base/render_streaming_test.rb
@@ -4,7 +4,9 @@ module RenderStreaming
class BasicController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"render_streaming/basic/hello_world.html.erb" => "Hello world",
- "layouts/application.html.erb" => "<%= yield %>, I'm here!"
+ "render_streaming/basic/boom.html.erb" => "<%= nil.invalid! %>",
+ "layouts/application.html.erb" => "<%= yield %>, I'm here!",
+ "layouts/boom.html.erb" => "<body class=\"<%= nil.invalid! %>\"<%= yield %></body>"
)]
layout "application"
@@ -13,6 +15,14 @@ module RenderStreaming
def hello_world
end
+ def layout_exception
+ render :action => "hello_world", :stream => true, :layout => "boom"
+ end
+
+ def template_exception
+ render :action => "boom", :stream => true
+ end
+
def skip
render :action => "hello_world", :stream => false
end
@@ -61,6 +71,26 @@ module RenderStreaming
assert_body "Hello world, I'm here!"
end
+ test "rendering with layout exception" do
+ get "/render_streaming/basic/layout_exception"
+ assert_body "d\r\n<body class=\"\r\n4e\r\n\"><script type=\"text/javascript\">window.location = \"/500.html\"</script></html>\r\n0\r\n\r\n"
+ assert_streaming!
+ end
+
+ test "rendering with template exception" do
+ get "/render_streaming/basic/template_exception"
+ assert_body "4e\r\n\"><script type=\"text/javascript\">window.location = \"/500.html\"</script></html>\r\n0\r\n\r\n"
+ assert_streaming!
+ end
+
+ test "do not stream on HTTP/1.0" do
+ get "/render_streaming/basic/hello_world", nil, "HTTP_VERSION" => "HTTP/1.0"
+ assert_body "Hello world, I'm here!"
+ assert_status 200
+ assert_equal "22", headers["Content-Length"]
+ assert_equal nil, headers["Transfer-Encoding"]
+ end
+
def assert_streaming!(cache="no-cache")
assert_status 200
assert_equal nil, headers["Content-Length"]
diff --git a/actionpack/test/dispatch/response_test.rb b/actionpack/test/dispatch/response_test.rb
index 6f38714b2e..5abbaf74fe 100644
--- a/actionpack/test/dispatch/response_test.rb
+++ b/actionpack/test/dispatch/response_test.rb
@@ -33,22 +33,6 @@ class ResponseTest < ActiveSupport::TestCase
}, headers)
end
- test "streaming block" do
- @response.body = Proc.new do |response, output|
- 5.times { |n| output.write(n) }
- end
-
- status, headers, body = @response.to_a
- assert_equal 200, status
- assert_equal({
- "Content-Type" => "text/html; charset=utf-8"
- }, headers)
-
- parts = []
- body.each { |part| parts << part.to_s }
- assert_equal ["0", "1", "2", "3", "4"], parts
- end
-
test "content type" do
[204, 304].each do |c|
@response.status = c.to_s
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 6b3d408720..9ff29f1155 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,16 @@
*Rails 3.1.0 (unreleased)*
+* default_scope can take a block, lambda, or any other object which responds to `call` for lazy
+ evaluation:
+
+ default_scope { ... }
+ default_scope lambda { ... }
+ default_scope method(:foo)
+
+ This feature was originally implemented by Tim Morgan, but was then removed in favour of
+ defining a 'default_scope' class method, but has now been added back in by Jon Leighton.
+ The relevant lighthouse ticket is #1812.
+
* Default scopes are now evaluated at the latest possible moment, to avoid problems where
scopes would be created which would implicitly contain the default scope, which would then
be impossible to get rid of via Model.unscoped.
@@ -11,25 +22,34 @@
[Jon Leighton]
-* Deprecated support for passing hashes and relations to 'default_scope'. Please create a class
- method for your scope instead. For example, change this:
+* Calling 'default_scope' multiple times in a class (including when a superclass calls
+ 'default_scope') is deprecated. The current behavior is that this will merge the default
+ scopes together:
- class Post < ActiveRecord::Base
+ class Post < ActiveRecord::Base # Rails 3.1
default_scope where(:published => true)
+ default_scope where(:hidden => false)
+ # The default scope is now: where(:published => true, :hidden => false)
end
- To this:
+ In Rails 3.2, the behavior will be changed to overwrite previous scopes:
+
+ class Post < ActiveRecord::Base # Rails 3.2
+ default_scope where(:published => true)
+ default_scope where(:hidden => false)
+ # The default scope is now: where(:hidden => false)
+ end
+
+ If you wish to merge default scopes in special ways, it is recommended to define your default
+ scope as a class method and use the standard techniques for sharing code (inheritance, mixins,
+ etc.):
class Post < ActiveRecord::Base
def self.default_scope
- where(:published => true)
+ where(:published => true).where(:hidden => false)
end
end
- Rationale: It will make the implementation simpler because we can simply use inheritance to
- handle inheritance scenarios, rather than trying to make up our own rules about what should
- happen when you call default_scope multiple times or in subclasses.
-
[Jon Leighton]
* PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 08a41e2d8b..9a01d793f9 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1177,13 +1177,11 @@ MSG
Thread.current[:"#{self}_current_scope"] = scope
end
- # Implement this method in your model to set a default scope for all operations on
+ # Use this macro in your model to set a default scope for all operations on
# the model.
#
# class Person < ActiveRecord::Base
- # def self.default_scope
- # order('last_name, first_name')
- # end
+ # default_scope order('last_name, first_name')
# end
#
# Person.all # => SELECT * FROM people ORDER BY last_name, first_name
@@ -1192,40 +1190,59 @@ MSG
# applied while updating a record.
#
# class Article < ActiveRecord::Base
- # def self.default_scope
- # where(:published => true)
- # end
+ # default_scope where(:published => true)
# end
#
# Article.new.published # => true
# Article.create.published # => true
#
- # === Deprecation warning
- #
- # There is an alternative syntax as follows:
+ # You can also use <tt>default_scope</tt> with a block, in order to have it lazily evaluated:
#
- # class Person < ActiveRecord::Base
- # default_scope order('last_name, first_name')
+ # class Article < ActiveRecord::Base
+ # default_scope { where(:published_at => Time.now - 1.week) }
# end
#
- # This is now deprecated and will be removed in Rails 3.2.
+ # (You can also pass any object which responds to <tt>call</tt> to the <tt>default_scope</tt>
+ # macro, and it will be called when building the default scope.)
+ #
+ # If you need to do more complex things with a default scope, you can alternatively
+ # define it as a class method:
+ #
+ # class Article < ActiveRecord::Base
+ # def self.default_scope
+ # # Should return a scope, you can call 'super' here etc.
+ # end
+ # end
def default_scope(scope = {})
- ActiveSupport::Deprecation.warn <<-WARN
-Passing a hash or scope to default_scope is deprecated and will be removed in Rails 3.2. You should create a class method for your scope instead. For example, change this:
+ if default_scopes.length != 0
+ ActiveSupport::Deprecation.warn <<-WARN
+Calling 'default_scope' multiple times in a class (including when a superclass calls 'default_scope') is deprecated. The current behavior is that this will merge the default scopes together:
-class Post < ActiveRecord::Base
+class Post < ActiveRecord::Base # Rails 3.1
default_scope where(:published => true)
+ default_scope where(:hidden => false)
+ # The default scope is now: where(:published => true, :hidden => false)
end
-To this:
+In Rails 3.2, the behavior will be changed to overwrite previous scopes:
+
+class Post < ActiveRecord::Base # Rails 3.2
+ default_scope where(:published => true)
+ default_scope where(:hidden => false)
+ # The default scope is now: where(:hidden => false)
+end
+
+If you wish to merge default scopes in special ways, it is recommended to define your default scope as a class method and use the standard techniques for sharing code (inheritance, mixins, etc.):
class Post < ActiveRecord::Base
def self.default_scope
- where(:published => true)
+ where(:published => true).where(:hidden => false)
end
end
-WARN
+ WARN
+ end
+ scope = Proc.new if block_given?
self.default_scopes = default_scopes.dup << scope
end
@@ -1238,6 +1255,8 @@ WARN
default_scopes.inject(relation) do |default_scope, scope|
if scope.is_a?(Hash)
default_scope.apply_finder_options(scope)
+ elsif !scope.is_a?(Relation) && scope.respond_to?(:call)
+ default_scope.merge(scope.call)
else
default_scope.merge(scope)
end
diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb
index 5079aec9ba..2ed676fe69 100644
--- a/activerecord/test/cases/relation_scoping_test.rb
+++ b/activerecord/test/cases/relation_scoping_test.rb
@@ -308,6 +308,22 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal expected, received
end
+ def test_default_scope_as_class_method
+ assert_equal [developers(:david).becomes(ClassMethodDeveloperCalledDavid)], ClassMethodDeveloperCalledDavid.all
+ end
+
+ def test_default_scope_with_lambda
+ assert_equal [developers(:david).becomes(LazyLambdaDeveloperCalledDavid)], LazyLambdaDeveloperCalledDavid.all
+ end
+
+ def test_default_scope_with_block
+ assert_equal [developers(:david).becomes(LazyBlockDeveloperCalledDavid)], LazyBlockDeveloperCalledDavid.all
+ end
+
+ def test_default_scope_with_callable
+ assert_equal [developers(:david).becomes(CallableDeveloperCalledDavid)], CallableDeveloperCalledDavid.all
+ end
+
def test_default_scope_is_unscoped_on_find
assert_equal 1, DeveloperCalledDavid.count
assert_equal 11, DeveloperCalledDavid.unscoped.count
@@ -339,6 +355,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal 50000, wheres[:salary]
end
+ def test_default_scope_with_multiple_calls
+ wheres = MultiplePoorDeveloperCalledJamis.scoped.where_values_hash
+ assert_equal "Jamis", wheres[:name]
+ assert_equal 50000, wheres[:salary]
+ end
+
def test_method_scope
expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
@@ -434,175 +456,18 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert DeveloperCalledJamis.unscoped.poor.include?(developers(:david).becomes(DeveloperCalledJamis))
assert_equal 10, DeveloperCalledJamis.unscoped.poor.length
end
-end
-
-class DeprecatedDefaultScopingTest < ActiveRecord::TestCase
- fixtures :developers, :posts
-
- def test_default_scope
- expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary }
- received = DeprecatedDeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
- assert_equal expected, received
- end
-
- def test_default_scope_is_unscoped_on_find
- assert_equal 1, DeprecatedDeveloperCalledDavid.count
- assert_equal 11, DeprecatedDeveloperCalledDavid.unscoped.count
- end
-
- def test_default_scope_is_unscoped_on_create
- assert_nil DeprecatedDeveloperCalledJamis.unscoped.create!.name
- end
-
- def test_default_scope_with_conditions_string
- assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeprecatedDeveloperCalledDavid.find(:all).map(&:id).sort
- assert_equal nil, DeprecatedDeveloperCalledDavid.create!.name
- end
- def test_default_scope_with_conditions_hash
- assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeprecatedDeveloperCalledJamis.find(:all).map(&:id).sort
- assert_equal 'Jamis', DeprecatedDeveloperCalledJamis.create!.name
- end
+ def test_multiple_default_scope_calls_are_deprecated
+ klass = Class.new(ActiveRecord::Base)
- def test_default_scoping_with_threads
- 2.times do
- Thread.new { assert DeprecatedDeveloperOrderedBySalary.scoped.to_sql.include?('salary DESC') }.join
+ assert_not_deprecated do
+ klass.send(:default_scope, :foo => :bar)
end
- end
- def test_default_scoping_with_inheritance
- # Inherit a class having a default scope and define a new default scope
- klass = Class.new(DeprecatedDeveloperOrderedBySalary)
- ActiveSupport::Deprecation.silence { klass.send :default_scope, :limit => 1 }
-
- # Scopes added on children should append to parent scope
- assert_equal [developers(:jamis).id], klass.all.map(&:id)
-
- # Parent should still have the original scope
- assert_equal Developer.order('salary DESC').map(&:id), DeprecatedDeveloperOrderedBySalary.all.map(&:id)
- end
-
- def test_default_scope_called_twice_merges_conditions
- Developer.destroy_all
- Developer.create!(:name => "David", :salary => 80000)
- Developer.create!(:name => "David", :salary => 100000)
- Developer.create!(:name => "Brian", :salary => 100000)
-
- klass = Class.new(Developer)
- ActiveSupport::Deprecation.silence do
- klass.__send__ :default_scope, :conditions => { :name => "David" }
- klass.__send__ :default_scope, :conditions => { :salary => 100000 }
- end
- assert_equal 1, klass.count
- assert_equal "David", klass.first.name
- assert_equal 100000, klass.first.salary
- end
-
- def test_default_scope_called_twice_in_different_place_merges_where_clause
- Developer.destroy_all
- Developer.create!(:name => "David", :salary => 80000)
- Developer.create!(:name => "David", :salary => 100000)
- Developer.create!(:name => "Brian", :salary => 100000)
-
- klass = Class.new(Developer)
- ActiveSupport::Deprecation.silence do
- klass.class_eval do
- default_scope where("name = 'David'")
- default_scope where("salary = 100000")
- end
+ assert_deprecated do
+ klass.send(:default_scope, :foo => :bar)
end
- assert_equal 1, klass.count
- assert_equal "David", klass.first.name
- assert_equal 100000, klass.first.salary
- end
-
- def test_method_scope
- expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
- received = DeprecatedDeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary }
- assert_equal expected, received
- end
-
- def test_nested_scope
- expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary }
- received = DeprecatedDeveloperOrderedBySalary.send(:with_scope, :find => { :order => 'name DESC'}) do
- DeprecatedDeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
- end
- assert_equal expected, received
- end
-
- def test_scope_overwrites_default
- expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.name }
- received = DeprecatedDeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.name }
- assert_equal expected, received
- end
-
- def test_reorder_overrides_default_scope_order
- expected = Developer.order('name DESC').collect { |dev| dev.name }
- received = DeprecatedDeveloperOrderedBySalary.reorder('name DESC').collect { |dev| dev.name }
- assert_equal expected, received
- end
-
- def test_nested_exclusive_scope
- expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary }
- received = DeprecatedDeveloperOrderedBySalary.send(:with_exclusive_scope, :find => { :limit => 100 }) do
- DeprecatedDeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary }
- end
- assert_equal expected, received
- end
-
- def test_order_in_default_scope_should_prevail
- expected = Developer.find(:all, :order => 'salary desc').collect { |dev| dev.salary }
- received = DeprecatedDeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary }
- assert_equal expected, received
- end
-
- def test_default_scope_using_relation
- posts = DeprecatedPostWithComment.scoped
- assert_equal 2, posts.to_a.length
- assert_equal posts(:thinking), posts.first
- end
-
- def test_create_attribute_overwrites_default_scoping
- assert_equal 'David', DeprecatedPoorDeveloperCalledJamis.create!(:name => 'David').name
- assert_equal 200000, DeprecatedPoorDeveloperCalledJamis.create!(:name => 'David', :salary => 200000).salary
- end
-
- def test_create_attribute_overwrites_default_values
- assert_equal nil, DeprecatedPoorDeveloperCalledJamis.create!(:salary => nil).salary
- assert_equal 50000, DeprecatedPoorDeveloperCalledJamis.create!(:name => 'David').salary
- end
-
- def test_default_scope_attribute
- jamis = DeprecatedPoorDeveloperCalledJamis.new(:name => 'David')
- assert_equal 50000, jamis.salary
- end
-
- def test_where_attribute
- aaron = DeprecatedPoorDeveloperCalledJamis.where(:salary => 20).new(:name => 'Aaron')
- assert_equal 20, aaron.salary
- assert_equal 'Aaron', aaron.name
- end
-
- def test_where_attribute_merge
- aaron = DeprecatedPoorDeveloperCalledJamis.where(:name => 'foo').new(:name => 'Aaron')
- assert_equal 'Aaron', aaron.name
- end
-
- def test_create_with_merge
- aaron = DeprecatedPoorDeveloperCalledJamis.create_with(:name => 'foo', :salary => 20).merge(
- DeprecatedPoorDeveloperCalledJamis.create_with(:name => 'Aaron')).new
- assert_equal 20, aaron.salary
- assert_equal 'Aaron', aaron.name
-
- aaron = DeprecatedPoorDeveloperCalledJamis.create_with(:name => 'foo', :salary => 20).
- create_with(:name => 'Aaron').new
- assert_equal 20, aaron.salary
- assert_equal 'Aaron', aaron.name
- end
-
- def test_create_with_reset
- jamis = DeprecatedPoorDeveloperCalledJamis.create_with(:name => 'Aaron').create_with(nil).new
- assert_equal 'Jamis', jamis.name
+ assert_equal 2, klass.default_scopes.length
end
end
diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb
index 89ee5416bf..c68d008c26 100644
--- a/activerecord/test/models/bulb.rb
+++ b/activerecord/test/models/bulb.rb
@@ -1,8 +1,5 @@
class Bulb < ActiveRecord::Base
- def self.default_scope
- where :name => 'defaulty'
- end
-
+ default_scope where(:name => 'defaulty')
belongs_to :car
attr_reader :scope_after_initialize
diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb
index a978debb58..b036f0f5c9 100644
--- a/activerecord/test/models/car.rb
+++ b/activerecord/test/models/car.rb
@@ -15,13 +15,9 @@ class Car < ActiveRecord::Base
end
class CoolCar < Car
- def self.default_scope
- order 'name desc'
- end
+ default_scope :order => 'name desc'
end
class FastCar < Car
- def self.default_scope
- order 'name desc'
- end
+ default_scope :order => 'name desc'
end
diff --git a/activerecord/test/models/categorization.rb b/activerecord/test/models/categorization.rb
index 39441e8610..4bd980e606 100644
--- a/activerecord/test/models/categorization.rb
+++ b/activerecord/test/models/categorization.rb
@@ -12,10 +12,7 @@ end
class SpecialCategorization < ActiveRecord::Base
self.table_name = 'categorizations'
-
- def self.default_scope
- where(:special => true)
- end
+ default_scope where(:special => true)
belongs_to :author
belongs_to :category
diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb
index 10385ba899..10701dd6fd 100644
--- a/activerecord/test/models/developer.rb
+++ b/activerecord/test/models/developer.rb
@@ -1,3 +1,5 @@
+require 'ostruct'
+
module DeveloperProjectsAssociationExtension
def find_most_recent
find(:first, :order => "id DESC")
@@ -86,10 +88,7 @@ end
class DeveloperOrderedBySalary < ActiveRecord::Base
self.table_name = 'developers'
-
- def self.default_scope
- order('salary DESC')
- end
+ default_scope :order => 'salary DESC'
scope :by_name, order('name DESC')
@@ -102,74 +101,56 @@ end
class DeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
-
- def self.default_scope
- where "name = 'David'"
- end
+ default_scope where("name = 'David'")
end
-class DeveloperCalledJamis < ActiveRecord::Base
+class LazyLambdaDeveloperCalledDavid < ActiveRecord::Base
self.table_name = 'developers'
+ default_scope lambda { where(:name => 'David') }
+end
- def self.default_scope
- where :name => 'Jamis'
- end
+class LazyBlockDeveloperCalledDavid < ActiveRecord::Base
+ self.table_name = 'developers'
+ default_scope { where(:name => 'David') }
+end
- scope :poor, where('salary < 150000')
+class CallableDeveloperCalledDavid < ActiveRecord::Base
+ self.table_name = 'developers'
+ default_scope OpenStruct.new(:call => where(:name => 'David'))
end
-class AbstractDeveloperCalledJamis < ActiveRecord::Base
- self.abstract_class = true
+class ClassMethodDeveloperCalledDavid < ActiveRecord::Base
+ self.table_name = 'developers'
def self.default_scope
- where :name => 'Jamis'
+ where(:name => 'David')
end
end
-class PoorDeveloperCalledJamis < ActiveRecord::Base
+class DeveloperCalledJamis < ActiveRecord::Base
self.table_name = 'developers'
+ default_scope where(:name => 'Jamis')
+ scope :poor, where('salary < 150000')
+end
- def self.default_scope
- where :name => 'Jamis', :salary => 50000
- end
+class PoorDeveloperCalledJamis < ActiveRecord::Base
+ self.table_name = 'developers'
+ default_scope where(:name => 'Jamis', :salary => 50000)
end
class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis
self.table_name = 'developers'
- def self.default_scope
- super.where :salary => 50000
+ ActiveSupport::Deprecation.silence do
+ default_scope where(:salary => 50000)
end
end
-ActiveSupport::Deprecation.silence do
- class DeprecatedDeveloperOrderedBySalary < ActiveRecord::Base
- self.table_name = 'developers'
- default_scope :order => 'salary DESC'
-
- def self.by_name
- order('name DESC')
- end
-
- def self.all_ordered_by_name
- with_scope(:find => { :order => 'name DESC' }) do
- find(:all)
- end
- end
- end
-
- class DeprecatedDeveloperCalledDavid < ActiveRecord::Base
- self.table_name = 'developers'
- default_scope :conditions => "name = 'David'"
- end
-
- class DeprecatedDeveloperCalledJamis < ActiveRecord::Base
- self.table_name = 'developers'
- default_scope :conditions => { :name => 'Jamis' }
- end
+class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base
+ self.table_name = 'developers'
+ default_scope where(:name => 'Jamis')
- class DeprecatedPoorDeveloperCalledJamis < ActiveRecord::Base
- self.table_name = 'developers'
- default_scope :conditions => { :name => 'Jamis', :salary => 50000 }
+ ActiveSupport::Deprecation.silence do
+ default_scope where(:salary => 50000)
end
end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 34cea60053..80296032bb 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -157,10 +157,7 @@ end
class FirstPost < ActiveRecord::Base
self.table_name = 'posts'
-
- def self.default_scope
- where(:id => 1)
- end
+ default_scope where(:id => 1)
has_many :comments, :foreign_key => :post_id
has_one :comment, :foreign_key => :post_id
diff --git a/activerecord/test/models/reference.rb b/activerecord/test/models/reference.rb
index 76c0a1a32e..c5af0b5d5f 100644
--- a/activerecord/test/models/reference.rb
+++ b/activerecord/test/models/reference.rb
@@ -19,8 +19,5 @@ end
class BadReference < ActiveRecord::Base
self.table_name = 'references'
-
- def self.default_scope
- where :favourite => false
- end
+ default_scope where(:favourite => false)
end
diff --git a/activerecord/test/models/without_table.rb b/activerecord/test/models/without_table.rb
index 1a63d6ceb6..184ab1649e 100644
--- a/activerecord/test/models/without_table.rb
+++ b/activerecord/test/models/without_table.rb
@@ -1,5 +1,3 @@
class WithoutTable < ActiveRecord::Base
- def self.default_scope
- where(:published => true)
- end
+ default_scope where(:published => true)
end