Rails:辅助重构
我有一个我觉得很可笑的助手,但我一直想不出改进的方法。这是有问题的帮助程序:
# Shows Admin Menu Button
def admin_toggle_button
if user_signed_in? && ( current_user.has_role?(:admin) || ( @collection && can?(:curate,@collection) ) )
if session[:admin_menu] == :on
link_to( 'Admin Tools', edit_shared_path(:admin_menu => :off), :remote=>true, :class => 'selected', :id => 'admin_toggle_button', :title => 'Hide Admin Menu' )
else
link_to( 'Admin Tools', edit_shared_path(:admin_menu => :on), :remote=>true, :id => 'admin_toggle_button', :title => 'Show Admin Menu' )
end
end
end
在我的应用程序的菜单栏中,我调用 admin_toggle_button
,该帮助程序确定该按钮是否应该存在及其状态。
要显示管理菜单按钮,需要有登录用户,并且该用户需要是管理员,或者该用户需要查看允许他管理(编辑)的集合。
我的问题是:像这样的辅助方法是正常的吗——即你是否发现你时不时地需要如此复杂的方法——或者我是否遗漏了一些东西?您能建议一种改进此方法的方法吗?
I have a helper which I feel is ridiculous, but I haven't been able to think of a way to improve it. Here's the helper in question:
# Shows Admin Menu Button
def admin_toggle_button
if user_signed_in? && ( current_user.has_role?(:admin) || ( @collection && can?(:curate,@collection) ) )
if session[:admin_menu] == :on
link_to( 'Admin Tools', edit_shared_path(:admin_menu => :off), :remote=>true, :class => 'selected', :id => 'admin_toggle_button', :title => 'Hide Admin Menu' )
else
link_to( 'Admin Tools', edit_shared_path(:admin_menu => :on), :remote=>true, :id => 'admin_toggle_button', :title => 'Show Admin Menu' )
end
end
end
In my application's menubar I call admin_toggle_button
and this helper determines whether or not that button should be present and what its state is.
For the admin menu button to be present there needs to be a logged-in user, and that user needs to be an administrator OR that user needs to be viewing a collection which he is allowed to curate (edit).
My question is: Are helper methods like this normal -- ie do you find you need methods this complex from time to time -- or am I missing something? Can you suggest a way to improve this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为第一个条件创建一个方法,以便可以在其他地方重用它。
但是,是的,我想找到这样的方法是正常的,特别是如果您只使用链接的条件。如果您将它用于代码的其他部分,那么最好有一个助手。
You can make a method for the first condition so you can reuse it elsewhere.
But yeah, it's normal I guess to find methods like especially if you're only using the condition for the link. If you're using it for other parts of your code, it'd be nice to have a helper for it.
该方法并不是非常复杂。最复杂的部分是访问检查,即使这样也不算太糟糕。需要帮助者的原因之一是让类似的事情远离你的视野,这样你就不会做任何错事。
两个
link_to
变体之间的重复可能需要注意。您可以对其进行一些重构,但是:这种方法突出显示了两个可能的
link_to
调用之间的差异。如果session[:admin_menu] != :on
更常见,那么您可能想要反转逻辑以启动opts
和admin_menu
“隐藏管理菜单”设置,然后根据需要将其调整为!= :on
大小写。That method isn't terribly complex. The most complicated part is the access checking and even that isn't too bad. One of the reasons for helpers is to keep stuff like that out of your views so you're not doing anything wrong.
The repetition between the two
link_to
variants might need some attention. You could restructure it a little but:This approach highlights the differences between the two possible
link_to
calls. Ifsession[:admin_menu] != :on
is more common then maybe you'd want to reverse the logic to startopts
andadmin_menu
with the "Hide Admin Menu" settings and then adjust them to the!= :on
case as needed.