Listing Actions

OK, call me crazy, but am I the first one to want to list out the
actions that are available @ runtime? Anyone else come across this
yet? Am I crazy? Well, yes, but…

Erich L. Timkar <erichtimkar@…> writes:

OK, call me crazy, but am I the first one to want to list out the
actions that are available runtime? Anyone else come across this
yet? Am I crazy? Well, yes, but…

How about in your view:

<%= debug (@controller.class.instance_methods -
ApplicationController.instance_methods) %>

Seems to work for me.

Interestingly, it showed me not only my action methods, but also some of
the
methods in my helpers. I guess I should make those ‘private’…

Kian wrote:

Erich L. Timkar <erichtimkar@…> writes:

OK, call me crazy, but am I the first one to want to list out the
actions that are available runtime? Anyone else come across this
yet? Am I crazy? Well, yes, but…

How about in your view:

<%= debug (@controller.class.instance_methods -
ApplicationController.instance_methods) %>

Seems to work for me.

Interestingly, it showed me not only my action methods, but also some of
the
methods in my helpers. I guess I should make those ‘private’…

Try this instead:

<%= debug (@controller.class.instance_methods -
@controller.class.hidden_actions) %>


Josh S.
http://blog.hasmanythrough.com

Erich L. Timkar wrote:

Thanks both of you. I’ve found another reason to love ruby. People
actually respond to posts with useful information :).

Yes, that’s why many of us love Ruby and Rails. By the way, I goofed a
bit. Try Foo.public_instance_methods - Foo.hidden actions. (Odds of
getting code right when composed in an email: 50%).


Josh S.
http://blog.hasmanythrough.com

The other reason I like rails/ruby is, based on your response, I tried
“public_instance_methods” and it worked. :slight_smile:

So, just to be a pest, anyway to determine dynamically the controllers
available @ runtime? I checked the ADWROR and Rails Recipes with no
luck.

Josh S. wrote:

Erich L. Timkar wrote:

Thanks both of you. I’ve found another reason to love ruby. People
actually respond to posts with useful information :).

Yes, that’s why many of us love Ruby and Rails. By the way, I goofed a
bit. Try Foo.public_instance_methods - Foo.hidden actions. (Odds of
getting code right when composed in an email: 50%).


Josh S.
http://blog.hasmanythrough.com

Thanks both of you. I’ve found another reason to love ruby. People
actually respond to posts with useful information :).

So, just to be a pest, anyway to determine dynamically the controllers
available @ runtime? I checked the ADWROR and Rails Recipes with no
luck.

Much trickier (to my knowledge anyways). If someone has a one-liner
that lists all the controllers, I’m excited to hear about it.

As I understand it, controllers aren’t loaded until they are called the
first time, so to find all the controllers, I

  1. Query the ObjectSpace object for all loaded Classes descending from
    ::ActionController::Base
  2. Query the Routes for all controllers named in routes
  3. Get the Rails::Configuration object and pull the controller_paths
    from there and then try to load controller objects inflected from the
    file names.

Ryan R. wrote:

So, just to be a pest, anyway to determine dynamically the controllers
available @ runtime? I checked the ADWROR and Rails Recipes with no
luck.

Much trickier (to my knowledge anyways). If someone has a one-liner
that lists all the controllers, I’m excited to hear about it.
Dir[File.join(File.expand_path(RAILS_ROOT), ‘app’, ‘controllers’, ‘**’,
‘*.rb’)].collect{|f| File.basename(f, ‘.rb’)}

Something along those lines, anyway :slight_smile:

You might find the code in here useful:

http://svn.rails-engines.org/user_engine/trunk/app/models/permission.rb

  • james

On 9 May 2006 01:40:14 -0000, Ryan R.
[email protected] wrote:

Dir[File.join(File.expand_path(RAILS_ROOT), ‘app’, ‘controllers’, ‘**’,
Posted with http://DevLists.com. Sign up and save your mailbox.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

  • J *
    ~

James A. wrote:

You might find the code in here useful:
http://svn.rails-engines.org/user_engine/trunk/app/models/permission.rb

Ah, I had thought about checking the controller path but having done
servlet development mostly, I’ve undergone years of operant conditioning
teaching me, “File Access BAD!!”

Thanks James

On Monday, May 08, 2006, at 9:48 PM, Alex Y. wrote:

Something along those lines, anyway :slight_smile:

Ah - but there can be subdirectories and controllers in components,
plugins, engines…

On Tuesday, May 09, 2006, at 9:49 AM, James A. wrote:

You might find the code in here useful:

http://svn.rails-engines.org/user_engine/trunk/app/models/permission.rb

  • james

Unless I’m horribly misreading that code, it still will miss any
controllers in components, etc.

One way to know if you’ve truly found all the available controllers is
if the method chosen finds the Rails::InfoController (which is present
in every 1.1+ (1.0+?) app).