Class: Menudo::MenuBuilder
- Inherits:
-
Object
- Object
- Menudo::MenuBuilder
- Defined in:
- lib/menudo/menu_builder.rb
Instance Method Summary collapse
- #authorize_items(object, options = {}) ⇒ Object
- #auto_set_controller(object) ⇒ Object
-
#build ⇒ Object
def initialize(collection, template, options) @collection = collection @options = options @template = template # @node = Tree::TreeNode.new(‘root’) end.
-
#build_item(object = @object, options = @options, &block) ⇒ Object
def treat_can(permissions) case permissions when end.
-
#concat(tag) ⇒ Object
def can?(*args) @current_ability.can?(*args) end.
-
#initialize(context, object, options) ⇒ MenuBuilder
constructor
A new instance of MenuBuilder.
- #item(object, options = {}) ⇒ Object
Constructor Details
#initialize(context, object, options) ⇒ MenuBuilder
Returns a new instance of MenuBuilder.
6 7 8 9 10 11 12 13 14 |
# File 'lib/menudo/menu_builder.rb', line 6 def initialize(context, object, ) @context = context @object = object @options = @childs = [] # @node = Tree::TreeNode.new('root') # @current_ability = ::Ability.new(current_user) # @request = request end |
Instance Method Details
#authorize_items(object, options = {}) ⇒ Object
73 74 75 76 77 |
# File 'lib/menudo/menu_builder.rb', line 73 def (object, = {}) can = [:can].presence || :read [:controller] ||= auto_set_controller(object) can?(object, can.to_sym) end |
#auto_set_controller(object) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/menudo/menu_builder.rb', line 79 def auto_set_controller(object) case object.class when Class object.to_s.demodulize.pluralize.underscore when Symbol object.to_s.pluralize.underscore end end |
#build ⇒ Object
def initialize(collection, template, options)
@collection = collection
@options =
@template = template
# @node = Tree::TreeNode.new('root')
end
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/menudo/menu_builder.rb', line 23 def build if @childs.present? build_item do |k| content_tag(:ul, class: "sidebar-nav sidebar-subnav collapse#{ k[:active] ? ' active' : '' }", id: k[:key]) do @childs.each do |i| concat(build_item(i[:object], i[:options]) || '') end end end end end |
#build_item(object = @object, options = @options, &block) ⇒ Object
def treat_can(permissions)
case permissions
when
end
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/menudo/menu_builder.rb', line 41 def build_item(object = @object, = @options, &block) return unless (object, ) path = [:path] icon = [:icon] key = ActiveSupport::Inflector.parameterize(object.to_s, separator: '_') label = [:label] || t("controllers.#{key.pluralize}.other") = {} = {} if block_given? controllers = @childs.map{ |i| i[:options][:controller] } # verify nil, and this is important, dont be passed as an option item_active = controllers.present? && controllers.include?(controller_name) = { class: 'active' } if item_active path = "##{key}" [:'data-toggle'] = 'collapse' else = { class: 'active' } if controller_name == [:controller] end content_tag :li, do concat(link_to(path || '#', ) do concat(content_tag(:em, '', class: icon)) if icon.present? concat(content_tag(:span, label)) end) concat(capture({ key: key, active: item_active }, &block)) if block_given? end end |
#concat(tag) ⇒ Object
def can?(*args)
@current_ability.can?(*args)
end
def cannot?(*args)
@current_ability.can?(*args)
end
95 96 97 98 |
# File 'lib/menudo/menu_builder.rb', line 95 def concat(tag) @context.safe_concat(tag) '' end |
#item(object, options = {}) ⇒ Object
68 69 70 71 |
# File 'lib/menudo/menu_builder.rb', line 68 def item(object, = {}) return unless (object, ) @childs << { object: object, options: } end |