Skip to content

Commit

Permalink
allow asset_path with no kind, closes padrino#1532
Browse files Browse the repository at this point in the history
  • Loading branch information
ujifgc authored and Ortuna committed Jan 17, 2014
1 parent cfab048 commit 67d91ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 13 additions & 7 deletions padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -293,16 +297,21 @@ 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)
"#{result_path}#{timestamp}"
end

private

##
# Returns the URI root of the application with optional paths appended.
#
Expand Down Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions padrino-helpers/test/test_asset_tag_helpers.rb
Expand Up @@ -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

0 comments on commit 67d91ce

Please sign in to comment.