Where is is_admin method located? defined in my Rails application?

I have a code that calls

current_user.is_admin? 

The code works fine, but I canโ€™t determine where the is_admin? method is is_admin? . I use Devise , CanCan , role_model , but the method is not in any of the source code of these projects or in my ...

I also tried to find the owner of the method by doing this in the Rails console:

 current_user.method(:is_admin?).owner => #<Module:0x00000105253c98> 

But it doesnโ€™t help much ...

+5
source share
1 answer

I got the original location by doing:

 current_user.method(:is_admin?).source_location 

(Thanks to this @BroiSatse)

This pointed me to this file in role_model: https://github.com/martinrehfeld/role_model/blob/master/lib/role_model/class_methods.rb

It turns out that role_model dynamically creates methods based on the assigned roles, so for some reason it does not appear in the source ...

From class_methods.rb :

 # Defines dynamic queries for :role # #is_<:role>? # #<:role>? # # Defines new methods which call #is?(:role) def define_dynamic_queries(roles) dynamic_module = Module.new do roles.each do |role| ["#{role}?".to_sym, "is_#{role}?".to_sym].each do |method| define_method(method) { is? role } end end end include dynamic_module end 
+1
source

Source: https://habr.com/ru/post/1209285/


All Articles