1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added AssetTagHelper that provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@689 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-02-19 16:04:50 +00:00
parent ad1fe7dd27
commit 2a5fc27cea
4 changed files with 107 additions and 1 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Added AssetTagHelper that provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
* Added FormTagHelper that provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like FormHelper does. With the FormTagHelper, you provide the names and values yourself.
* Added Iran and Irak to the countries list used by FormOptions#country_select and FormOptions#country_options_for_select

View file

@ -0,0 +1,59 @@
require 'cgi'
require File.dirname(__FILE__) + '/url_helper'
require File.dirname(__FILE__) + '/tag_helper'
module ActionView
module Helpers
# Provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
module AssetTagHelper
# Returns a link tag that browsers and news readers can use to auto-detect a RSS or ATOM feed for this page. The +type+ can
# either be <tt>:rss</tt> (default) or <tt>:atom</tt> and the +options+ follow the url_for style of declaring a link target.
#
# Examples:
# auto_discovery_link_tag # =>
# <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/controller/action" />
# auto_discovery_link_tag(:atom) # =>
# <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/action" />
# auto_discovery_link_tag(:rss, :action => "feed") # =>
# <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/feed" />
def auto_discovery_link_tag(type = :rss, options = {})
tag(
"link", "rel" => "alternate", "type" => "application/#{type}+xml", "title" => type.to_s.upcase,
"href" => url_for(options.merge(:only_path => false))
)
end
# Returns a script include tag per source given as argument. Examples:
#
# javascript_include_tag "xmlhr" # =>
# <script language="JavaScript" type="text/javascript" src="/javascripts/xmlhr.js"></script>
#
# javascript_include_tag "common.javascript", "/elsewhere/cools" # =>
# <script language="JavaScript" type="text/javascript" src="/javascripts/common.javascript"></script>
# <script language="JavaScript" type="text/javascript" src="/elsewhere/cools.js"></script>
def javascript_include_tag(*sources)
sources.collect { |source|
source = "/javascripts/#{source}" unless source.include?("/")
source = "#{source}.js" unless source.include?(".")
content_tag("script", "", "language" => "JavaScript", "type" => "text/javascript", "src" => source)
}.join("\n")
end
# Returns a css link tag per source given as argument. Examples:
#
# stylesheet_link_tag "style" # =>
# <link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />
#
# stylesheet_link_tag "random.styles", "/css/stylish" # =>
# <link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />
# <link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />
def stylesheet_link_tag(*sources)
sources.collect { |source|
source = "/stylesheets/#{source}" unless source.include?("/")
source = "#{source}.css" unless source.include?(".")
tag("link", "rel" => "Stylesheet", "type" => "text/css", "media" => "screen", "href" => source)
}.join("\n")
end
end
end
end

View file

@ -0,0 +1,45 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
class AssetTagHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::AssetTagHelper
def setup
@controller = Class.new do
def url_for(options, *parameters_for_method_reference)
"http://www.world.com"
end
end
@controller = @controller.new
end
AutoDiscoveryToTag = {
%(auto_discovery_link_tag) => %(<link href="http://www.world.com" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:atom)) => %(<link href="http://www.world.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
%(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.world.com" rel="alternate" title="RSS" type="application/rss+xml" />),
}
JavascriptIncludeToTag = {
%(javascript_include_tag("xmlhr")) => %(<script language="JavaScript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
%(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(<script language="JavaScript" src="/javascripts/common.javascript" type="text/javascript"></script>\n<script language="JavaScript" src="/elsewhere/cools.js" type="text/javascript"></script>),
}
StyleLinkToTag = {
%(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
%(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />)
}
def test_auto_discovery
AutoDiscoveryToTag.each { |method, tag| assert_equal(tag, eval(method)) }
end
def test_javascript_include
JavascriptIncludeToTag.each { |method, tag| assert_equal(tag, eval(method)) }
end
def test_style_link
StyleLinkToTag.each { |method, tag| assert_equal(tag, eval(method)) }
end
end

View file

@ -1,7 +1,7 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper'
class TagHelperTest < Test::Unit::TestCase
class FormTagHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormTagHelper