[英]How to convert an rails initializer into a module or concern
I have a set of authentication functions that I have right now in an initializer. 我现在在初始化器中拥有一组身份验证功能。 This means every time I need to make a change or debug I need to restart my development server to see the changes. 这意味着每次我需要进行更改或调试时,都需要重新启动开发服务器以查看更改。 I guess it's high time to put the code somewhere more dynamic. 我想是时候将代码放在更具动态性的地方了。 My thought is a module. 我的想法是一个模块。 Trouble is I use the functions both in a controller and in views. 问题是我在控制器和视图中都使用了这些功能。 I think I can get the functions included into the controller but they show as undefined methods in views. 我想我可以将功能包含在控制器中,但是它们在视图中显示为未定义的方法。 Can someone tell me the rails way to do this? 有人可以告诉我这样做的方法吗?
The auth mechanism is looking for a cookie set from an legacy PHP system to avoid a second login for the new code done in Rails. auth机制正在从旧版PHP系统中查找cookie集,以避免再次登录Rails中完成的新代码。
Everything works great as an initializer. 一切都可以很好地用作初始化程序。 To covert to a module I just wrapped the initializer code in module BtrSSO
, moved it to the lib folder, and then started including it in controllers. 为了隐蔽一个模块,我只是将初始化代码包装在module BtrSSO
,将其移动到lib文件夹,然后开始将其包含在控制器中。
config/initializers/btr_sso.rb config / initializers / btr_sso.rb
def logoutSSO
cookies.delete :BTR_SSO
session[:sso_options] = nil
end
def getSSOShop
return "" if session[:sso_options].nil?
session[:sso_options][:ediv]
end
def getSSOUserName
return "Not Logged In" if session[:sso_options].nil?
session[:sso_options][:sysname]
end
def getSSOIsManager
return false if session[:sso_options].nil?
session[:sso_options][:is_mgr]==true
end
def setSSO(employee)
#logger.debug employee.inspect
return if employee.nil?
#logger.debug "After employee nil check"
logger.info "setting cookie with #{employee.sysname} {employee.ediv}"
cookie_stuff = Hash.new
cookie_stuff[:sysname] = employee.sysname
cookie_stuff[:ediv] = employee.ediv
cookies[:BTR_SSO] = {
:value => cookie_stuff.to_json,
:expires => 1.week.from_now,
## required on production I think
# :domain => 'mydomain.com'
}
logger.debug employee.inspect
#should this be in the sessions controller?
session[:sso_options] = Hash.new() if session[:sso_options].nil?
session[:sso_options][:sysname] = employee.sysname
session[:sso_options][:ediv] = employee.ediv
session[:sso_options][:is_mgr] = employee.ismgr
logger.debug session[:sso_options].inspect
end
def authSSO
logger.debug session[:sso_options].inspect
session[:redirect_to] = request.path_info
redirect_to login_path(nexturl: request.path_info) if session[:sso_options].nil?
end
def is_worker_class
read_cookie
unless session[:sso_options].nil?
session[:sso_options][:sysname] == "worker"
else
false
end
end
def read_cookie
session[:sso_options] = JSON.parse cookies["BTR_SSO"] unless cookies["BTR_SSO"].nil?
end
The view calls getSSOUserName which is undefined. 该视图调用未定义的getSSOUserName。
I've tried "include BtrSSO" in the application_controller.rb and in the more direct controller but no difference. 我已经尝试在application_controller.rb和更直接的控制器中使用“ include BtrSSO”,但是没有区别。
The view is in views/layouts if that makes a difference. 如果这有所不同,则该视图位于视图/布局中。
!!!
%html
%head
%meta{:content => "width=device-width, initial-scale=1.0", :name => "viewport"}
%title= content_for?(:title) ? yield(:title) : "Intranet"
%meta{:content => content_for?(:description) ? yield(:description) : "Our Intranet", :name => "description"}
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
= yield(:head)
= yield(:javascripts)
%body{:class => "#{controller_name} #{action_name}"}
- unless getSSOUserName == "worker"
= render 'layouts/navigation'
- else
= render 'layouts/navigation_worker'
.uk-container.uk-container-center.buffer-top
/= render 'layouts/messages'
= show_flash_messages
= yield
= render 'layouts/footer'
Another example is in the menubar which tries to show the user name. 菜单栏中的另一个示例尝试显示用户名。 This is a fragment in view/layouts 这是视图/布局中的片段
Wrap it in module and move the entire file to app/helpers folder: 将其包装在模块中,然后将整个文件移至app / helpers文件夹:
# app/helpers/btr_sso.rb
module BtrSso # Name has to match the file name
# your definitions here
end
Helpers are automatically included in all view context, so explicit include
is not needed. 辅助程序自动包含在所有视图上下文中,因此不需要显式include
。 If you prefer to name your module BtrSSO
, use inflector acronyms. 如果您更愿意为模块BtrSSO
,请使用变形器首字母缩写词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.