Listing Actions


#1

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…


#2

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’…


#3

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


#4

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


#5

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


#6

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


#7

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.

#8

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:


#9

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.
removed_email_address@domain.invalid 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • J *
    ~

#10

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


#11

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…


#12

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).