Skip to content

Commit

Permalink
Prefer MIME constants to strings. Closes #7707.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6350 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Mar 6, 2007
1 parent e0cded2 commit dbf42e3
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 37 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Prefer MIME constants to strings. #7707 [Dan Kubb]

* Allow array and hash query parameters. Array route parameters are converted/to/a/path as before. #6765, #7047, #7462 [bgipsy, Jeremy McAnally, Dan Kubb, brendan] * Allow array and hash query parameters. Array route parameters are converted/to/a/path as before. #6765, #7047, #7462 [bgipsy, Jeremy McAnally, Dan Kubb, brendan]


# Add a #dbman attr_reader for CGI::Session and make CGI::Session::CookieStore#generate_digest public so it's easy to generate digests # Add a #dbman attr_reader for CGI::Session and make CGI::Session::CookieStore#generate_digest public so it's easy to generate digests
Expand Down
Expand Up @@ -6,10 +6,10 @@ class AbstractRequest
# For backward compatibility, the post format is extracted from the # For backward compatibility, the post format is extracted from the
# X-Post-Data-Format HTTP header if present. # X-Post-Data-Format HTTP header if present.
def post_format def post_format
case content_type.to_s case content_type
when 'application/xml' when Mime::XML
:xml :xml
when 'application/x-yaml' when Mime::YAML
:yaml :yaml
else else
:url_encoded :url_encoded
Expand Down
8 changes: 4 additions & 4 deletions actionpack/lib/action_controller/mime_type.rb
Expand Up @@ -24,7 +24,7 @@ class AcceptItem #:nodoc:
def initialize(order, name, q=nil) def initialize(order, name, q=nil)
@order = order @order = order
@name = name.strip @name = name.strip
q ||= 0.0 if @name == "*/*" # default "*/*" to end of list q ||= 0.0 if @name == Mime::ALL # default wilcard match to end of list
@q = ((q || 1.0).to_f * 100).to_i @q = ((q || 1.0).to_f * 100).to_i
end end


Expand Down Expand Up @@ -70,7 +70,7 @@ def parse(accept_header)


# Take care of the broken text/xml entry by renaming or deleting it # Take care of the broken text/xml entry by renaming or deleting it
text_xml = list.index("text/xml") text_xml = list.index("text/xml")
app_xml = list.index("application/xml") app_xml = list.index(Mime::XML.to_s)


if text_xml && app_xml if text_xml && app_xml
# set the q value to the max of the two # set the q value to the max of the two
Expand All @@ -84,9 +84,9 @@ def parse(accept_header)


# delete text_xml from the list # delete text_xml from the list
list.delete_at(text_xml) list.delete_at(text_xml)

elsif text_xml elsif text_xml
list[text_xml].name = "application/xml" list[text_xml].name = Mime::XML.to_s
end end


# Look for more specific xml-based types and sort them ahead of app/xml # Look for more specific xml-based types and sort them ahead of app/xml
Expand Down
8 changes: 4 additions & 4 deletions actionpack/lib/action_controller/request.rb
Expand Up @@ -60,16 +60,16 @@ def content_type
begin begin
# Receive header sans any charset information. # Receive header sans any charset information.
content_type = @env['CONTENT_TYPE'].to_s.sub(/\s*\;.*$/, '').strip.downcase content_type = @env['CONTENT_TYPE'].to_s.sub(/\s*\;.*$/, '').strip.downcase

if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] if x_post_format = @env['HTTP_X_POST_DATA_FORMAT']
case x_post_format.to_s.downcase case x_post_format.to_s.downcase
when 'yaml' when 'yaml'
content_type = 'application/x-yaml' content_type = Mime::YAML.to_s
when 'xml' when 'xml'
content_type = 'application/xml' content_type = Mime::XML.to_s
end end
end end

Mime::Type.lookup(content_type) Mime::Type.lookup(content_type)
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/response.rb
Expand Up @@ -20,7 +20,7 @@ def content_type
end end


