#self.included(base) 在 Ruby on Rails 的 Restful 身份验证中做了什么? [英] What does #self.included(base) do in Ruby on Rails' Restful Authentication?

查看:52
本文介绍了#self.included(base) 在 Ruby on Rails 的 Restful 身份验证中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我们会这样做

helper_method :current_user, :logged_in?, :authorized?

使这些控制器方法可用作视图中的辅助方法.但是在 Restful Authentication 的 lib/authenticated_system.rb 中,我看到:

to make these controller methods available for use as helper methods in views. But in Restful Authentication's lib/authenticated_system.rb, I see:

# Inclusion hook to make #current_user and #logged_in?
# available as ActionView helper methods.
def self.included(base)
  base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method
end

为什么要这样做而不是单行?此外,我没有看到 included 在任何地方被调用.

Why is it done this way instead of that single line? Also, I don't see included being called anywhere.

推荐答案

self.included 函数在包含模块时被调用.它允许在基础(包含模块的地方)的上下文中执行方法.

The self.included function is called when the module is included. It allows methods to be executed in the context of the base (where the module is included).

更多信息:ruby mixin教程.

这篇关于#self.included(base) 在 Ruby on Rails 的 Restful 身份验证中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