Ognjen Regoje bio photo

MY NAME IS
Ognjen Regoje
BUT YOU CAN CALL ME OGGY


I make things that run on the web (mostly).
More /ABOUT me.

me@ognjen.io Twitter LinkedIn Instagram Github

Getting all methods in rails

For controllers

PagesController.instance_methods - ActionController::Base.instance_methods - ApplicationController.instance_methods

For models

exclude = Regexp.union ['before_add', 'after_add', 'before_remove', 'after_remove']
(Page.methods(false) - ApplicationRecord.instance_methods - ActiveRecord::Base.methods(true)).reject{|x| x.match? exclude }

Page is the model.

The exclude is needed to remove all the callbacks for related associations.

Note that this also includes the named scopes, and I’ve not found a way to exclude them yet.

Other objects (such as services)

PagesService.methods(false) + PagesService.instance_methods

#rails #technical