From 67f55e282236eef53adc6036e735190b1dda5a47 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 5 Jul 2012 22:02:11 +0200 Subject: 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. --- actionpack/lib/action_dispatch.rb | 1 + actionpack/lib/action_view/template.rb | 5 ++- actionpack/lib/action_view/template/resolver.rb | 2 +- actionpack/lib/action_view/template/text.rb | 3 +- actionpack/lib/action_view/template/types.rb | 58 +++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 actionpack/lib/action_view/template/types.rb 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 -- cgit v1.2.3