# Use of self reflection to determine possible actions

**URL:** <https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443>\
**Category:** Rails\
**Created:** [December 17, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443 "2007-12-17T20:12:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 17, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/1 "2007-12-17T20:12:16Z")

</div>

Is there a way to use either Ruby or Rail’s self reflection to return  
a list of actions a given controller can respond to?  
I found the “methods” method, but it appears to return everything,  
including all of the super class methods. I want just the actions a  
given controller has defined. I am trying to implement a Roles Based  
Access framework and want to have dynamic links based upon what a user  
can do. It would be nice if the controllers can tell me what is  
possible, and then remove those things the user isn’t allowed to do.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 17, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/2 "2007-12-17T20:15:48Z")

</div>

On Dec 17, 2007 1:11 PM, Crispy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any suggestions?  
> [http://destiney.com/blog/rubyonrails-get-public-actions-from-a-controller](http://destiney.com/blog/rubyonrails-get-public-actions-from-a-controller)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 17, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/3 "2007-12-17T20:30:32Z")

</div>

Crispy :

> Is there a way to use either Ruby or Rail’s self reflection to return  
> a list of actions a given controller can respond to?

ActionController::Base#action\_methods and  
ActionController::Base.action\_methods

– Jean-François.

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 5:19am UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/4 "2007-12-28T05:19:03Z")

</div>

Sometimes this happen.

Where you want to know actions???

On Dec 27, 2007 11:32 PM, Crispy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Crispy :
> > 
> > > Is there a way to use either Ruby or Rail’s self reflection to return  
> > > a list of actions a given controller can respond to?
> > 
> > ActionController::Base#action\_methods and  
> > ActionController::Base.action\_methods
> > 
> > – Jean-FranÃ§ois.

–  
Everton J. Carpes  
Mobile: +55 53 9129.4593  
MSN: removed\_email\_address@domain.invalid  
UIN: 343716195  
Jabber: removed\_email\_address@domain.invalid

“If art interprets our dreams, the computer executes them in the guise  
of  
programs!” - Alan J. Perlis

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 2:32am UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/5 "2007-12-28T02:32:58Z")

</div>

Neither of the two seem to work.  
I get…

Evaluation failed. Reason(s):  
uninitialized constant  
ActionWebService::Dispatcher::ActionController::Base

On Dec 17, 11:29 am, “Jean-François Trân” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/6 "2007-12-28T18:10:59Z")

</div>

Everton,  
I wasn’t sure I understood your question.  
What I’m trying to do is implement a Role Based Access Control (RBAC)  
framework.  
Basically, a user gets assigned one or more Roles. A Role has a  
collection of Rights. A Right is a Controller and Action within in  
the applicatoin.  
If a user has a given right, the authenticator will allow a redirect  
to that Controller and Action.  
In the administration section of the application, where Rights are  
defined and assigned to Roles, I wanted to provide a drop down list of  
all the possible Rights to select from. Ideally, the application  
itself should be able to tell me what Controllers and Actions it  
provides via self reflection.  
This would have been a snap in Smalltalk, but I can’t seem to get  
something that works in Ruby/Rails.  
If one does something like  
allowedControllers = ApplicationController.subclasses  
you only get the controller that was in context (presumably because  
the subclasses method uses ObjectSpace), and its only a string, not  
the actual class.  
Therefore, I can’t loop through the collection and do something like  
allowedActions = aController.action\_methods  
Therefore, i’m a bit frustrated and looking for those wiser than I to  
clue me in.

On Dec 27, 8:18 pm, “Everton J. Carpes” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/7 "2007-12-28T20:24:43Z")

</div>

The problem with calling ApplicationController.subclasses is that Rails  
does  
not loadclasses until they are invoked - so all you see is the current  
controller. To get a list of  
all available controllers, you need to explicitly load them all.  
Example:

(in your controller)  
def load\_all\_controllers  
require RAILS\_ROOT + “/app/controllers/application”

# Load all the controller files

controller\_files = Dir[RAILS\_ROOT +  
“/app/controllers/\*\*/\*\_controller.rb”]  
controller\_files.each do |file\_name|  
require file\_name  
end  
end

This will pull all of the controller definitions in, giving a complete  
list  
in ApplicationController.subclasses.

To use the list, you need to iterate over the entries:  
def iterate\_over\_controllers  
ApplicationController.subclasses.each do |controller\_name|  
controller\_name.constantize.action\_methods.each do |method|  
# do something with combination of controller\_name and method  
end  
end  
end

