controller plugin?

Do I understand correctly that you can't write controller plugins?

Of course "can't" is a big word, what I'm really wondering is what the
major stumbling blocks to doing so are?

Am I missing something or wouldn't this be extremely useful?

Thanks!
Tim

googling
  rails controller plugin

got me here http://wiki.rubyonrails.org/rails/pages/Controller+Extensions
in short order.

Plugins are just code.

I was reading this article:
http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to-rails-plugins-part-i

to quote:

"Controllers: You can't directly write a controller plugin, but you
can write a generator that copies a controller to your app/controllers
directory. Intermediate difficulty."

???

Tim

Outside of me being unable to really understand what “controller plugin” means in the quote, the article is from over a year ago.

If you’re trying to add macros or what not to controllers, you simply do:

ActionController::Base.send(:include, MyModuleOfMethods)

in your plugin/init.rb

What are you trying to do tmac?

Jason

I think that's pretty much the answer I was hoping for Jason! So the
article is wrong and yes you can write controller plugins... thought
that was odd!

I have a site/project where I use a controller base class that handles
all the standard REST methods (new, show, edit, etc...) in a fairly
generic way. It would certainly take some work but I thought it might
be nice to share it as a plugin... at least for my own projects, and
others if there is any interest.

I have looked a bit but haven't seen anything like it. Is there a
plugin that already does something similar?

Thanks a ton Jason!
Tim

Hi Tim

Have you taken a lookat activescaffold ?
Imanot sure towhat extentisit RESTful...

I think that's pretty much the answer I was hoping for Jason! So the
article is wrong and yes you can write controller plugins... thought
that was odd!

Yes and no. What the article is saying is that you can't have
foo_controller in your plugin, install your plugin and then try to
access http://myapp.com/foo
I think that there is stuff afoot so that you can specify the paths
searched for views & controllers, but out of the box it won't work.
What you can of course do is a generator, provide modules for
controllers or extend ActionController with macros and so on.

Fred

>
> I think that's pretty much the answer I was hoping for Jason! So the
> article is wrong and yes you can write controller plugins... thought
> that was odd!

Yes and no. What the article is saying is that you can't have
foo_controller in your plugin, install your plugin and then try to
access http://myapp.com/foo
I think that there is stuff afoot so that you can specify the paths
searched for views & controllers, but out of the box it won't work.
What you can of course do is a generator, provide modules for
controllers or extend ActionController with macros and so on.

Hmmm, looks like you already can. Try adding your load paths to:

* MyController.template_root
* ActionController::Routing.controller_paths
* Dependencies.load_paths

Demo:

$ cat vendor/plugins/test_plugin/init.rb
config.after_initialize do
  path = File.expand_path('app/controllers', File.dirname(__FILE__))
  ActionController::Routing.controller_paths << path
  Dependencies.load_paths << path
end

$ cat vendor/plugins/test_plugin/app/controllers/test_plugin_controller.rb
class TestPluginController < ApplicationController
  self.template_root = File.expand_path('../views', File.dirname(__FILE__))
  def test
  end
end

$ cat vendor/plugins/test_plugin/app/views/test_plugin/test.rhtml
hi

$ script/console
Loading development environment.

app.get('/test_plugin/test'); puts app.response.body

hi
=> nil

Thanks for setting me down the right path here guys... It appears to
be relatively straight forward.

CHH - I'm only vaguely familiar with activescaffold ... I was really
trying to avoid a generator ... unless I'm misunderstanding they
typically generate a lot of very similar code over and over ... this
is for more than just admin stuff too:-)

I really just want to be able to inherit functionality into my
controllers.

Tim