Class: Trestle::Navigation::Item

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

Defined Under Namespace

Classes: Badge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path = nil, options = {}) ⇒ Item

Returns a new instance of Item.



6
7
8
# File 'lib/trestle/navigation/item.rb', line 6

def initialize(name, path=nil, options={})
  @name, @path, @options = name, path, options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  priority <=> other.priority
end

#==(other) ⇒ Object Also known as: eql?



10
11
12
# File 'lib/trestle/navigation/item.rb', line 10

def ==(other)
  other.is_a?(self.class) && name == other.name && path == other.path
end

#adminObject



38
39
40
# File 'lib/trestle/navigation/item.rb', line 38

def admin
  options[:admin]
end

#badgeObject



54
55
56
# File 'lib/trestle/navigation/item.rb', line 54

def badge
  Badge.new(options[:badge]) if badge?
end

#badge?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/trestle/navigation/item.rb', line 50

def badge?
  !!options[:badge]
end

#groupObject



34
35
36
# File 'lib/trestle/navigation/item.rb', line 34

def group
  options[:group] || NullGroup.new
end

#hashObject



15
16
17
# File 'lib/trestle/navigation/item.rb', line 15

def hash
  [name, path].hash
end

#html_optionsObject



58
59
60
# File 'lib/trestle/navigation/item.rb', line 58

def html_options
  options.except(:admin, :badge, :group, :icon, :if, :label, :priority, :unless)
end

#iconObject



46
47
48
# File 'lib/trestle/navigation/item.rb', line 46

def icon
  options[:icon] || Trestle.config.default_navigation_icon
end

#labelObject



42
43
44
# File 'lib/trestle/navigation/item.rb', line 42

def label
  options[:label] || I18n.t("admin.navigation.items.#{name}", default: name.to_s.titlecase)
end

#priorityObject



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

def priority
  case options[:priority]
  when :first
    -Float::INFINITY
  when :last
    Float::INFINITY
  else
    options[:priority] || 0
  end
end

#visible?(context) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/trestle/navigation/item.rb', line 62

def visible?(context)
  if options[:if]
    context.instance_exec(&options[:if])
  elsif options[:unless]
    !context.instance_exec(&options[:unless])
  else
    true
  end
end