Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a method like current_user for devise's current resources

I want to create a method like current_user for devise's current resources. Suppose I have two resources like User and Admin and devise is associated with both. So as usual it dynamically creates it's default methods like current_user and current_admin.

It creates it by defining like this in file lib/devise/controllers/helpers.rb:

def current_#{mapping}

How can I add a new method like this to it's dynamic methods.

I want to implement it with devise methods, so that when devise initializes then my method is also initialize with same mapping name.

like image 624
Shivam Bajpai Avatar asked Dec 05 '25 14:12

Shivam Bajpai


1 Answers

Copy this code into your application controller. and your problem will solved.

Devise.mappings.each do |mapping, obj|
  define_method "current_#{mapping}_email" do
    eval("current_#{mapping}.email if current_#{mapping}")
end
helper_method "current_#{mapping}_email"

On view page/controller used according to your resource name like if you have resource_name as user then its 'current_user_email' or if have admin then 'current_admin_email'

like image 153
Sanket Avatar answered Dec 08 '25 08:12

Sanket



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!