From 67d91ce6bb4acd9f50c9dc88862380d8ef6917d5 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Thu, 26 Dec 2013 13:49:34 +0400 Subject: [PATCH] allow asset_path with no kind, closes #1532 --- .../lib/padrino-helpers/asset_tag_helpers.rb | 20 ++++++++++++------- .../test/test_asset_tag_helpers.rb | 16 +++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb b/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb index 0ad1887ff..53131196c 100644 --- a/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb +++ b/padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb @@ -6,6 +6,10 @@ module Helpers module AssetTagHelpers APPEND_ASSET_EXTENSIONS = ["js", "css"] ABSOLUTE_URL_PATTERN = %r{^(https?://)} + ASSET_FOLDERS = { + :js => 'javascripts', + :css => 'stylesheets', + } ## # Creates a div to display the flash of given type if it exists. @@ -293,9 +297,13 @@ def image_path(src) # # Generates: /images/example.jpg?1269008689 # asset_path :images, 'example.jpg' # - def asset_path(kind, source) + # # Generates: /uploads/file.ext?1269008689 + # asset_path 'uploads/file.ext' + # + def asset_path(kind, source = nil) + kind, source = source, kind if source.nil? source = asset_normalize_extension(kind, URI.escape(source.to_s)) - return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// # absolute source + return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// source = File.join(asset_folder_name(kind), source) timestamp = asset_timestamp(source) result_path = uri_root_path(source) @@ -303,6 +311,7 @@ def asset_path(kind, source) end private + ## # Returns the URI root of the application with optional paths appended. # @@ -340,16 +349,13 @@ def asset_timestamp(file_path) # asset_folder_name(:css) => 'stylesheets' # asset_folder_name(:js) => 'javascripts' # asset_folder_name(:images) => 'images' + # asset_folder_name(:abrakadabrah) => 'abrakadabrah' # def asset_folder_name(kind) if self.class.respond_to? "#{kind}_asset_folder" self.class.send "#{kind}_asset_folder" else - case kind - when :css then 'stylesheets' - when :js then 'javascripts' - else kind.to_s - end + (ASSET_FOLDERS[kind] || kind).to_s end end diff --git a/padrino-helpers/test/test_asset_tag_helpers.rb b/padrino-helpers/test/test_asset_tag_helpers.rb index 4749a4796..a510a7084 100644 --- a/padrino-helpers/test/test_asset_tag_helpers.rb +++ b/padrino-helpers/test/test_asset_tag_helpers.rb @@ -374,4 +374,20 @@ def flash assert_has_tag('link', :type => 'my-type', :rel => 'my-rel', :href => "/blog/post.rss", :title => 'my-title') { feed_tag :rss, "/blog/post.rss", :type => "my-type", :rel => "my-rel", :title => "my-title" } end end + + context 'for #asset_path method' do + should 'generate proper paths for js and css' do + assert_match /\/javascripts\/app.js\?\d+/, asset_path(:js, 'app') + assert_match /\/stylesheets\/app.css\?\d+/, asset_path(:css, 'app') + end + + should 'generate proper paths for images and other files' do + assert_match /\/images\/app.png\?\d+/, asset_path(:images, 'app.png') + assert_match /\/documents\/app.pdf\?\d+/, asset_path(:documents, 'app.pdf') + end + + should 'generate proper paths for public folder' do + assert_match /\/files\/file.ext\?\d+/, asset_path('files/file.ext') + end + end end