Permalink
Browse files

Initial steps towards compatability with Rails 2.3. Removed routing a…

…nd ActionMailer extensions, and we no longer need to add view paths from plugins.
  • Loading branch information...
1 parent 15ebe4b commit a57772ac0c6fab7a64501f8cfe811a10a371afe0 @lazyatom committed Apr 20, 2009
View
@@ -172,18 +172,29 @@ namespace :test do
desc 'Update the plugin and tests files in the test application from the plugin'
task :mirror_engine_files => [:test_app, :copy_engines_plugin] do
- puts "> Modifying default config files to load engines plugin"
+ puts "> Tweaking generated application to be suitable for testing"
+
+ # Replace the Rails plugin loader with the engines one.
insert_line("require File.join(File.dirname(__FILE__), '../vendor/plugins/engines/boot')",
:into => 'config/environment.rb',
:after => "require File.join(File.dirname(__FILE__), 'boot')")
-
- insert_line('map.from_plugin :test_routing', :into => 'config/routes.rb',
- :after => /\AActionController::Routing::Routes/)
-
+
+ # Add the engines test helper to handle fixtures & stuff.
insert_line("require 'engines_test_helper'", :into => 'test/test_helper.rb')
+ # Run engine plugin tests when running the application
insert_line("task :test => ['test:engines:all']", :into => 'Rakefile')
+ # We want exceptions to be raised
+ insert_line("def rescue_action(e) raise e end;",
+ :into => "app/controllers/application_controller.rb",
+ :after => "class ApplicationController < ActionController::Base")
+
+ # We need this method to test where actions are being rendered from.
+ insert_line("include RenderInformation",
+ :into => "app/controllers/application_controller.rb",
+ :after => "class ApplicationController < ActionController::Base")
+
puts "> Mirroring test application files into #{test_app_dir}"
mirror_test_files('app')
mirror_test_files('lib')
View
@@ -1,5 +1,5 @@
# Only call Engines.init once, in the after_initialize block so that Rails
# plugin reloading works when turned on
config.after_initialize do
- Engines.init if defined? :Engines
-end
+ Engines.init(initializer) if defined? :Engines
+end
View
@@ -43,7 +43,7 @@ module Engines
# List of extensions to load, can be changed in init.rb before calling Engines.init
mattr_accessor :rails_extensions
- self.rails_extensions = %w(action_mailer asset_helpers form_tag_helpers routing migrations dependencies)
+ self.rails_extensions = %w(asset_helpers form_tag_helpers migrations dependencies)
# The name of the public directory to mirror public engine assets into.
# Defaults to <tt>RAILS_ROOT/public/plugin_assets</tt>.
@@ -81,7 +81,7 @@ module Engines
self.code_mixing_file_types = %w(controller helper)
class << self
- def init
+ def init(initializer)
load_extensions
Engines::Assets.initialize_base_public_directory
end
View
@@ -75,7 +75,6 @@ def load_paths
def load(initializer)
return if loaded?
super initializer
- add_plugin_view_paths
add_plugin_locale_paths
Assets.mirror_files_for(self)
end
@@ -86,13 +85,6 @@ def select_existing_paths(name)
Engines.select_existing_paths(self.send(name).map { |p| File.join(directory, p) })
end
- def add_plugin_view_paths
- view_path = File.join(directory, 'app', 'views')
- if File.exist?(view_path)
- ActionController::Base.view_paths.insert(1, view_path) # push it just underneath the app
- end
- end
-
def add_plugin_locale_paths
locale_path = File.join(directory, 'locales')
return unless File.exists?(locale_path)
@@ -112,11 +104,6 @@ def public_asset_directory
"#{File.basename(Engines.public_directory)}/#{name}"
end
- # The path to this plugin's routes file
- def routes_path
- File.join(directory, "routes.rb")
- end
-
# The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>)
def migration_directory
File.join(self.directory, 'db', 'migrate')
@@ -5,14 +5,7 @@ class Loader < Rails::Plugin::Loader
def register_plugin_as_loaded(plugin)
super plugin
Engines.plugins << plugin
- register_to_routing(plugin)
end
-
- # Registers the plugin's controller_paths for the routing system.
- def register_to_routing(plugin)
- initializer.configuration.controller_paths += plugin.select_existing_paths(:controller_paths)
- initializer.configuration.controller_paths.uniq!
- end
end
end
end
@@ -1,82 +0,0 @@
-# The way ActionMailer is coded in terms of finding templates is very restrictive, to the point
-# where all templates for rendering must exist under the single base path. This is difficult to
-# work around without re-coding significant parts of the action mailer code.
-#
-# ---
-#
-# The MailTemplates module overrides two (private) methods from ActionMailer to enable mail
-# templates within plugins:
-#
-# [+template_path+] which now produces the contents of #template_paths
-# [+initialize_template_class+] which now find the first matching template and creates
-# an ActionVew::Base instance with the correct view_paths
-#
-# Ideally ActionMailer would use the same template-location logic as ActionView, and the same
-# view paths as ActionController::Base.view_paths, but it currently does not.
-module Engines::RailsExtensions::ActionMailer
- def self.included(base) #:nodoc:
- base.class_eval do
- alias_method_chain :template_path, :engine_additions
- alias_method_chain :initialize_template_class, :engine_additions
- end
- end
-
- private
-
- #--
- # ActionMailer::Base#create uses two mechanisms to determine the proper template file(s)
- # to load. Firstly, it searches within the template_root for files that much the explicit
- # (or implicit) part encodings (like signup.text.plain.erb for the signup action).
- # This is how implicit multipart emails are built, by the way.
- #
- # Secondly, it then creates an ActionMailer::Base instance with it's view_paths parameter
- # set to the template_root, so that ActionMailer will then take over rendering the
- # templates.
- #
- # Ideally, ActionMailer would pass the same set of view paths as it gets in a normal
- # request (i.e. ActionController::Base.view_paths), so that all possible view paths
- # were searched. However, this seems to introduce some problems with helper modules.
- #
- # So instead, and because we have to fool these two independent parts of ActionMailer,
- # we fudge with the mechanisms it uses to find the templates (via template_paths, and
- # template_path_with_engine_additions), and then intercept the creation of the ActionView
- # instance so we can set the view_paths (in initialize_template_class_with_engine_additions).
- #++
-
- # Returns all possible template paths for the current mailer, including those
- # within the loaded plugins.
- def template_paths
- paths = Engines.plugins.by_precedence.map { |p| "#{p.directory}/app/views/#{mailer_name}" }
- paths.unshift(template_path_without_engine_additions) unless Engines.disable_application_view_loading
- paths
- end
-
- # Return something that Dir[] can glob against. This method is called in
- # ActionMailer::Base#create! and used as part of an argument to Dir. We can
- # take advantage of this by using some of the features of Dir.glob to search
- # multiple paths for matching files.
- def template_path_with_engine_additions
- "{#{template_paths.join(",")}}"
- end
-
- # Return an instance of ActionView::Base with the view paths set to all paths
- # in ActionController::Base.view_paths (i.e. including all plugin view paths)
- def initialize_template_class_with_engine_additions(assigns)
- # I'd like to just return this, but I get problems finding methods in helper
- # modules if the method implemention from the regular class is not called
- #
- # ActionView::Base.new(ActionController::Base.view_paths.dup, assigns, self)
- renderer = initialize_template_class_without_engine_additions(assigns)
- renderer.view_paths.unshift(*ActionController::Base.view_paths.dup)
- renderer
- end
-end
-
-# We don't need to do this if ActionMailer hasn't been loaded.
-if Object.const_defined?(:ActionMailer)
- module ::ActionMailer #:nodoc:
- class Base #:nodoc:
- include Engines::RailsExtensions::ActionMailer
- end
- end
-end
@@ -1,83 +0,0 @@
-# Effective use of Rails' routes can help create a tidy and elegant set of URLs,
-# and is a significant part of creating an external API for your web application.
-#
-# When developing plugins which contain controllers, it seems obvious that including
-# the corresponding routes would be extremely useful. This is particularly true
-# when exposing RESTful resources using the new REST-ian features of Rails.
-#
-# == Including routes in your plugin
-#
-# The engines plugin makes it possible to include a set of routes within your plugin
-# very simply, as it turns out. Include a <tt>routes.rb</tt> file like the one below
-# at the root of your plugin (along-side <tt>init.rb</tt> and <tt>lib/</tt>):
-#
-# connect "/login", :controller => "account", :action => "login"
-#
-# # add a named route
-# logout "/logout", :controller => "account", :action => "logout"
-#
-# # some restful stuff
-# resources :things do |t|
-# t.resources :other_things
-# end
-#
-# Everywhere in a normal <tt>RAILS_ROOT/config/routes.rb</tt> file
-# where you might have <tt>map.connect</tt>, you just use <tt>connect</tt> in your
-# plugin's <tt>routes.rb</tt>.
-#
-# === Hooking it up in your application
-#
-# While it would be possible to have each plugin's routes automagically included into
-# the application's route set, to do so would actually be a stunningly bad idea. Route
-# priority is the key issue here. You, the application developer, needs to be in complete
-# control when it comes to specifying the priority of routes in your application, since
-# the ordering of your routes directly affects how Rails will interpret incoming requests.
-#
-# To add plugin routes into your application's <tt>routes.rb</tt> file, you need to explicitly
-# map them in using the Engines::RailsExtensions::Routing#from_plugin method:
-#
-# ApplicationController::Routing::Routes.draw do |map|
-#
-# map.connect "/app_stuff", :controller => "application_thing" # etc...
-#
-# # This line includes the routes from the given plugin at this point, giving you
-# # control over the priority of your application routes
-# map.from_plugin :your_plugin
-#
-# map.connect ":controller/:action/:id"
-# end
-#
-# By including routes in plugins which have controllers, you can now share in a simple way
-# a compact and elegant URL scheme which corresponds to those controllers.
-#
-# ---
-#
-# The Engines::RailsExtensions::Routing module defines extensions to Rails'
-# routing (ActionController::Routing) mechanism such that routes can be loaded
-# from a given plugin.
-#
-# The key method is Engines::RailsExtensions::Routing#from_plugin, which can be called
-# within your application's <tt>config/routes.rb</tt> file to load plugin routes at that point.
-#
-module Engines::RailsExtensions::Routing
- # Loads the set of routes from within a plugin and evaluates them at this
- # point within an application's main <tt>routes.rb</tt> file.
- #
- # Plugin routes are loaded from <tt><plugin_root>/routes.rb</tt>.
- def from_plugin(name)
- map = self # to make 'map' available within the plugin route file
- routes_path = Engines.plugins[name].routes_path
- eval(IO.read(routes_path), binding, routes_path) if File.file?(routes_path)
- end
-end
-
-
-module ::ActionController #:nodoc:
- module Routing #:nodoc:
- class RouteSet #:nodoc:
- class Mapper #:nodoc:
- include Engines::RailsExtensions::Routing
- end
- end
- end
-end
@@ -1,18 +0,0 @@
-# Filters added to this controller apply to all controllers in the application.
-# Likewise, all the methods added will be available for all controllers.
-
-class ApplicationController < ActionController::Base
- def render_class_and_action(note = nil, options={})
- text = "rendered in #{self.class.name}##{params[:action]}"
- text += " (#{note})" unless note.nil?
- render options.update(:text => text)
- end
-
- def rescue_action(e) raise e end;
-
- helper :all # include all helpers, all the time
-
- # See ActionController::RequestForgeryProtection for details
- # Uncomment the :secret if you're not using the cookie session store
- protect_from_forgery # :secret => 'b955354e438fc4ba070083505af94518'
-end
@@ -7,7 +7,7 @@
require File.dirname(__FILE__) + '/../test_helper'
-class ControllerLoadingTest < Test::Unit::TestCase
+class ControllerLoadingTest < ActionController::TestCase
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
-class ExceptionNotificationCompatibilityTest < Test::Unit::TestCase
+class ExceptionNotificationCompatibilityTest < ActionController::TestCase
ExceptionNotifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)
class SimpleController < ApplicationController
include ExceptionNotifiable
@@ -5,7 +5,7 @@
require File.dirname(__FILE__) + '/../test_helper'
-class LocaleLoadingTest < Test::Unit::TestCase
+class LocaleLoadingTest < ActionController::TestCase
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -7,7 +7,7 @@
require File.dirname(__FILE__) + '/../test_helper'
-class ViewLoadingTest < Test::Unit::TestCase
+class ViewLoadingTest < ActionController::TestCase
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -0,0 +1,7 @@
+module RenderInformation
+ def render_class_and_action(note = nil, options={})
+ text = "rendered in #{self.class.name}##{params[:action]}"
+ text += " (#{note})" unless note.nil?
+ render options.update(:text => text)
+ end
+end

0 comments on commit a57772a

Please sign in to comment.