diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-07-05 22:02:11 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-08-28 11:19:36 +0200 |
commit | 67f55e282236eef53adc6036e735190b1dda5a47 (patch) | |
tree | 3fb6712eaaa98871caace81f6b4d1ce6b7d1a965 | |
parent | dc663dd52c99ab6e6c633b54d1a0e836f379bf9f (diff) | |
download | rails-67f55e282236eef53adc6036e735190b1dda5a47.tar.gz rails-67f55e282236eef53adc6036e735190b1dda5a47.tar.bz2 rails-67f55e282236eef53adc6036e735190b1dda5a47.zip |
Implement ActionView::Template::Types
AV::Template::Types is a small abstraction to allow to specify template types
that can be used in ActionView. When Action Pack is loaded it's replaced with
Mime::Type.
-rw-r--r-- | actionpack/lib/action_dispatch.rb | 1 | ||||
-rw-r--r-- | actionpack/lib/action_view/template.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/resolver.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/text.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/types.rb | 58 |
5 files changed, 65 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index bef18a9e81..0ec355246e 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -104,4 +104,5 @@ autoload :Mime, 'action_dispatch/http/mime_type' ActiveSupport.on_load(:action_view) do ActionView::Base.default_formats ||= Mime::SET.symbols + ActionView::Template::Types.delegate_to Mime end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 8c9310533c..c36b06a83d 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -92,6 +92,7 @@ module ActionView autoload :Error autoload :Handlers autoload :Text + autoload :Types end extend Template::Handlers @@ -121,7 +122,7 @@ module ActionView @locals = details[:locals] || [] @virtual_path = details[:virtual_path] @updated_at = details[:updated_at] || Time.now - @formats = Array(format).map { |f| f.to_sym } + @formats = Array(format).map { |f| f.respond_to?(:ref) ? f.ref : f } @compile_mutex = Mutex.new end @@ -147,7 +148,7 @@ module ActionView end def type - @type ||= @formats.first.to_sym if @formats.first + @type ||= Types[@formats.first] if @formats.first end # Receives a view object and return a template similar to self by using @virtual_path. diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index a8c8e919e2..25c6fd4aa8 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -235,7 +235,7 @@ module ActionView extension = pieces.pop ActiveSupport::Deprecation.warn "The file #{path} did not specify a template handler. The default is currently ERB, but will change to RAW in the future." unless extension handler = Template.handler_for_extension(extension) - format = pieces.last && pieces.last.to_sym + format = pieces.last && Template::Types[pieces.last] [handler, format] end end diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb index b629faaa9a..859c7bc3ce 100644 --- a/actionpack/lib/action_view/template/text.rb +++ b/actionpack/lib/action_view/template/text.rb @@ -6,7 +6,8 @@ module ActionView #:nodoc: def initialize(string, type = nil) @string = string.to_s - @type = type || :text + @type = Types[type] || type if type + @type ||= Types[:text] end def identifier diff --git a/actionpack/lib/action_view/template/types.rb b/actionpack/lib/action_view/template/types.rb new file mode 100644 index 0000000000..7611c9e708 --- /dev/null +++ b/actionpack/lib/action_view/template/types.rb @@ -0,0 +1,58 @@ +require 'set' +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/object/blank' + +module ActionView + class Template + class Types + class Type + cattr_accessor :types + self.types = Set.new + + def self.register(*t) + types.merge(t.map { |type| type.to_s }) + end + + register :html, :text, :js, :css, :xml, :json + + def self.[](type) + return type if type.is_a?(self) + + if type.is_a?(Symbol) || types.member?(type.to_s) + new(type) + end + end + + attr_reader :symbol + + def initialize(symbol) + @symbol = symbol.to_sym + end + + delegate :to_s, :to_sym, :to => :symbol + alias to_str to_s + + def ref + to_sym || to_s + end + + def ==(type) + return false if type.blank? + symbol.to_sym == type.to_sym + end + end + + cattr_accessor :type_klass + + def self.delegate_to(klass) + self.type_klass = klass + end + + delegate_to Type + + def self.[](type) + type_klass[type] + end + end + end +end |