You may also want to take a look at the User engine (  
[http://agilewebdevelopment.com/plugins/user\_engine](http://agilewebdevelopment.com/plugins/user_engine) ) - even if Engines  
aren’t your thing, the ideas there could be useful. Note that the engine  
uses  
a rake task to sync available controller/action pairs rather than  
generating  
them on the fly.

Hope this helps…

–Matt

On Dec 28, 2007 12:10 PM, Crispy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the administration section of the application, where Rights are  
> the actual class.
> 
> > > Neither of the two seem to work.
> > > 
> > > > > Is there a way to use either Ruby or Rail’s self reflection to  
> > > > > Mobile: +55 53 9129.4593

–  
Matt J.  
removed\_email\_address@domain.invalid  
President/Technical Director, Acme Art Company ([acmeartco.org](http://acmeartco.org))

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/8 "2007-12-28T22:02:11Z")

</div>

On previous post i spoke about similar question:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/f6c1b3a546e7e240/40233a3286dfdccf?hl=en&lnk=gst&q="Admin+and+Dry"#40233a3286dfdccf](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/f6c1b3a546e7e240/40233a3286dfdccf?hl=en&lnk=gst&q=%22Admin+and+Dry%22#40233a3286dfdccf)

I have done the same thing that are you speaking about, but  
controllers/actions was created on a migration. I think that this should  
be  
moved to a rake task too.

I think that engines like User Engine have problems for me, and so, i  
create  
RBAC system myself. On my system, i have more than just  
controller/action or  
“permissions” models, i have tools\_categories that group actions  
behind  
some “tool”. Each permission is maded by relationships between  
tools\_categories and roles through functions (where functions is just  
C-R-U-D at now). The system “introspect” function of each action based  
on  
his name (ugly, but works). When you specify a permission you just say  
that  
some role have access to create and read, but not update or delete on a  
specific tool. This is very simple to use and configure and works fine  
for  
me.  
To help me, i just add some useful methods to user model, like  
canCreate?(action) or canUpdate?(action) and i just overwrite link\_to\*\*\*  
helpers to return “” if user has no access to relative access.

Note: actions like ‘‘new’’ should be classified as C (create), so links  
for  
new never will appear if the role has no permissions to create. This  
give  
more freedom to write my code, without many IFs on all views…

I can give you all this code, but it’s in portuguese until now, ☹ . I  
will  
translate all and move to engine in a few days. If you want to help me,  
i  
will be grateful.

On Dec 28, 2007 5:23 PM, Matt J. \< removed\_email\_address@domain.invalid\> wrote:

> controller\_files = Dir[RAILS\_ROOT +  
> def iterate\_over\_controllers  
> generating them on the fly.
> 
> > What I’m trying to do is implement a Role Based Access Control (RBAC)  
> > provides via self reflection.  
> > clue me in.
> > 
> > > On Dec 27, 2007 11:32 PM, Crispy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > > 
> > > > Evaluation failed. Reason(s):
> > > 
> > > Jabber: removed\_email\_address@domain.invalid
> > > 
> > > “If art interprets our dreams, the computer executes them in the guise  
> > > of  
> > > programs!” - Alan J. Perlis- Hide quoted text -
> > > 
> > > - Show quoted text -  
> > > [acmeartco.org](http://acmeartco.org))
> > 
> > > >

–  
Everton J. Carpes  
Mobile: +55 53 9129.4593  
MSN: removed\_email\_address@domain.invalid  
UIN: 343716195  
Jabber: removed\_email\_address@domain.invalid

“If art interprets our dreams, the computer executes them in the guise  
of  
programs!” - Alan J. Perlis

---

<div class="post-metadata">

**Author:** ![crispy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47774/32.png) [@crispy](https://www.ruby-forum.com/u/crispy)\
**Post date:** [December 28, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/use-of-self-reflection-to-determine-possible-actions/123443/9 "2007-12-28T22:14:30Z")

</div>

One smallest p.s.:

My approach was so non-intrusive, that all application was created  
before  
AC, and so i do the AC system and restrict all data under the system on  
a  
complex scope restriction, without any change on code that was made  
before  
the system.

Some practices on this system could be “questionable” (abusive use of  
with\_scope, link\_to\*\*\* that return blank string obviously generate some  
invalid or ugly html (tables and lists with blank tds/items, etc)), but  
the  
clean code and very fast work done pay this price.

On Dec 28, 2007 7:01 PM, Everton J. Carpes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> create RBAC system myself. On my system, i have more than just  
> helpers to return “” if user has no access to relative access.  
> On Dec 28, 2007 5:23 PM, Matt J. \< removed\_email\_address@domain.invalid\> wrote:
> 
> > that the engine uses
> > 
> > > Everton,  
> > > all the possible Rights to select from. Ideally, the application  
> > > allowedActions = aController.action\_methods
> > > 
> > > > Where you want to know actions???
> > > > 
> > > > > I get…  
> > > > > return  
> > > > > MSN: removed\_email\_address@domain.invalid
> > > > > 
> > > > > >
> 
> “If art interprets our dreams, the computer executes them in the guise of  
> programs!” - Alan J. Perlis

–  
Everton J. Carpes  
Mobile: +55 53 9129.4593  
MSN: removed\_email\_address@domain.invalid  
UIN: 343716195  
Jabber: removed\_email\_address@domain.invalid

“If art interprets our dreams, the computer executes them in the guise  
of  
programs!” - Alan J. Perlis
