此代码是为ActiveAdmin编写的,但实际上并不重要。 我该如何优化呢?谢谢。
module ActiveAdminSimpleMenu
# in options can take:
# menu_priority:integer
# menu_parent:string
# permitted_params:array for some additions to main_fields permitted params
def simple_menu_for(klass, options = {})
ActiveAdmin.register klass do
menu_options = {}
menu_options[:priority] = options[:menu_priority] if options[:menu_priority]
menu_options[:parent] = options[:menu_parent] if options[:menu_parent]
menu menu_options unless menu_options.empty?
permit_params(*(klass.main_fields + (options[:permitted_params] ? options[:permitted_params] : [])))
...
答案 0 :(得分:2)
我认为"优化"你的意思是重构。你可以
Hash#slice
(Rails方法)构建menu_options
if
和any?
||
来测试options[:permitted_params]
所有在一起:
def simple_menu_for(klass, options = {})
ActiveAdmin.register klass do
menu_options = options.slice(:menu_priority, :menu_parent)
menu menu_options if menu_options.any?
permit_params(*(klass.main_fields + (options[:permitted_params] || [])))
...