我正在尝试重构我的 Rails 助手并将面包屑和导航菜单逻辑移动到单独的类中。但是在这些类中,我无权访问params
、cookies
hashes 等。我认为在不同的类之间传递参数是一个坏主意。我怎样才能避免这种情况?
例如我有:
module NavigationHelper
def nav_item(name, path, inactive = false)
NavItem.new(params, name, path, inactive ).render
end
class NavItem
include ActionView::Helpers
include Haml::Helpers
def initialize(params, name, path, inactive )
init_haml_helpers
@params = params
@name = name
@path = path
@inactive = inactive
end
def render
capture_haml do
haml_tag :li, item_class do
haml_concat link_to @name, @path
end
end
end
def item_class
klass = {class: 'active'} if active?
klass = {class: 'inactive'} if @inactive
klass
end
# Class of the current page
def active?
slug = @path.gsub /\//, ''
@params[:page] == slug || @params[:category] == slug
end
end
end