From 439d3407eaef1f1b3abc94c766dedac220e59785 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 3 Jan 2012 17:03:45 -0200 Subject: Add font_path helper method --- actionpack/CHANGELOG.md | 2 ++ .../lib/action_view/helpers/asset_tag_helper.rb | 14 ++++++++++++++ actionpack/lib/sprockets/helpers/rails_helper.rb | 5 +++++ .../test/fixtures/sprockets/app/fonts/dir/font.ttf | 0 .../test/fixtures/sprockets/app/fonts/font.ttf | 0 actionpack/test/template/sprockets_helper_test.rb | 20 ++++++++++++++++++++ 6 files changed, 41 insertions(+) create mode 100644 actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf create mode 100644 actionpack/test/fixtures/sprockets/app/fonts/font.ttf (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index cd40982301..f408c50390 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 3.2.0 (unreleased) ## +* Add font_path helper method *Santiago Pastorino* + * Depends on rack ~> 1.4.0 *Santiago Pastorino* * Add :gzip option to `caches_page`. The default option can be configured globally using `page_cache_compression` *Andrey Sitnik* diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 653e12c7d8..5dbba3c4a7 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -306,6 +306,20 @@ module ActionView end alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route + # Computes the path to a font asset in the public fonts directory. + # Full paths from the document root will be passed through. + # + # ==== Examples + # font_path("font") # => /fonts/font + # font_path("font.ttf") # => /fonts/font.ttf + # font_path("dir/font.ttf") # => /fonts/dir/font.ttf + # font_path("/dir/font.ttf") # => /dir/font.ttf + # font_path("http://www.example.com/dir/font.ttf") # => http://www.example.com/dir/font.ttf + def font_path(source) + asset_paths.compute_public_path(source, 'fonts') + end + alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route + # Returns an html image tag for the +source+. The +source+ can be a full # path or a file that exists in your public images directory. # diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb index c34b3f6f26..177f7f6747 100644 --- a/actionpack/lib/sprockets/helpers/rails_helper.rb +++ b/actionpack/lib/sprockets/helpers/rails_helper.rb @@ -63,6 +63,11 @@ module Sprockets end alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route + def font_path(source) + path_to_asset(source) + end + alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route + def javascript_path(source) path_to_asset(source) end diff --git a/actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf b/actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/actionpack/test/fixtures/sprockets/app/fonts/font.ttf b/actionpack/test/fixtures/sprockets/app/fonts/font.ttf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/actionpack/test/template/sprockets_helper_test.rb b/actionpack/test/template/sprockets_helper_test.rb index 64fdd53e73..fb472cfbe5 100644 --- a/actionpack/test/template/sprockets_helper_test.rb +++ b/actionpack/test/template/sprockets_helper_test.rb @@ -24,6 +24,7 @@ class SprocketsHelperTest < ActionView::TestCase @assets.append_path(FIXTURES.join("sprockets/app/javascripts")) @assets.append_path(FIXTURES.join("sprockets/app/stylesheets")) @assets.append_path(FIXTURES.join("sprockets/app/images")) + @assets.append_path(FIXTURES.join("sprockets/app/fonts")) application = Struct.new(:config, :assets).new(config, @assets) Rails.stubs(:application).returns(application) @@ -147,6 +148,14 @@ class SprocketsHelperTest < ActionView::TestCase path_to_image("logo.png") end + test "font_path" do + assert_match %r{/assets/font-[0-9a-f]+.ttf}, + font_path("font.ttf") + + assert_match %r{/assets/font-[0-9a-f]+.ttf}, + path_to_font("font.ttf") + end + test "javascript_path" do assert_match %r{/assets/application-[0-9a-f]+.js}, javascript_path("application.js") @@ -186,6 +195,17 @@ class SprocketsHelperTest < ActionView::TestCase asset_path("/images/logo.gif") end + test "font path through asset_path" do + assert_match %r{/assets/font-[0-9a-f]+.ttf}, + asset_path('font.ttf') + + assert_match %r{/assets/dir/font-[0-9a-f]+.ttf}, + asset_path("dir/font.ttf") + + assert_equal "http://www.example.com/fonts/font.ttf", + asset_path("http://www.example.com/fonts/font.ttf") + end + test "javascript path through asset_path" do assert_match %r{/assets/application-[0-9a-f]+.js}, asset_path(:application, :ext => "js") -- cgit v1.2.3