def charset=(encoding) def charset=(encoding)
self.headers["Content-Type"] = "#{content_type || "text/html"}; charset=#{encoding}" self.headers["Content-Type"] = "#{content_type || Mime::HTML}; charset=#{encoding}"
end end


def charset def charset
Expand Down
14 changes: 7 additions & 7 deletions actionpack/lib/action_controller/test_process.rb
Expand Up @@ -325,29 +325,29 @@ def close
class TestUploadedFile class TestUploadedFile
# The filename, *not* including the path, of the "uploaded" file # The filename, *not* including the path, of the "uploaded" file
attr_reader :original_filename attr_reader :original_filename

# The content type of the "uploaded" file # The content type of the "uploaded" file
attr_reader :content_type attr_reader :content_type

def initialize(path, content_type = 'text/plain') def initialize(path, content_type = Mime::TEXT)
raise "#{path} file does not exist" unless File.exist?(path) raise "#{path} file does not exist" unless File.exist?(path)
@content_type = content_type @content_type = content_type
@original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 } @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
@tempfile = Tempfile.new(@original_filename) @tempfile = Tempfile.new(@original_filename)
FileUtils.copy_file(path, @tempfile.path) FileUtils.copy_file(path, @tempfile.path)
end end

def path #:nodoc: def path #:nodoc:
@tempfile.path @tempfile.path
end end

alias local_path path alias local_path path

def method_missing(method_name, *args, &block) #:nodoc: def method_missing(method_name, *args, &block) #:nodoc:
@tempfile.send(method_name, *args, &block) @tempfile.send(method_name, *args, &block)
end end
end end

module TestProcess module TestProcess
def self.included(base) def self.included(base)
# execute the request simulating a specific http method and set/volley the response # execute the request simulating a specific http method and set/volley the response
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -470,11 +470,11 @@ def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension) if template_requires_setup?(extension)
body = case extension.to_sym body = case extension.to_sym
when :rxml, :builder when :rxml, :builder
"controller.response.content_type ||= 'application/xml'\n" + "controller.response.content_type ||= Mime::XML\n" +
"xml = Builder::XmlMarkup.new(:indent => 2)\n" + "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
template template
when :rjs when :rjs
"controller.response.content_type ||= 'text/javascript'\n" + "controller.response.content_type ||= Mime::JS\n" +
"update_page do |page|\n#{template}\nend" "update_page do |page|\n#{template}\nend"
end end
else else
Expand Down
14 changes: 7 additions & 7 deletions actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -149,19 +149,19 @@ def javascript_include_tag(*sources)
if !File.exists?(joined_javascript_path) if !File.exists?(joined_javascript_path)
File.open(joined_javascript_path, "w+") do |cache| File.open(joined_javascript_path, "w+") do |cache|
javascript_paths = expand_javascript_sources(sources).collect do |source| javascript_paths = expand_javascript_sources(sources).collect do |source|
compute_public_path(source, 'javascripts', 'js', false) compute_public_path(source, 'javascripts', 'js', false)
end end


cache.write(join_asset_file_contents(javascript_paths)) cache.write(join_asset_file_contents(javascript_paths))
end end
end end


content_tag("script", "", { content_tag("script", "", {
"type" => "text/javascript", "src" => javascript_path(joined_javascript_name) "type" => Mime::JS, "src" => javascript_path(joined_javascript_name)
}.merge(options)) }.merge(options))
else else
expand_javascript_sources(sources).collect do |source| expand_javascript_sources(sources).collect do |source|
content_tag("script", "", { "type" => "text/javascript", "src" => javascript_path(source) }.merge(options)) content_tag("script", "", { "type" => Mime::JS, "src" => javascript_path(source) }.merge(options))
end.join("\n") end.join("\n")
end end
end end
Expand Down Expand Up @@ -252,15 +252,15 @@ def stylesheet_link_tag(*sources)
end end


