Permalink
Browse files

Rewrite to work as a Rails 3-only Engine and Rack middleware

  • Loading branch information...
1 parent c5d61d4 commit 91bb65202472157ec5595383b978d31775a35a36 @justinfrench committed Jul 2, 2010
Showing with 84 additions and 95 deletions.
  1. +0 −10 Rakefile
  2. +0 −1 VERSION
  3. +0 −49 app/metal/publicious_metal.rb
  4. +5 −0 config/routes.rb
  5. +13 −1 lib/publicious.rb
  6. +51 −0 lib/publicious/responder.rb
  7. +0 −3 rails/init.rb
  8. +1 −13 test/multiple_match_test.rb
  9. +1 −13 test/single_match_test.rb
  10. +13 −5 test/test_helper.rb
View
10 Rakefile
@@ -10,16 +10,6 @@ Rake::TestTask.new(:test) do |t|
t.pattern = 'test/*_test.rb'
end
-require 'jeweler'
-Jeweler::Tasks.new do |gemspec|
- gemspec.name = "publicious"
- gemspec.summary = "A Rails gem plugin for plugins to serve images, javascripts and stylesheets from thier own public directory"
- gemspec.description = "A Rails gem plugin for plugins to serve images, javascripts and stylesheets from thier own public directory"
- gemspec.email = "justin@indent.com.au"
- gemspec.homepage = "http://github.com/justinfrench/publicious"
- gemspec.authors = ["Justin French", "Daniel Neighman"]
-end
-
desc 'Generate documentation for the Publicious plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
View
1 VERSION
@@ -1 +0,0 @@
-0.1.0
View
49 app/metal/publicious_metal.rb
@@ -1,49 +0,0 @@
-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
View
5 config/routes.rb
@@ -0,0 +1,5 @@
+Rails::Application.routes.draw do |map|
+ match "/stylesheets/*path" => Publicious::Responder
+ match "/images/*path" => Publicious::Responder
+ match "/javascripts/*path" => Publicious::Responder
+end
View
14 lib/publicious.rb
@@ -1 +1,13 @@
-require File.join(File.dirname(__FILE__), "../rails/init")
+Mime::Type.register "image/jpeg", :jpg
+Mime::Type.register "image/gif", :gif
+Mime::Type.register "image/png", :png
+
+module Publicious
+ class Engine < Rails::Engine
+ engine_name :publicious
+ end
+end
+
+require "publicious/responder"
+
+
View
51 lib/publicious/responder.rb
@@ -0,0 +1,51 @@
+module Publicious
+ class Responder
+
+ 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
+ ::Rails::Engine.subclasses.map { |klass|
+ klass.config.paths.public.paths.first
+ }.flatten.compact.reject { |path| path =~ /publicious/}
+ end
+
+ end
+
+end
View
3 rails/init.rb
@@ -1,3 +0,0 @@
-Mime::Type.register "image/jpeg", :jpg
-Mime::Type.register "image/gif", :gif
-Mime::Type.register "image/png", :png
View
14 test/multiple_match_test.rb
@@ -33,19 +33,7 @@ def setup
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
View
14 test/single_match_test.rb
@@ -23,19 +23,7 @@ def setup
def test_should_respond_with_success
assert last_response.ok?
end
-
- def test_view_paths_should_contain_three_items
- assert_equal 2, ActionController::Base.view_paths.size
- end
-
- def test_public_paths_should_contain_two_items
- 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
-
+
def test_should_respond_with_first_file_contents
assert_equal File.read(@filename), last_response.body
assert_equal @filecontents, last_response.body
View
18 test/test_helper.rb
@@ -1,17 +1,17 @@
require 'rubygems'
require 'test/unit'
require "rack/test"
+require "rails"
require "action_controller"
-require File.join(File.dirname(__FILE__), "../app/metal/publicious_metal")
-require File.join(File.dirname(__FILE__), "../rails/init")
+require "publicious"
RAILS_ROOT = "/tmp"
module TestHelper
include Rack::Test::Methods
def app
- PubliciousMetal
+ Publicious::Responder
end
def setup_vendor_dir
@@ -49,8 +49,16 @@ def setup_plugin(*plugin_names)
plugin_names.each do |plugin_name|
plugin_name = plugin_name.to_s
- ActionController::Base.view_paths << File.join(@vendor_dir, plugin_name, 'app', 'views')
-
+ class_eval "
+ module ::#{plugin_name.classify}
+ class Engine < ::Rails::Engine
+ #engine_name :#{plugin_name}
+ paths.public = '#{@vendor_dir}/#{plugin_name}/public'
+ end
+ end"
+
+ #ActionController::Base.view_paths << File.join(@vendor_dir, plugin_name, 'app', 'views')
+
FileUtils.mkdir(File.join(@vendor_dir, plugin_name))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public'))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'stylesheets'))

0 comments on commit 91bb652

Please sign in to comment.