[英]Ruby - How to access module's methods?
I'm installing a forum using the Forem gem. 我正在使用Forem gem安装论坛。 There's an option that allows avatar personalization, since it's possible to login with Facebook.
有一个选项允许头像个性化,因为可以使用Facebook登录。 You just specify your method in the User model and that's it.
您只需在用户模型中指定方法即可。
# Forem initializer
Forem.avatar_user_method = 'forem_avatar'
# User model
def forem_avatar
unless self.user_pic.empty?
self.user_pic
end
end
But I want a fallback on Gravatar for normal, non-facebook accounts. 但是我想让Gravatar退回普通的非Facebook帐户。 I've found the method on Forem and in theory, I need to call the
avatar_url
method: 我已经在Forem上找到了该方法,并且从理论上讲,我需要调用
avatar_url
方法:
# User model
def forem_avatar
unless self.user_pic.empty?
self.user_pic
else
Forem::PostsHelper.avatar_url self.email
end
end
However, Forem isn't an instance, but a module and I can't call it nor create a new instance. 但是,Forem不是实例,而是模块,我无法调用它也不能创建新实例。 The easy way is to copy the lines of that method, but that's not the point.
简单的方法是复制该方法的行,但这不是重点。 Is there a way to do it?
有办法吗?
Thanks 谢谢
Both answers are correct, but when I call the method either way, there's this undefined local variable or method 'request'
error, which is the last line of the original avatar_url
. 这两个答案都是正确的,但是当我以任何一种方式调用方法时,都会出现此
undefined local variable or method 'request'
错误,这是原始avatar_url
的最后一行。
Is there a way to globalize that object like in PHP? 有没有办法像在PHP中那样全球化该对象? Do I have to manually pass it that argument?
我是否必须手动传递该参数?
perhaps reopen the module like this: 也许像这样重新打开模块:
module Forem
module PostsHelper
module_function :avatar_url
end
end
then call Forem::PostsHelper.avatar_url
然后调用
Forem::PostsHelper.avatar_url
if avatar_url
call other module methods, you'll have to "open" them too via module_function
如果
avatar_url
调用其他模块方法,则也必须通过module_function
“打开”它们
or just include Forem::PostsHelper
in your class and use avatar_url
directly, without Forem::PostsHelper
namespace 或仅在您的类中
include Forem::PostsHelper
并直接使用avatar_url
,而无需Forem::PostsHelper
命名空间
If you want to be able to use those methods in the user class, include them and use 如果您希望能够在用户类中使用这些方法,请包括它们并使用
class User < ActiveRecord::Base
include Forem::PostsHelper
def forem_avatar
return user_pic if user_pic.present?
avatar_url email
end
end
Another way would be to set the Forem.avatar_user_method
dynamically since the Forem code checks it it exists before using it and defaults to avatar_url
if it does not. 另一种方法是动态设置
Forem.avatar_user_method
因为Forem代码会在使用前检查它是否存在,如果没有,默认为avatar_url
。
class User < ActiveRecord::Base
# This is run after both User.find and User.new
after_initialize :set_avatar_user_method
# Only set avatar_user_method when pic is present
def set_avatar_user_method
unless self.user_pic.empty?
Forem.avatar_user_method = 'forem_avatar'
end
end
def forem_avatar
self.user_pic
end
end
This way you dont pollute your model with unnecessary methods from Forem and don't monkey patch Forem itself. 这样,您就不会使用来自Forem的不必要方法来污染模型,也不会伪造Forem本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.