tag("link", { tag("link", {
"rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "rel" => "Stylesheet", "type" => Mime::CSS, "media" => "screen",
"href" => stylesheet_path(joined_stylesheet_name) "href" => stylesheet_path(joined_stylesheet_name)
}.merge(options)) }.merge(options))
else else
options.delete("cache") options.delete("cache")


expand_stylesheet_sources(sources).collect do |source| expand_stylesheet_sources(sources).collect do |source|
tag("link", { tag("link", {
"rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "href" => stylesheet_path(source) "rel" => "Stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => stylesheet_path(source)
}.merge(options)) }.merge(options))
end.join("\n") end.join("\n")
end end
Expand Down
12 changes: 6 additions & 6 deletions actionpack/lib/action_view/helpers/java_script_macros_helper.rb
Expand Up @@ -198,10 +198,10 @@ def text_field_with_auto_complete(object, method, tag_options = {}, completion_o
content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") + content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") +
auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options)) auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
end end

private private
def auto_complete_stylesheet def auto_complete_stylesheet
content_tag('style', <<-EOT, :type => 'text/css') content_tag('style', <<-EOT, :type => Mime::CSS)
div.auto_complete { div.auto_complete {
width: 350px; width: 350px;
background: #fff; background: #fff;
Expand All @@ -217,17 +217,17 @@ def auto_complete_stylesheet
margin:0; margin:0;
padding:3px; padding:3px;
} }
div.auto_complete ul li.selected { div.auto_complete ul li.selected {
background-color: #ffb; background-color: #ffb;
} }
div.auto_complete ul strong.highlight { div.auto_complete ul strong.highlight {
color: #800; color: #800;
margin:0; margin:0;
padding:0; padding:0;
} }
EOT EOT
end end

end end
end end
end end
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/javascript_helper.rb
Expand Up @@ -132,7 +132,7 @@ def button_to_function(name, *args, &block)
# public/javascripts/ directory, and use +javascript_include_tag+ to # public/javascripts/ directory, and use +javascript_include_tag+ to
# create remote <script> links. # create remote <script> links.
def define_javascript_functions def define_javascript_functions
javascript = '<script type="text/javascript">' javascript = "<script type=\"#{Mime::JS}\">"


# load prototype.js and its extensions first # load prototype.js and its extensions first
prototype_libs = Dir.glob(File.join(JAVASCRIPT_PATH, 'prototype*')).sort.reverse prototype_libs = Dir.glob(File.join(JAVASCRIPT_PATH, 'prototype*')).sort.reverse
Expand Down Expand Up @@ -166,7 +166,7 @@ def escape_javascript(javascript)
# +html_options+ may be a hash of attributes for the <script> tag. Example: # +html_options+ may be a hash of attributes for the <script> tag. Example:
# javascript_tag "alert('All is good')", :defer => 'true' # => <script defer="true" type="text/javascript">alert('All is good')</script> # javascript_tag "alert('All is good')", :defer => 'true' # => <script defer="true" type="text/javascript">alert('All is good')</script>
def javascript_tag(content, html_options = {}) def javascript_tag(content, html_options = {})
content_tag("script", javascript_cdata_section(content), html_options.merge(:type => "text/javascript")) content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS))
end end


def javascript_cdata_section(content) #:nodoc: def javascript_cdata_section(content) #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/url_helper.rb
Expand Up @@ -295,7 +295,7 @@ def mail_to(email_address, name = nil, html_options = {})
for i in 0...tmp.length for i in 0...tmp.length
string << sprintf("%%%x",tmp[i]) string << sprintf("%%%x",tmp[i])
end end
"<script type=\"text/javascript\">eval(unescape('#{string}'))</script>" "<script type=\"#{Mime::JS}\">eval(unescape('#{string}'))</script>"
elsif encode == "hex" elsif encode == "hex"
email_address_encoded = '' email_address_encoded = ''
email_address_obfuscated.each_byte do |c| email_address_obfuscated.each_byte do |c|
Expand Down

0 comments on commit dbf42e3

Please sign in to comment.