mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
dc663dd52c
commit
67f55e2822
5 changed files with 65 additions and 4 deletions
|
@ -104,4 +104,5 @@ autoload :Mime, 'action_dispatch/http/mime_type'
|
||||||
|
|
||||||
ActiveSupport.on_load(:action_view) do
|
ActiveSupport.on_load(:action_view) do
|
||||||
ActionView::Base.default_formats ||= Mime::SET.symbols
|
ActionView::Base.default_formats ||= Mime::SET.symbols
|
||||||
|
ActionView::Template::Types.delegate_to Mime
|
||||||
end
|
end
|
||||||
|
|
|
@ -92,6 +92,7 @@ module ActionView
|
||||||
autoload :Error
|
autoload :Error
|
||||||
autoload :Handlers
|
autoload :Handlers
|
||||||
autoload :Text
|
autoload :Text
|
||||||
|
autoload :Types
|
||||||
end
|
end
|
||||||
|
|
||||||
extend Template::Handlers
|
extend Template::Handlers
|
||||||
|
@ -121,7 +122,7 @@ module ActionView
|
||||||
@locals = details[:locals] || []
|
@locals = details[:locals] || []
|
||||||
@virtual_path = details[:virtual_path]
|
@virtual_path = details[:virtual_path]
|
||||||
@updated_at = details[:updated_at] || Time.now
|
@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
|
@compile_mutex = Mutex.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -147,7 +148,7 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
def type
|
def type
|
||||||
@type ||= @formats.first.to_sym if @formats.first
|
@type ||= Types[@formats.first] if @formats.first
|
||||||
end
|
end
|
||||||
|
|
||||||
# Receives a view object and return a template similar to self by using @virtual_path.
|
# Receives a view object and return a template similar to self by using @virtual_path.
|
||||||
|
|
|
@ -235,7 +235,7 @@ module ActionView
|
||||||
extension = pieces.pop
|
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
|
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)
|
handler = Template.handler_for_extension(extension)
|
||||||
format = pieces.last && pieces.last.to_sym
|
format = pieces.last && Template::Types[pieces.last]
|
||||||
[handler, format]
|
[handler, format]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,8 @@ module ActionView #:nodoc:
|
||||||
|
|
||||||
def initialize(string, type = nil)
|
def initialize(string, type = nil)
|
||||||
@string = string.to_s
|
@string = string.to_s
|
||||||
@type = type || :text
|
@type = Types[type] || type if type
|
||||||
|
@type ||= Types[:text]
|
||||||
end
|
end
|
||||||
|
|
||||||
def identifier
|
def identifier
|
||||||
|
|
58
actionpack/lib/action_view/template/types.rb
Normal file
58
actionpack/lib/action_view/template/types.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue