【问题标题】:Where is the method 'is_admin?' defined in my Rails app?方法“is_admin”在哪里?在我的 Rails 应用程序中定义?
【发布时间】:2015-02-15 09:52:43
【问题描述】:

我有一些代码调用

current_user.is_admin?

代码运行良好,但我不知道is_admin? 方法的定义位置。我正在使用DeviseCanCanrole_model,但该方法不在这些项目的任何源代码中,或者在我的...

我还尝试通过在 Rails 控制台中执行此操作来查找 owner of the method

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

但这并没有多大帮助......

【问题讨论】:

  • 尝试输入current_user.method(:is_admin?).source_location
  • @BroiSatse - 谢谢!做到了...我会回答...

标签: ruby-on-rails ruby


【解决方案1】:

我通过以下方式获得了源位置:

current_user.method(:is_admin?).source_location

(感谢@BroiSatse)

这将我指向 role_model 中的这个文件: https://github.com/martinrehfeld/role_model/blob/master/lib/role_model/class_methods.rb

原来 role_model 会根据分配的角色动态创建方法——这就是它没有出现在源代码中的原因...

来自 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2015-08-26
    • 2014-04-08
    • 2012-06-27
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多