Controller methods


#1

Hi,

I want to be a little clever with my views and basically inspect a
controller to autodiscover it’s methods and then create a link for each
one.

Now I’ve given a try to MyController.new.methods, but I get a problem
with the dependency on ApplicationController (in the console), is there
a way to do this? I assume there is a way to do this.

What I want is

def available
@available = ReportsController.actions - [:index, :list]
end

or something similar

Kev


#2

There are two methods that do what you’re looking for.

Object.methods returns a list of all the class methods,
Module.instance_methods returns a list of all the instance methods.

What you’re looking for are the instance methods of your controller,
without anything that belong to the superclass (ApplicationController).

As such, try this (you might be able to stick it in
ApplicationController and still work, haven’t actually tried it):

def actions

start with all my methods

self.class.instance_methods -
# remove my superclass’s methods
self.class.superclass.instance_methods -
# manually remove anything else I don’t want showing up
non_action_methods
end

def non_action_methods

this method should be overridden by the concrete controllers,

and list anything you don’t want showing up.

Also good to list any methods of mixins, like a login generator

[‘index’, ‘list’]
end

Do note that the method names returned from non_action_methods should be
strings, not symbols.

  • Jamie