Class: Trestle::Toolbar::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/toolbar/item.rb

Direct Known Subclasses

Button, Link

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, label, options = {}, &block) ⇒ Item

Returns a new instance of Item.


8
9
10
11
12
13
14
15
16
17
# File 'lib/trestle/toolbar/item.rb', line 8

def initialize(template, label, options={}, &block)
  @template = template
  @label, @options, @block = label, options

  @menu = Menu.new(template)
  @menu.build(&block) if block_given?

  @icon = options.delete(:icon)
  @style = options.delete(:style)
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label


4
5
6
# File 'lib/trestle/toolbar/item.rb', line 4

def label
  @label
end

Returns the value of attribute menu


4
5
6
# File 'lib/trestle/toolbar/item.rb', line 4

def menu
  @menu
end

Instance Method Details

#==(other) ⇒ Object


19
20
21
# File 'lib/trestle/toolbar/item.rb', line 19

def ==(other)
  to_s == other.to_s
end

#button_classesObject


48
49
50
51
52
53
# File 'lib/trestle/toolbar/item.rb', line 48

def button_classes
  classes = (@options[:class] || "").split(/\s/)
  classes.push(*button_style_classes)
  classes.push("has-icon") if @icon
  classes.uniq
end

#button_label(content, options) ⇒ Object


55
56
57
58
59
60
# File 'lib/trestle/toolbar/item.rb', line 55

def button_label(content, options)
  icon = icon(@icon) if @icon
  label = (:span, content, class: "btn-label")

  safe_join([icon, label].compact, " ")
end

#button_styleObject


62
63
64
# File 'lib/trestle/toolbar/item.rb', line 62

def button_style
  @style || "default"
end

#button_style_classesObject


66
67
68
# File 'lib/trestle/toolbar/item.rb', line 66

def button_style_classes
  ["btn", "btn-#{button_style}"]
end

#optionsObject


44
45
46
# File 'lib/trestle/toolbar/item.rb', line 44

def options
  @options.merge(class: button_classes)
end

#renderObject

Raises:

  • (NotImplementedError)

33
34
35
# File 'lib/trestle/toolbar/item.rb', line 33

def render
  raise NotImplementedError
end

#render_menuObject


37
38
39
40
41
42
# File 'lib/trestle/toolbar/item.rb', line 37

def render_menu
  [
    menu.render_toggle(class: button_style_classes),
    menu.render_items
  ]
end

#to_sObject


23
24
25
26
27
28
29
30
31
# File 'lib/trestle/toolbar/item.rb', line 23

def to_s
  if menu.items.any?
    (:div, class: "btn-group", role: "group") do
      safe_join([render, render_menu], "\n")
    end
  else
    render
  end
end