diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2010-07-30 07:14:48 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2010-09-03 22:59:10 +0200 |
commit | c7664d112fadb313146da33f48d1da318f249927 (patch) | |
tree | 926ace3a678884f4fe114a0ab2f9c112c4d4c7f2 /railties | |
parent | 99131939316230065b4297573d080d1585e4e5a7 (diff) | |
download | rails-c7664d112fadb313146da33f48d1da318f249927.tar.gz rails-c7664d112fadb313146da33f48d1da318f249927.tar.bz2 rails-c7664d112fadb313146da33f48d1da318f249927.zip |
Include application's helpers and router helpers by default, but include engine's ones for controllers inside isolated namespace
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/engine.rb | 9 | ||||
-rw-r--r-- | railties/test/railties/engine_test.rb | 129 |
2 files changed, 138 insertions, 0 deletions
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 85ff09d2af..7f53e2dc72 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -199,6 +199,15 @@ module Rails @endpoint = endpoint if endpoint @endpoint end + + def isolated_engine_for(mod) + _engine = self + mod.singleton_class.instance_eval do + define_method(:_railtie) do + _engine + end + end + end end delegate :middleware, :root, :paths, :to => :config diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index d4ecdf4742..db610451bd 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -298,5 +298,134 @@ module RailtiesTest response = Rails.application.call(env) assert_equal response[2].path, File.join(@plugin.path, "public/bukkits.html") end + + test "shared engine should include application's helpers" do + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match "/foo" => "bukkits/foo#index", :as => "foo" + match "/foo/show" => "bukkits/foo#show" + end + RUBY + + app_file "app/helpers/some_helper.rb", <<-RUBY + module SomeHelper + def something + "Something... Something... Something..." + end + end + RUBY + + @plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY + class Bukkits::FooController < ActionController::Base + def index + render :inline => "<%= something %>" + end + + def show + render :text => foo_path + end + end + RUBY + + boot_rails + + env = Rack::MockRequest.env_for("/foo") + response = Rails.application.call(env) + assert_equal "Something... Something... Something...", response[2].body + + env = Rack::MockRequest.env_for("/foo/show") + response = Rails.application.call(env) + assert_equal "/foo", response[2].body + end + + test "isolated engine should include only its own routes and helpers" do + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + isolated_engine_for Bukkits + end + end + RUBY + + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match "/bar" => "bar#index", :as => "bar" + mount Bukkits::Engine => "/bukkits", :as => "bukkits" + end + RUBY + + @plugin.write "config/routes.rb", <<-RUBY + Bukkits::Engine.routes.draw do + match "/foo" => "bukkits/foo#index", :as => "foo" + match "/foo/show" => "bukkits/foo#show" + match "/from_app" => "bukkits/foo#from_app" + match "/routes_helpers_in_view" => "bukkits/foo#routes_helpers_in_view" + end + RUBY + + app_file "app/helpers/some_helper.rb", <<-RUBY + module SomeHelper + def something + "Something... Something... Something..." + end + end + RUBY + + @plugin.write "app/helpers/engine_helper.rb", <<-RUBY + module EngineHelper + def help_the_engine + "Helped." + end + end + RUBY + + @plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY + class Bukkits::FooController < ActionController::Base + def index + render :inline => "<%= help_the_engine %>" + end + + def show + render :text => foo_path + end + + def from_app + render :inline => "<%= (self.respond_to?(:bar_path) || self.respond_to?(:something)) %>" + end + + def routes_helpers_in_view + render :inline => "<%= foo_path %>, <%= app.bar_path %>" + end + end + RUBY + + @plugin.write "app/mailers/bukkits/my_mailer.rb", <<-RUBY + module Bukkits + class MyMailer < ActionMailer::Base + end + end + RUBY + + boot_rails + + assert_equal Bukkits._railtie, Bukkits::Engine + assert ::Bukkits::MyMailer.method_defined?(:foo_path) + + env = Rack::MockRequest.env_for("/bukkits/from_app") + response = AppTemplate::Application.call(env) + assert_equal "false", response[2].body + + env = Rack::MockRequest.env_for("/bukkits/foo/show") + response = AppTemplate::Application.call(env) + assert_equal "/bukkits/foo", response[2].body + + env = Rack::MockRequest.env_for("/bukkits/foo") + response = AppTemplate::Application.call(env) + assert_equal "Helped.", response[2].body + + env = Rack::MockRequest.env_for("/bukkits/routes_helpers_in_view") + response = AppTemplate::Application.call(env) + assert_equal "/bukkits/foo, /bar", response[2].body + end end end |