Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced the Controller with a piece of Metal:
* removed controller * removed routing * added a Metal/Rack middleware to handle the requests instead * updated README Many thanks to Daniel Neighman (hassox) for his help converting this to Rack at RailsCamp in Melbourne!
- Loading branch information
1 parent
dbdae08
commit fdd7343
Showing
11 changed files
with
202 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
class PubliciousMetal | |||
|
|||
def self.call(env) | |||
request = Rack::Request.new(env) | |||
if request.path_info =~ %r{^\/(#{allowed_dirs.join("|")})} | |||
file_name = nil | |||
path = nil | |||
|
|||
public_paths.detect do |pub_path| | |||
path = pub_path | |||
fp = File.join(pub_path, request.path_info) | |||
file_name = fp if File.file?(fp) | |||
end | |||
|
|||
return respond_not_found! unless file_name | |||
|
|||
# Make sure pricks aren't ../../config/database.yml ing us | |||
respond_not_found! unless file_name.gsub(%r[^#{path}], "") == request.path_info | |||
|
|||
Rack::Response.new( | |||
File.open(file_name), | |||
200,'Content-Type' => content_type_for_file(file_name) | |||
).finish | |||
else | |||
respond_not_found! | |||
end | |||
end | |||
|
|||
def self.respond_not_found! | |||
Rack::Response.new("Not Found", 404).finish | |||
end | |||
|
|||
def self.allowed_dirs | |||
%w(stylesheets javascripts images) | |||
end | |||
|
|||
def self.content_type_for_file(name) | |||
file_name = File.basename(name).split(".").last.to_s | |||
Mime::Type.lookup_by_extension(file_name).to_s | |||
end | |||
|
|||
def self.public_paths | |||
ActionController::Base.view_paths.map do |vp| | |||
full_path = File.expand_path(vp.to_s, RAILS_ROOT) | |||
full_path.sub("app/views", "public") if full_path =~ /vendor/ | |||
end.compact | |||
end | |||
|
|||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
Mime::Type.register "image/jpeg", :jpg | |||
Mime::Type.register "image/gif", :gif | |||
Mime::Type.register "image/png", :png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,54 @@ | |||
require File.dirname(__FILE__) + '/test_helper' | |||
|
|||
class MultipleMatchesTest < Test::Unit::TestCase | |||
|
|||
include TestHelper | |||
|
|||
def setup | |||
super | |||
|
|||
setup_plugin :no_file_here | |||
setup_plugin :my_plugin | |||
setup_plugin :your_plugin | |||
|
|||
@path = 'foo/bah.css' | |||
|
|||
@filename_1 = "/tmp/vendor/my_plugin/public/stylesheets/#{@path}" | |||
@filecontents_1 = "hello from my stylesheet" | |||
FileUtils.mkdir(File.dirname(@filename_1)) | |||
File.open(@filename_1, 'w') do |file| | |||
file << @filecontents_1 | |||
end | |||
|
|||
@filename_2 = "/tmp/vendor/your_plugin/public/stylesheets/#{@path}" | |||
@filecontents_2 = "hello from your stylesheet" | |||
FileUtils.mkdir(File.dirname(@filename_2)) | |||
File.open(@filename_2, 'w') do |file| | |||
file << @filecontents_2 | |||
end | |||
|
|||
get "/stylesheets/#{@path}" | |||
end | |||
|
|||
def test_should_respond_with_success | |||
assert last_response.ok? | |||
end | |||
|
|||
def test_view_paths_should_contain_three_items | |||
assert_equal 4, ActionController::Base.view_paths.size | |||
end | |||
|
|||
def test_public_paths_should_contain_two_items | |||
assert_equal 3, app.public_paths.size # one less that view_paths | |||
end | |||
|
|||
def test_public_paths_should_contain_the_plugins_public_dir | |||
assert_equal "/tmp/vendor/my_plugin/public", app.public_paths.second | |||
end | |||
|
|||
def test_should_respond_with_first_file_contents | |||
assert_equal File.read(@filename_1), last_response.body | |||
assert_equal @filecontents_1, last_response.body | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
require File.join(File.dirname(__FILE__), "test_helper") | |||
|
|||
class NoMatchTest < Test::Unit::TestCase | |||
|
|||
include TestHelper | |||
|
|||
def test_redirect_logged_in_users_to_dashboard | |||
get "/stylesheets/not_here.css" | |||
assert last_response.not_found? | |||
end | |||
|
|||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,37 +1,44 @@ | |||
require File.dirname(__FILE__) + '/test_helper' | require File.join(File.dirname(__FILE__), "test_helper") | ||
|
|||
class SingleMatchTest < PubliciousBaseTest | |||
|
|
||
class SingleMatchTest < Test::Unit::TestCase | |||
|
|||
include TestHelper | |||
|
|||
def setup | def setup | ||
super | super | ||
|
|||
setup_plugin :my_plugin | setup_plugin :my_plugin | ||
|
|
||
@path = ['foo', 'bah.css'] | @path = 'foo/bah.css' | ||
@filename = "/tmp/vendor/my_plugin/public/stylesheets/#{@path.join('/')}" | @filename = "/tmp/vendor/my_plugin/public/stylesheets/#{@path}" | ||
@filecontents = "hello from stylesheet" | @filecontents = "hello from stylesheet" | ||
FileUtils.mkdir(File.dirname(@filename)) | FileUtils.mkdir(File.dirname(@filename)) | ||
File.open(@filename, 'w') do |file| | File.open(@filename, 'w') do |file| | ||
file << "hello from stylesheet" | file << "hello from stylesheet" | ||
end | end | ||
|
|
||
get :show, :path => @path | get "/stylesheets/#{@path}" | ||
end | end | ||
|
|
||
test "view_paths should contain two items" do | def test_should_respond_with_success | ||
assert_equal 2, @controller.view_paths.size | assert last_response.ok? | ||
end | end | ||
|
|
||
test "public_paths should contain one item (one less than view paths)" do | def test_view_paths_should_contain_three_items | ||
assert_equal 1, @controller.send(:public_paths).size | assert_equal 2, ActionController::Base.view_paths.size | ||
end | end | ||
|
|
||
test "public_paths should contain the plugin's public dir" do | def test_public_paths_should_contain_two_items | ||
assert_equal "/tmp/vendor/my_plugin/public", @controller.send(:public_paths).first | assert_equal 1, app.public_paths.size # one less that view_paths | ||
end | |||
|
|||
def test_public_paths_should_contain_the_plugins_public_dir | |||
assert_equal "/tmp/vendor/my_plugin/public", app.public_paths.first | |||
end | end | ||
|
|
||
test "should respond with the file contents" do | def test_should_respond_with_first_file_contents | ||
assert_equal File.read(@filename), @controller.response.body | assert_equal File.read(@filename), last_response.body | ||
assert_equal @filecontents, @controller.response.body | assert_equal @filecontents, last_response.body | ||
end | end | ||
|
|
||
end | end |
Oops, something went wrong.