From 2a5fc27ceac142762dc66680364be17b100ab0b8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 19 Feb 2005 16:04:50 +0000 Subject: 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 --- actionpack/CHANGELOG | 2 + .../lib/action_view/helpers/asset_tag_helper.rb | 59 ++++++++++++++++++++++ actionpack/test/template/asset_tag_helper_test.rb | 45 +++++++++++++++++ actionpack/test/template/form_tag_helper_test.rb | 2 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 actionpack/lib/action_view/helpers/asset_tag_helper.rb create mode 100644 actionpack/test/template/asset_tag_helper_test.rb (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 342f96f3ee..e153726ffc 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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 diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb new file mode 100644 index 0000000000..33914b2144 --- /dev/null +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -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 :rss (default) or :atom and the +options+ follow the url_for style of declaring a link target. + # + # Examples: + # auto_discovery_link_tag # => + # + # auto_discovery_link_tag(:atom) # => + # + # auto_discovery_link_tag(:rss, :action => "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" # => + # + # + # javascript_include_tag "common.javascript", "/elsewhere/cools" # => + # + # + 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" # => + # + # + # stylesheet_link_tag "random.styles", "/css/stylish" # => + # + # + 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 diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb new file mode 100644 index 0000000000..a8d01f18f5 --- /dev/null +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -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) => %(), + %(auto_discovery_link_tag(:atom)) => %(), + %(auto_discovery_link_tag(:rss, :action => "feed")) => %(), + } + + JavascriptIncludeToTag = { + %(javascript_include_tag("xmlhr")) => %(), + %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(\n), + } + + StyleLinkToTag = { + %(stylesheet_link_tag("style")) => %(), + %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(\n) + } + + 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 \ No newline at end of file diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 25da1937d4..6036dd4b50 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -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 -- cgit v1.2.3