Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 14671087f8f0a52bef2bf67cbf91359a64250ab3 1 parent 1d180f1
@drogus drogus authored
View
1  actionpack/lib/action_dispatch.rb
@@ -104,4 +104,5 @@ module Session
ActiveSupport.on_load(:action_view) do
ActionView::Base.default_formats ||= Mime::SET.symbols
+ ActionView::Template::Types.delegate_to Mime
end
View
5 actionpack/lib/action_view/template.rb
@@ -92,6 +92,7 @@ class Template
autoload :Error
autoload :Handlers
autoload :Text
+ autoload :Types
end
extend Template::Handlers
@@ -121,7 +122,7 @@ def initialize(source, identifier, handler, details)
@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 @@ def render(view, locals, buffer=nil, &block)
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.
View
2  actionpack/lib/action_view/template/resolver.rb
@@ -235,7 +235,7 @@ def extract_handler_and_format(path, default_formats)
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
View
3  actionpack/lib/action_view/template/text.rb
@@ -6,7 +6,8 @@ class Text #: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
View
58 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
Please sign in to comment.
Something went wrong with that request. Please try again.