class Tabit::Item

Attributes

active[RW]
children[RW]
name[RW]
options[RW]
type[RW]
url[RW]

Public Class Methods

new(name, url = nil, options = {}) click to toggle source
# File lib/tabit/item.rb, line 38
def initialize(name, url = nil, options = {})
  self.name = name
  self.url = url
  self.options = options

  self.active = self.options.delete(:active) || configuration.active_detect
  self.type = self.options.delete(:type) || :default
end

Public Instance Methods

active?() click to toggle source
# File lib/tabit/item.rb, line 85
def active?
  if template.is_active_link? url, active
    true
  else
    children.each do |child|
      next if child.is_a? String
      return true if child.active?
    end

    false
  end
end
add(name, url = nil, options = {}) { |adding| ... } click to toggle source
# File lib/tabit/item.rb, line 47
def add(name, url = nil, options = {})
  Item.new(name, url, options).tap do |adding|
    children.push adding
    yield adding if block_given?
  end
end
divider(name = nil, options = {}) click to toggle source
# File lib/tabit/item.rb, line 64
def divider(name = nil, options = {})
  append_class_to_options configuration.divider_class, options

  children.push template.content_tag(
    :li,
    name,
    options
  )
end
heading(name = nil, options = {}) click to toggle source
# File lib/tabit/item.rb, line 54
def heading(name = nil, options = {})
  append_class_to_options configuration.heading_class, options

  children.push template.content_tag(
    :li,
    name,
    options
  )
end
to_s() click to toggle source
# File lib/tabit/item.rb, line 74
def to_s
  configure_item_attrs

  case type
  when :dropdown
    generate_dropdown_item
  else
    generate_default_item
  end
end

Protected Instance Methods

append_class_to_options(value, target = nil) click to toggle source
# File lib/tabit/item.rb, line 167
def append_class_to_options(value, target = nil)
  target ||= options

  target[:class] ||= ""
  target[:class] << " #{value}"
  target[:class].strip!
end
append_toggle_to_options(value, target = nil) click to toggle source
# File lib/tabit/item.rb, line 175
def append_toggle_to_options(value, target = nil)
  target ||= options

  target[:data] ||= {}
  target[:data][:toggle] = value
end
configuration() click to toggle source
# File lib/tabit/item.rb, line 201
def configuration
  Tabit.configuration
end
configure_item_attrs() click to toggle source
# File lib/tabit/item.rb, line 115
def configure_item_attrs
  case configuration.active_scope
  when :outer
    append_class_to_options current_active_class, options[:outer]
  when :inner
    append_class_to_options current_active_class, options[:inner]
  end
end
current_active_class() click to toggle source
# File lib/tabit/item.rb, line 111
def current_active_class
  configuration.active_class if active?
end
generate_caret_label(label) click to toggle source
# File lib/tabit/item.rb, line 141
def generate_caret_label(label)
  [
    label,
    content_tag(
      configuration.caret_element,
      "",
      class: "caret"
    )
  ].join(" ").html_safe
end
generate_default_item() click to toggle source
# File lib/tabit/item.rb, line 135
def generate_default_item
  generate_simple_item(
    name
  )
end
generate_dropdown_item() click to toggle source
# File lib/tabit/item.rb, line 124
def generate_dropdown_item
  append_class_to_options "dropdown", options[:outer]
  append_class_to_options "dropdown-toggle", options[:inner]

  append_toggle_to_options "dropdown", options[:inner]

  generate_simple_item(
    generate_caret_label(name)
  )
end
generate_simple_item(label) click to toggle source
# File lib/tabit/item.rb, line 152
def generate_simple_item(label)
  template.content_tag(
    :li,
    [
      template.link_to(
        label,
        url,
        options[:inner]
      ),
      output
    ].compact.join.html_safe,
    options[:outer]
  )
end
list(clazz = nil) click to toggle source
# File lib/tabit/item.rb, line 193
def list(clazz = nil)
  template.content_tag(
    :ul,
    children.collect(&:to_s).join.html_safe,
    class: clazz
  )
end
output() click to toggle source
# File lib/tabit/item.rb, line 182
def output
  return if children.empty?

  case type
  when :dropdown
    list "dropdown-menu"
  else
    list
  end
end