Permalink
Browse files

Sync with wycats/rails/master

  • Loading branch information...
michaelklishin committed Dec 28, 2008
2 parents 89e9895 + d77deb8 commit 12bff2af4081c25682d0466b1d7380b702c27417
@@ -109,16 +109,13 @@ def respond_to(*types, &block)
end
class Responder #:nodoc:
+
def initialize(controller)
@controller = controller
@request = controller.request
@response = controller.response
- if ActionController::Base.use_accept_header
- @mime_type_priority = Array(Mime::Type.lookup_by_extension(@request.parameters[:format]) || @request.accepts)
- else
- @mime_type_priority = [@request.format]
- end
+ @mime_type_priority = @request.formats
@order = []
@responses = {}
@@ -5,6 +5,10 @@ module Mime
EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
+ def self.[](type)
+ Type.lookup_by_extension(type)
+ end
+
# Encapsulates the notion of a mime type. Can be used at render time, for example, with:
#
# class PostsController < ActionController::Base
@@ -187,17 +191,13 @@ def =~(mime_type)
# Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See
# ActionController::RequestForgeryProtection.
def verify_request?
- browser_generated?
+ @@browser_generated_types.include?(to_sym)
end
def html?
@@html_types.include?(to_sym) || @string =~ /html/
end
- def browser_generated?
- @@browser_generated_types.include?(to_sym)
- end
-
private
def method_missing(method, *args)
if method.to_s =~ /(\w+)\?$/
@@ -152,24 +152,33 @@ def fresh?(response)
end
end
+ ONLY_ALL = [Mime::ALL].freeze
+
# Returns the Mime type for the \format used in the request.
#
# GET /posts/5.xml | request.format => Mime::XML
# GET /posts/5.xhtml | request.format => Mime::HTML
# GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt>
+
def format
@format ||=
if parameters[:format]
- Mime::Type.lookup_by_extension(parameters[:format])
- elsif ActionController::Base.use_accept_header
- accepts.first
- elsif xhr?
- Mime::Type.lookup_by_extension("js")
- else
- Mime::Type.lookup_by_extension("html")
+ Mime[parameters[:format]]
+ elsif Base.use_accept_header && !(accepts == ONLY_ALL)
+ accepts.first
+ elsif xhr? then Mime::JS
+ else Mime::HTML
end
end
+ def formats
+ @formats =
+ if Base.use_accept_header
+ Array(Mime[parameters[:format]] || accepts)
+ else
+ [format]
+ end
+ end
# Sets the \format by string extension, which can be used to force custom formats
# that are not controlled by the extension.
@@ -266,7 +266,7 @@ def template_format
if defined? @template_format
@template_format
elsif controller && controller.respond_to?(:request)
- @template_format = controller.request.template_format.to_sym
+ @template_format = controller.request.format.to_sym
else
@template_format = :html
end
@@ -67,14 +67,14 @@ def add(severity, message = nil, progname = nil, &block)
end
for severity in Severity.constants
- class_eval <<-EOT, __FILE__, __LINE__
- def #{severity.downcase}(message = nil, progname = nil, &block)
- add(#{severity}, message, progname, &block)
- end
-
- def #{severity.downcase}?
- #{severity} >= @level
- end
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
+ end # end
+
+ def #{severity.downcase}? # def debug?
+ #{severity} >= @level # DEBUG >= @level
+ end # end
EOT
end
@@ -209,21 +209,21 @@ def self.included(base)
module ClassMethods
def define_callbacks(*callbacks)
callbacks.each do |callback|
- class_eval <<-"end_eval"
- def self.#{callback}(*methods, &block)
- callbacks = CallbackChain.build(:#{callback}, *methods, &block)
- (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks
- end
+ class_eval <<-"end_eval", __FILE__, __LINE__ + 1
+ def self.#{callback}(*methods, &block) # def self.validate_on_create(*methods, &block)
+ callbacks = CallbackChain.build(:#{callback}, *methods, &block) # callbacks = CallbackChain.build(:validate_on_create, *methods, &block)
+ (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks # (@validate_on_create_callbacks ||= CallbackChain.new).concat callbacks
+ end # end
- def self.#{callback}_callback_chain
- @#{callback}_callbacks ||= CallbackChain.new
-
- if superclass.respond_to?(:#{callback}_callback_chain)
- CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks)
- else
- @#{callback}_callbacks
- end
- end
+ def self.#{callback}_callback_chain # def self.validate_on_create_callback_chain
+ @#{callback}_callbacks ||= CallbackChain.new # @validate_on_create_callbacks ||= CallbackChain.new
+ #
+ if superclass.respond_to?(:#{callback}_callback_chain) # if superclass.respond_to?(:validate_on_create_callback_chain)
+ CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks) # CallbackChain.new(superclass.validate_on_create_callback_chain + @validate_on_create_callbacks)
+ else # else
+ @#{callback}_callbacks # @validate_on_create_callbacks
+ end # end
+ end # end
end_eval
end
end
@@ -10,38 +10,38 @@ class Class
def cattr_reader(*syms)
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym} # unless defined @@property
+ @@#{sym} = nil # @@property = nil
+ end # end
- def self.#{sym}
- @@#{sym}
- end
+ def self.#{sym} # def self.property
+ @@#{sym} # @@property
+ end # end
- def #{sym}
- @@#{sym}
- end
+ def #{sym} # def property
+ @@#{sym} # @@property
+ end # end
EOS
end
end
def cattr_writer(*syms)
options = syms.extract_options!
syms.flatten.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
-
- def self.#{sym}=(obj)
- @@#{sym} = obj
- end
-
- #{"
- def #{sym}=(obj)
- @@#{sym} = obj
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym} # unless defined? @@property
+ @@#{sym} = nil # @@property = nil
+ end # end
+
+ def self.#{sym}=(obj) # def self.property=(obj)
+ @@#{sym} = obj # @@property
+ end # end
+
+ #{"
+ def #{sym}=(obj) # def property=(obj)
+ @@#{sym} = obj # @@property = obj
+ end # end
" unless options[:instance_writer] == false }
EOS
end
@@ -8,33 +8,33 @@ class Class
def superclass_delegating_reader(*names)
class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name
names.each do |name|
- class_eval <<-EOS
- def self.#{name}
- if defined?(@#{name})
- @#{name}
- elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name})
- superclass.#{name}
- end
- end
- def #{name}
- self.class.#{name}
- end
- def self.#{name}?
- !!#{name}
- end
- def #{name}?
- !!#{name}
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{name} # def self.property
+ if defined?(@#{name}) # if defined?(@property)
+ @#{name} # @property
+ elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name}) # elseif superclass < Object && superclass.respond_to?(:property)
+ superclass.#{name} # superclass.property
+ end # end
+ end # end
+ def #{name} # def property
+ self.class.#{name} # self.class.property
+ end # end
+ def self.#{name}? # def self.property?
+ !!#{name} # !!property
+ end # end
+ def #{name}? # def property?
+ !!#{name} # !!property
+ end # end
EOS
end
end
def superclass_delegating_writer(*names)
names.each do |name|
- class_eval <<-EOS
- def self.#{name}=(value)
- @#{name} = value
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{name}=(value) # def self.property=(value)
+ @#{name} = value # @property = value
+ end # end
EOS
end
end
@@ -10,30 +10,30 @@ class Class # :nodoc:
def class_inheritable_reader(*syms)
syms.each do |sym|
next if sym.is_a?(Hash)
- class_eval <<-EOS
- def self.#{sym}
- read_inheritable_attribute(:#{sym})
- end
-
- def #{sym}
- self.class.#{sym}
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{sym} # def self.after_add
+ read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:after_add)
+ end # end
+
+ def #{sym} # def after_add
+ self.class.#{sym} # self.class.after_add
+ end # end
EOS
end
end
def class_inheritable_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_attribute(:#{sym}, obj)
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{sym}=(obj) # def self.property=(obj)
+ write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:property, obj)
+ end # end
#{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
+ def #{sym}=(obj) # def property=(obj)
+ self.class.#{sym} = obj # self.class.property = obj
+ end # end
" unless options[:instance_writer] == false }
EOS
end
@@ -42,15 +42,15 @@ def #{sym}=(obj)
def class_inheritable_array_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_array(:#{sym}, obj)
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{sym}=(obj) # def self.property=(obj)
+ write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:property, obj)
+ end # end
#{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
+ def #{sym}=(obj) # def property=(obj)
+ self.class.#{sym} = obj # self.class.property = obj
+ end # end
" unless options[:instance_writer] == false }
EOS
end
@@ -59,15 +59,15 @@ def #{sym}=(obj)
def class_inheritable_hash_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_hash(:#{sym}, obj)
- end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def self.#{sym}=(obj) # def self.property=(obj)
+ write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:property, obj)
+ end # end
#{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
+ def #{sym}=(obj) # def property=(obj)
+ self.class.#{sym} = obj # self.class.property = obj
+ end # end
" unless options[:instance_writer] == false }
EOS
end
Oops, something went wrong.

0 comments on commit 12bff2a

Please sign in to comment.