Hi engines developers,
I'm working on a project that uses Cells extensively
(http://nick.smt.de/trac/nick/wiki/Cells). Cells relies on Engines in
turn. I hit a wall when I tried to make cells load a class tree of cells
This was caused by the way Cells works, but I will not bother you with
the boring details of that.
I decided to rework the way cells loads its classes, and the best way to do
this is via the autoload dependencies system, which is overridden by Engines.
This requires that the correct file is loaded not only when the user refers
to FooController (from controllers/foo_controller.rb), but also when the
user refers to FooCell (from cells/foo_cell.rb).
This can be done by copying over the entire
require_or_load_with_engine_additions and adding 'cell' to the file_types,
but that would be silly and very un-DRY. Instead, I propose this patch
(see attachment). It adds a new mattr_accessor Engines.file_types which
defaults to ['controller', 'helper']. Cells then adds to this so it reads
['controller', 'helper', 'cell'] and hacks the load paths in such a way
that the correct load paths are looked up.
In my opinion, this small patch paves the way for other cell-like extensions
to Rails, so I would like to see this integrated in Engines.
Comments? Suggestions?
Regards,
Peter Bex
Solide ICT - http://www.solide-ict.nl
Index: engines/rails_extensions/dependencies.rb
===================================================================
--- engines/rails_extensions/dependencies.rb (revision 615)
+++ engines/rails_extensions/dependencies.rb (working copy)
@@ -98,17 +98,17 @@
# try and load the plugin code first
# can't use model, as there's nothing in the name to indicate that the
file is a 'model' file
# rather than a library or anything else.
- ['controller', 'helper'].each do |file_type|
+ Engines.file_types.each do |file_type|
# if we recognise this type
# (this regexp splits out the module/filename from any instances of
app/#{type}, so that
# modules are still respected.)
- if file_name =~ /^(.*app\/#{file_type}s\/)?(.*_#{file_type})(\.rb)?$/
+ if file_name =~
/^(.*app\/#{file_type.pluralize}\/)?(.*_#{file_type})(\.rb)?$/
base_name = $2
# ... go through the plugins from first started to last, so that
# code with a high precedence (started later) will override lower
precedence
# implementations
Rails.plugins.each do |plugin|
- plugin_file_name = File.expand_path(File.join(plugin.root, 'app',
"#{file_type}s", base_name))
+ plugin_file_name = File.expand_path(File.join(plugin.root, 'app',
file_type.pluralize, base_name))
logger.debug("checking plugin '#{plugin.name}' for '#{base_name}'")
if File.file?("#{plugin_file_name}.rb")
logger.debug("==> loading from plugin '#{plugin.name}'")
@@ -123,7 +123,7 @@
logger.debug("loading from application disabled.")
else
# Ensure we are only loading from the /app directory at this point
- app_file_name = File.join(RAILS_ROOT, 'app', "#{file_type}s",
"#{base_name}")
+ app_file_name = File.join(RAILS_ROOT, 'app', file_type.pluralize,
"#{base_name}")
if File.file?("#{app_file_name}.rb")
logger.debug("loading from application: #{base_name}")
file_loaded = true if
require_or_load_without_engine_additions(app_file_name, const_path)
@@ -140,4 +140,4 @@
end
end
-::Dependencies.send(:include, Engines::RailsExtensions::Dependencies)
\ No newline at end of file
+::Dependencies.send(:include, Engines::RailsExtensions::Dependencies)
Index: engines.rb
===================================================================
--- engines.rb (revision 615)
+++ engines.rb (working copy)
@@ -84,6 +84,10 @@
# A reference to the current Rails::Initializer instance
mattr_accessor :rails_initializer
+ # The types of files that can be auto-loaded. Classes are expected to end
+ # with this name and files should end with an underscore and this name
+ mattr_accessor :file_types
+ self.file_types = ['controller', 'helper']
#--
# These attributes control the behaviour of the engines extensions
@@ -320,4 +324,4 @@
end
end
end
-end
\ No newline at end of file
+end
_______________________________________________
Engine-Developers mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org