Permalink
Browse files

Deprecate template, session, assigns, and layout accessors on respons…

…e object. Instead access them through the controller instance. This mainly affects functional test assertions.
  • Loading branch information...
1 parent 8925e89 commit c0a372ba87f556769b98a6d06e8c684c3c3156df @josh josh committed Apr 29, 2009
@@ -367,6 +367,8 @@ def session
# Returns the name of the action this controller is processing.
attr_accessor :action_name
+ attr_reader :template
+
class << self
def call(env)
# HACK: For global rescue to have access to the original request and response
@@ -816,7 +818,7 @@ def _process_options(options)
def initialize_template_class(response)
@template = response.template = ActionView::Base.new(self.class.view_paths, {}, self, formats)
- response.template.helpers.send :include, self.class.master_helper_module
+ @template.helpers.send :include, self.class.master_helper_module
response.redirected_to = nil
@performed_render = @performed_redirect = false
end
@@ -127,7 +127,7 @@ def custom(mime_type, &block)
@order << mime_type
@responses[mime_type] ||= Proc.new do
- @response.template.formats = [mime_type.to_sym]
+ @controller.template.formats = [mime_type.to_sym]
@response.content_type = mime_type.to_s
block_given? ? block.call : @controller.send(:render, :action => @controller.action_name)
end
@@ -121,7 +121,7 @@ def cache_layout?
end
def content_for_layout(controller)
- controller.response.layout && controller.response.template.instance_variable_get('@cached_content_for_layout')
+ controller.template.layout && controller.template.instance_variable_get('@cached_content_for_layout')
end
end
@@ -33,7 +33,7 @@ def assert_response(type, message = nil)
assert_block("") { true } # to count the assertion
else
if @controller && @response.error?
- exception = @controller.response.template.instance_variable_get(:@exception)
+ exception = @controller.template.instance_variable_get(:@exception)
exception_message = exception && exception.message
assert_block(build_message(message, "Expected response to be a <?>, but was <?>\n<?>", type, @response.response_code, exception_message.to_s)) { false }
else
@@ -98,20 +98,20 @@ def assert_template(options = {}, message = nil)
clean_backtrace do
case options
when NilClass, String
- rendered = (@controller.response.rendered[:template] || []).map { |t| t.identifier }
+ rendered = (@controller.template.rendered[:template] || []).map { |t| t.identifier }
msg = build_message(message,
"expecting <?> but rendering with <?>",
options, rendered.join(', '))
assert_block(msg) do
if options.nil?
- @controller.response.rendered[:template].blank?
+ @controller.template.rendered[:template].blank?
else
rendered.any? { |t| t.match(options) }
end
end
when Hash
if expected_partial = options[:partial]
- partials = @controller.response.rendered[:partials]
+ partials = @controller.template.rendered[:partials]
if expected_count = options[:count]
found = partials.detect { |p, _| p.identifier.match(expected_partial) }
actual_count = found.nil? ? 0 : found.second
@@ -126,7 +126,7 @@ def assert_template(options = {}, message = nil)
assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg)
end
else
- assert @controller.response.rendered[:partials].empty?,
+ assert @controller.template.rendered[:partials].empty?,
"Expected no partials to be rendered"
end
end
@@ -160,7 +160,8 @@ def redirect_url_match?(pattern)
# Returns the template of the file which was used to
# render this response (or nil)
def rendered
- template.instance_variable_get(:@_rendered)
+ ActiveSupport::Deprecation.warn("response.rendered has been deprecated. Use tempate.rendered instead", caller)
+ @template.instance_variable_get(:@_rendered)
end
# A shortcut to the flash. Returns an empty hash if no session flash exists.
@@ -190,11 +191,13 @@ def has_session_object?(name=nil)
# A shortcut to the template.assigns
def template_objects
- template.assigns || {}
+ ActiveSupport::Deprecation.warn("response.template_objects has been deprecated. Use tempate.assigns instead", caller)
+ @template.assigns || {}
end
# Does the specified template object exist?
def has_template_object?(name=nil)
+ ActiveSupport::Deprecation.warn("response.has_template_object? has been deprecated. Use tempate.assigns[name].nil? instead", caller)
!template_objects[name].nil?
end
@@ -317,9 +320,9 @@ def xml_http_request(request_method, action, parameters = nil, session = nil, fl
def assigns(key = nil)
if key.nil?
- @response.template.assigns
+ @controller.template.assigns
else
- @response.template.assigns[key.to_s]
+ @controller.template.assigns[key.to_s]
end
end
@@ -431,7 +434,7 @@ def set_test_assigns
(instance_variable_names - self.class.protected_instance_variables).each do |var|
name, value = var[1..-1], instance_variable_get(var)
@assigns[name] = value
- response.template.assigns[name] = value if response
+ @template.assigns[name] = value if response
end
end
end
@@ -34,20 +34,30 @@ class Response < Rack::Response
DEFAULT_HEADERS = { "Cache-Control" => "no-cache" }
attr_accessor :request
- attr_accessor :template, :layout
attr_accessor :redirected_to, :redirected_to_method_params
attr_writer :header
alias_method :headers=, :header=
+ def template
+ ActiveSupport::Deprecation.warn("response.template has been deprecated. Use controller.template instead", caller)
+ @template
+ end
+ attr_writer :template
+
def session
ActiveSupport::Deprecation.warn("response.session has been deprecated. Use request.session instead", caller)
- request.session
+ @request.session
end
def assigns
ActiveSupport::Deprecation.warn("response.assigns has been deprecated. Use controller.assigns instead", caller)
- template.assigns
+ @template.controller.assigns
+ end
+
+ def layout
+ ActiveSupport::Deprecation.warn("response.layout has been deprecated. Use template.layout instead", caller)
+ @template.layout
end
delegate :default_charset, :to => 'ActionController::Base'
@@ -191,7 +191,7 @@ def self.cache_template_loading?
ActionController::Base.allow_concurrency || (cache_template_loading.nil? ? !ActiveSupport::Dependencies.load? : cache_template_loading)
end
- attr_internal :request
+ attr_internal :request, :layout
delegate :controller_path, :to => :controller, :allow_nil => true
@@ -46,7 +46,7 @@ def _render_content_with_layout(content, layout, locals)
locals ||= {}
if controller && layout
- response.layout = layout.identifier if controller.respond_to?(:response)
+ @_layout = layout.identifier
logger.info("Rendering template within #{layout.identifier}") if logger
end
@@ -7,7 +7,8 @@ def initialize(*args)
@_rendered = { :template => nil, :partials => Hash.new(0) }
initialize_without_template_tracking(*args)
end
-
+
+ attr_internal :rendered
alias_method :_render_template_without_template_tracking, :_render_template
def _render_template(template, local_assigns = {})
if template.respond_to?(:identifier)
@@ -16,7 +17,7 @@ def _render_template(template, local_assigns = {})
@_rendered[:template] << template
end
_render_template_without_template_tracking(template, local_assigns)
- end
+ end
end
class TestCase < ActiveSupport::TestCase
@@ -292,14 +292,14 @@ def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in
# make sure that the template objects exist
def test_template_objects_alive
process :assign_this
- assert !@response.has_template_object?('hi')
- assert @response.has_template_object?('howdy')
+ assert !@controller.template.assigns['hi']
+ assert @controller.template.assigns['howdy']
end
# make sure we don't have template objects when we shouldn't
def test_template_object_missing
process :nothing
- assert_nil @response.template_objects['howdy']
+ assert_nil @controller.template.assigns['howdy']
end
# check the empty flashing
@@ -328,11 +328,11 @@ def test_flash_have_nots
# check if we were rendered by a file-based template?
def test_rendered_action
process :nothing
- assert_nil @response.rendered[:template]
+ assert_nil @controller.template.rendered[:template]
process :hello_world
- assert @response.rendered[:template]
- assert 'hello_world', @response.rendered[:template].to_s
+ assert @controller.template.rendered[:template]
+ assert 'hello_world', @controller.template.rendered[:template].to_s
end
# check the redirection location
Oops, something went wrong.

1 comment on commit c0a372b

Contributor

smtlaissezfaire commented on c0a372b Apr 29, 2009

+1

Please sign in to comment.