diff options
author | Łukasz Strzałkowski <lukasz.strzalkowski@gmail.com> | 2013-12-03 11:17:01 +0100 |
---|---|---|
committer | Łukasz Strzałkowski <lukasz.strzalkowski@gmail.com> | 2013-12-04 00:13:16 +0100 |
commit | 2d3a6a0cb8df0360dd588a4d2fb260dd07cc9bcf (patch) | |
tree | 7f0c914f8af3585b4df30bd8f19784aeefae1e99 /actionpack/lib/abstract_controller | |
parent | 4d648819c5662f375b8ca431a14511ae6a97a29c (diff) | |
download | rails-2d3a6a0cb8df0360dd588a4d2fb260dd07cc9bcf.tar.gz rails-2d3a6a0cb8df0360dd588a4d2fb260dd07cc9bcf.tar.bz2 rails-2d3a6a0cb8df0360dd588a4d2fb260dd07cc9bcf.zip |
Action Pack Variants
By default, variants in the templates will be picked up if a variant is set
and there's a match. The format will be:
app/views/projects/show.html.erb
app/views/projects/show.html+tablet.erb
app/views/projects/show.html+phone.erb
If request.variant = :tablet is set, we'll automatically be rendering the
html+tablet template.
In the controller, we can also tailer to the variants with this syntax:
class ProjectsController < ActionController::Base
def show
respond_to do |format|
format.html do |html|
@stars = @project.stars
html.tablet { @notifications = @project.notifications }
html.phone { @chat_heads = @project.chat_heads }
end
format.js
format.atom
end
end
end
The variant itself is nil by default, but can be set in before filters, like
so:
class ApplicationController < ActionController::Base
before_action do
if request.user_agent =~ /iPad/
request.variant = :tablet
end
end
end
This is modeled loosely on custom mime types, but it's specifically not
intended to be used together. If you're going to make a custom mime type,
you don't need a variant. Variants are for variations on a single mime
types.
Diffstat (limited to 'actionpack/lib/abstract_controller')
-rw-r--r-- | actionpack/lib/abstract_controller/collector.rb | 10 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/rendering.rb | 2 |
2 files changed, 11 insertions, 1 deletions
diff --git a/actionpack/lib/abstract_controller/collector.rb b/actionpack/lib/abstract_controller/collector.rb index 09b9e7ddf0..f7a309c26c 100644 --- a/actionpack/lib/abstract_controller/collector.rb +++ b/actionpack/lib/abstract_controller/collector.rb @@ -23,7 +23,15 @@ module AbstractController protected def method_missing(symbol, &block) - mime_constant = Mime.const_get(symbol.upcase) + mime_const = symbol.upcase + + raise NoMethodError, "To respond to a custom format, register it as a MIME type first:" + + "http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads." + + "If you meant to respond to a variant like :tablet or :phone, not a custom format," + + "be sure to nest your variant response within a format response: format.html" + + "{ |html| html.tablet { ..." unless Mime.const_defined?(mime_const) + + mime_constant = Mime.const_get(mime_const) if Mime::SET.include?(mime_constant) AbstractController::Collector.generate_method_for_mime(mime_constant) diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index fb8f40cb9b..ce3a0316c4 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -102,6 +102,8 @@ module AbstractController # :api: private def _normalize_render(*args, &block) options = _normalize_args(*args, &block) + #TODO: remove defined? when we restore AP <=> AV dependency + options[:variant] = request.variant if defined?(request) && request.variant.present? _normalize_options(options) options end |