Permalink
Browse files

Revert "Changes out the responder and removes the engine"

This reverts commit 5e890ba.

I accidentally rebased on top of this, now my commits are out of order.
  • Loading branch information...
1 parent 5e890ba commit d95d4826f3e3a306f9621e55322d2942300acd45 @justinfrench committed Jul 22, 2010
Showing with 72 additions and 78 deletions.
  1. +5 −0 config/routes.rb
  2. +4 −64 lib/publicious.rb
  3. +51 −0 lib/publicious/responder.rb
  4. +12 −14 test/test_helper.rb
View
@@ -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
@@ -2,71 +2,11 @@
Mime::Type.register "image/gif", :gif
Mime::Type.register "image/png", :png
-class Publicious
- FILE_METHODS = %w(GET HEAD).freeze
-
- def initialize(app)
- @app = app
- yield if block_given?
- end
-
- def klass
- self.class
- end
-
- def call(env)
- request = Rack::Request.new(env)
- return @app.call(env) unless FILE_METHODS.include?(request.request_method)
-
- if request.path_info =~ %r{^\/(#{klass.allowed_dirs.join("|")})}
- file_name = nil
- path = nil
-
- klass.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
- @app.call(env)
- end
- end
-
- def respond_not_found!
- Rack::Response.new("Not Found", 404).finish
- end
-
- def 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.allowed_dirs
- %w(stylesheets javascripts images)
+module Publicious
+ class Engine < Rails::Engine
end
+end
- def self.public_paths
- @public_paths ||= begin
- paths = []
- paths += add_engines_public_paths! if defined?(::Rails::Engine)
- paths
- end
- end
+require "publicious/responder"
- def self.add_engines_public_paths!
- ::Rails::Engine.subclasses.map { |klass|
- klass.config.paths.public.paths.first
- }.flatten.compact.reject { |path| path =~ /publicious/}
- end
-end
@@ -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
@@ -7,22 +7,20 @@
RAILS_ROOT = "/tmp"
-NOT_FOUND_APP = lambda{|e| Rack::Response.new("NOT FOUND", 404).finish}
-
module TestHelper
include Rack::Test::Methods
-
+
def app
- Publicious.new(NOT_FOUND_APP)
+ Publicious::Responder
end
-
+
def setup_vendor_dir
unless @vendor_dir
@vendor_dir = "/tmp/vendor"
FileUtils.mkdir(@vendor_dir)
end
end
-
+
def teardown_vendor_dir
if @vendor_dir
begin
@@ -32,41 +30,41 @@ def teardown_vendor_dir
end
end
end
-
+
def setup_standard_view_paths
ActionController::Base.view_paths = ['/tmp/app/views']
end
-
+
def setup
setup_vendor_dir
setup_standard_view_paths
end
-
+
def teardown
teardown_vendor_dir
setup_standard_view_paths
end
-
+
def setup_plugin(*plugin_names)
plugin_names.each do |plugin_name|
plugin_name = plugin_name.to_s
-
+
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'))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'images'))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'javascripts'))
end
end
-
+
end

0 comments on commit d95d482

Please sign in to comment.