Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed `SystemStackError: stack level too deep' issue #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/themes_for_rails/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def images
end

private

def handle_asset(prefix)
asset, theme = params[:asset], params[:theme]
find_themed_asset(asset, theme, prefix) do |path, mime_type|
Expand All @@ -26,14 +26,14 @@ def handle_asset(prefix)
send_file path, :type => mime_type, :disposition => "inline"
end
end

def find_themed_asset(asset_name, asset_theme, asset_type, &block)
path = asset_path(asset_name, asset_theme, asset_type)
if File.exists?(path)
yield path, mime_type_for(request)
elsif File.extname(path).blank?
elsif File.extname(path).blank? && extension_from(request.path_info).present?
asset_name = "#{asset_name}.#{extension_from(request.path_info)}"
return find_themed_asset(asset_name, asset_theme, asset_type, &block)
return find_themed_asset(asset_name, asset_theme, asset_type, &block)
else
render_not_found
end
Expand All @@ -46,21 +46,21 @@ def asset_path(asset_name, asset_theme, asset_type)
def render_not_found
render :text => 'Not found', :status => 404
end

def mime_type_for(request)
existing_mime_type = mime_type_from_uri(request.path_info)
unless existing_mime_type.nil?
unless existing_mime_type.nil?
existing_mime_type.to_s
else
"image/#{extension_from(request.path_info)}"
end
end

def mime_type_from_uri(path)
extension = extension_from(path)
Mime::Type.lookup_by_extension(extension)
end

def extension_from(path)
File.extname(path).to_s[1..-1]
end
Expand Down
23 changes: 15 additions & 8 deletions test/lib/assets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
require File.expand_path("test/test_helper.rb")

module ThemesForRails
class AssetsControllerTest < ::ActionController::TestCase
class AssetsControllerTest < ::ActionController::TestCase
tests ThemesForRails::AssetsController

should "respond to stylesheets" do
assert @controller.respond_to?(:stylesheets)
end
Expand All @@ -14,12 +14,19 @@ class AssetsControllerTest < ::ActionController::TestCase
assert_response :success
assert_equal @response.content_type, 'text/css'
end

should "not be success when the stylesheet file is not found" do
get 'stylesheets', { :theme => 'default', :asset => 'oldstyle.css'}
assert_response :missing
end


should "not raise an exception with a missing extension" do
assert_nothing_raised do
get 'stylesheets', { :theme => 'default', :asset => 'style'}
end
assert_response :missing
end

# javascripts
should "respond to javascripts" do
assert @controller.respond_to?(:javascripts)
Expand All @@ -41,7 +48,7 @@ class AssetsControllerTest < ::ActionController::TestCase
get 'javascripts', { :theme => 'default', :asset => 'oldapp.js'}
assert_response :missing
end

# images
should "respond to images" do
assert @controller.respond_to?(:images)
Expand All @@ -52,18 +59,18 @@ class AssetsControllerTest < ::ActionController::TestCase
assert_response :success
assert_equal 'image/png', @response.content_type
end

should "not be success when the image file is not found" do
get 'images', { :theme => 'default', :asset => 'i_am_not_here.jpg'}
assert_response :missing
end

should "respond with a nested asset" do
get 'images', { :theme => 'default', :asset => 'nested/logo.png'}
assert_response :success
assert_equal 'image/png', @response.content_type
end

should "respond with properly even when requesting an image inside the stylesheets folder" do
get 'stylesheets', { :theme => 'default', :asset => 'images/logo.png'}
assert_response :success
Expand Down