I have a helper module ModelHelper. I want to use user_signed_in? method inside that helper module. But it shows error. How can I call this method inside helper file.
Method user_signed_in? defined in the Devise::Controllers::Helpers::ClassModule module. Long story short it just checks if scope authenticated in warden. So you can try to check it without Devise helpers
def #{mapping}_signed_in?
!!current_#{mapping}
end
def current_#{mapping}
#current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
end
I think that you can use current_user.present?
Anyway, rails helpers are very ugly and I advise to don't use it at all
Related
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.
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'
This works!
I added a custom method current_user_email as follows!
Add a file devise_ext.rb in config/initializers:
And in that file, add your custom methods in this way:
module Devise
module Controllers
# Those helpers are convenience methods added to ApplicationController.
module Helpers
def self.define_helpers(mapping) #:nodoc:
mapping = mapping.name
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
def #{mapping}_signed_in?
!!current_#{mapping}
end
def current_#{mapping}
#current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
end
def #{mapping}_session
current_#{mapping} && warden.session(:#{mapping})
end
def current_#{mapping}_email
#current_#{mapping}.email if #current_#{mapping}
end
METHODS
ActiveSupport.on_load(:action_controller) do
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "current_#{mapping}_email"
end
end
end
end
end
P.S. I don't think this is the best way to do this. But it works. I tried this in one of my applications! You can use this code till I find a better way to do this :)
Simple question.
I have a method in my ApplicationHelper that calls my SessionsHelper to load the current_user
i.e.
module ApplicationHelper
def some_helper_method
if current_user.respond_to? :some_method
#does stuff
end
end
end
module SessionsHelper
def current_user=(user)
#current_user = user
end
def current_user
#current_user ||= User.find(...)
end
This works fine in my running application. However when running from Rspect the ApplicationHelper method cannot find current_user method. In the running app I know the method is available by some rails automagic class loading. But not sure what the best way is to make this work in Rspec.
There are multiple ways to handle this issue, but let me give you some background first.
You can configure rails to tell it what kind of helpers you are going to expose to your controllers and within your views with include_all_helpers.
In the old days you had a call to helper :all in your ApplicationController.
So that's how those methods get exposed.
Back to your question:
Solution numero uno: helper.stub(current_user: build(:user))
Solution numero due: helper.extend UserHandling
Prego
Devise provide several helper methods such as current_user. It uses ActiveSupport's on_load to lazily load these helpers into the controllers.
However this means that when I first starts Rails, my attempt to alias these helper methods will not work correctly. For example:
# in my ApplicationController
alias_method :devise_current_user, :current_user
def current_user
user = devise_current_user
if !user && !devise_controller?
foo
end
return user
end
This would cause "method not found error" when Rails is first initialized. I can start Rails first, then add these code, then it would work (correctly aliasing and overriding the method).
How to properly do my aliasing/overriding for these on_load helpers?
I use MongoDB as a database in my Rails application with MongoID gem. I want to call the helper method from the model within after_create callback method. How is it possible?My model code is:
class Department
include ApplicationHelper
after_create :create_news
private
def create_news
#user = ApplicationHelper.get_current_users
end
end
And my helper code is:
module ApplicationHelper
def get_current_users
current_user
end
end
When I create new department then following error occur.
undefined method `get_current_users' for ApplicationHelper:Module
How to remove error? Thanks in advance.
I also use mongoid and use this all the time. Shouldn't be unique to mongoid though.
ApplicationController.helpers.my_helper_method
If you want a helper method that you can use in your views to return the current user, you can do so in your ApplicationController, something like this for example:
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
Then you can use this in any view.
If you want some arbitrary method in a model to know what user it's dealing with, pass #current_user in as an argument to the method when you call it in your controller.
Your code seems incomplete so I can't really see what you're trying to accomplish, but this is pretty standard practice.
Make sure the module file is named properly, meaning in your case application_helper.rb and it's located on the helpers library.
You can also try to include the helper in the ApplicationController (app/controller/application_controller.rb).
for the purpose of reuse i put some method on applciation_helper,now i want to invoke this method on a specific helper like CategoryHelper,
is i need do something else?
Application_helper.rb
def ad_materials(a)
do sth
end
CategoryHelper.rb
ad_materials("dd")#this method define on application_helper,but it didnt' work
is this a commany way to use common method put them in application_helper or any other recommend way,hope someone could give me a hand,thanks
i have re-edited the question,to make it clear,hope someone help again
If you call helper :all in your application_controller, then any helper will be available in any view.
Example:
app/helpers/application_helper.rb
module ApplicationHelper
def helper(object)
object.to_s
end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper :all
end
app/views/other_objects/show.html.erb
<%= helper(other_object) %>
Is this what you are asking?
It doesn't really matter where you put the helpers in terms of how your application finds them. But you do need to define and end your helpers such as
def ad_materials("dd")
do stuff here
end
If you're saying "how do I invoke a ApplicationHelper methods from CategoryHelper" I've ran into this problem. My less than elegant workaround was this:
module ReusableHelpers
def ad_materials(a)
do sth
end
end
module ApplicationHelper
...
include ReusableHelpers
...
end
module CategoryHelper
include ReusableHelpers
def show_pastries_only
ad_materials("cherry")
end
end