Ruby on rails 如何在类中正确地预编模块?
我有一个AccountController类,这个控制器类在应用程序内核中。我不想对内核进行更改,因此我将对其进行修补。 控制器有一个名为成功的\u身份验证的方法,我已经重写了该方法。在新方法(在我的模块中)内,此代码调用名为load\u favorite\u或\u index的新方法 我已经读到alias\u method\u chain现在已被弃用,不应使用。我正试图在AccountController之前预编我的模块。但是什么也没发生,我想我的预编码是不正确的,你能帮我吗?这是我的密码Ruby on rails 如何在类中正确地预编模块?,ruby-on-rails,ruby,metaprogramming,monkeypatching,Ruby On Rails,Ruby,Metaprogramming,Monkeypatching,我有一个AccountController类,这个控制器类在应用程序内核中。我不想对内核进行更改,因此我将对其进行修补。 控制器有一个名为成功的\u身份验证的方法,我已经重写了该方法。在新方法(在我的模块中)内,此代码调用名为load\u favorite\u或\u index的新方法 我已经读到alias\u method\u chain现在已被弃用,不应使用。我正试图在AccountController之前预编我的模块。但是什么也没发生,我想我的预编码是不正确的,你能帮我吗?这是我的密码 #
# frozen_string_literal: true
module RedmineKapDesign
module Patches
module AccountControllerPatch
def self.prepended(base) # :nodoc:
class << base
prepend InstanceMethods
end
end
module InstanceMethods
def successful_authentication(user)
logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
# Valid user
self.logged_user = user
logger.info "Setting.autologin? = #{Setting.autologin?}, params[:autologin] = #{params[:autologin]}"
# generate a key and set cookie if autologin
if params[:autologin] && Setting.autologin?
set_autologin_cookie(user)
end
call_hook(:controller_account_success_authentication_after, {:user => user})
load_favourite_page_or_index
#redirect_back_or_default my_page_path
end
def load_favourite_page_or_index
user = User.current
favourite_page_field = CustomField.where(name: ["Favourite page", "favourite page", "favorite page", "Favourite page", "Любимая страница", "любимая страница", "Избранная страница", "избранная страница"]).first
page_url = user.custom_values.where(custom_field_id: favourite_page_field.id).first.value
if page_url.empty?
redirect_back_or_default my_page_path
else
redirect_to(page_url)
end
end
def self.hello
puts "Hello"
end
end
end
end
end
#unless AccountController.included_modules.include?(RedmineKapDesign::Patches::AccountControllerPatch)
# AccountController.send(:prepend, RedmineKapDesign::Patches::AccountControllerPatch)
#end
AccountController.singleton_class.prepend(RedmineKapDesign::Patches::AccountControllerPatch)
#冻结的字符串文字:true
模块设计
模块补丁
模块AccountControllerPatch
def self.prepend(base)#:nodoc:
类用户})
加载\u收藏夹\u页面\u或\u索引
#重定向\返回\或\默认我的\页面\路径
终止
def加载\u收藏夹\u页面\u或\u索引
user=user.current
收藏夹页面字段=自定义字段。其中(名称:[“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”、“收藏夹页面”))。首先
page\u url=user.custom\u value.where(custom\u field\u id:favorite\u page\u field.id).first.value
如果page_url.empty?
重定向\返回\或\默认我的\页面\路径
其他的
重定向到(页面url)
终止
终止
你好
“你好”
终止
终止
终止
终止
终止
#除非AccountController.include\u模块.include?(RedmineKapDesign::Patches::AccountControllerPatch)
#发送(:prepend,RedmineKapDesign::Patches::AccountControllerPatch)
#结束
AccountController.singleton_class.prepend(RedmineKapDesign::Patches::AccountControllerPatch)
不需要将实例方法分离到单独的模块中。您可以将它们放在AccountControllerPatch中,并预先编写您正在使用它修补的任何类
module RedmineKapDesign
module Patches
module AccountControllerPatch
def successful_authentication(user)
logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
# Valid user
self.logged_user = user
logger.info "Setting.autologin? = #{Setting.autologin?}, params[:autologin] = #{params[:autologin]}"
# generate a key and set cookie if autologin
if params[:autologin] && Setting.autologin?
set_autologin_cookie(user)
end
call_hook(:controller_account_success_authentication_after, {:user => user})
load_favourite_page_or_index
#redirect_back_or_default my_page_path
end
def load_favourite_page_or_index
user = User.current
favourite_page_field = CustomField.where(name: ["Favourite page", "favourite page", "favorite page", "Favourite page", "Любимая страница", "любимая страница", "Избранная страница", "избранная страница"]).first
page_url = user.custom_values.where(custom_field_id: favourite_page_field.id).first.value
if page_url.empty?
redirect_back_or_default my_page_path
else
redirect_to(page_url)
end
end
def self.hello
puts "Hello"
end
end
end
end
只有在使用模块混合模式时,类方法才真正需要使用单独的模块。使用单独InstanceMethods模块的框架内代码可用于组织目的。但在一个简单的monkeypatch中,它只会造成更多的混乱
# config/initializers/my_monkey_patch.rb
# You should explicity require classes in initializers
require Rails.root.join('path', 'to', 'monkeypatch')
require Rails.root.join('path', 'to', 'target')
::AccountController.prepend RedmineKapDesign::Patches::AccountControllerPatch