Class: Menudo::MenuBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/menudo/menu_builder.rb

Instance Method Summary collapse

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, options)
  @context = context
  @object = object
  @options = 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 authorize_items(object, options = {})
  can = options[:can].presence || :read
  options[: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

#buildObject

def initialize(collection, template, options)

@collection = collection
@options = 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|
      (: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 = @options, &block)
  return unless authorize_items(object, options)

  path = options[:path]
  icon = options[:icon]
  key = ActiveSupport::Inflector.parameterize(object.to_s, separator: '_')
  label = options[:label] || t("controllers.#{key.pluralize}.other")
  li_options = {}
  a_options = {}
  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)
    li_options = { class: 'active' } if item_active
    path = "##{key}"
    a_options[:'data-toggle'] = 'collapse'
  else
    li_options = { class: 'active' } if controller_name == options[:controller]
  end
   :li, li_options do
    concat(link_to(path || '#', a_options) do
      concat((:em, '', class: icon)) if icon.present?
      concat((: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, options = {})
  return unless authorize_items(object, options)
  @childs << { object: object, options: options }
end