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.to_s, 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

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  [priority, name] <=> [other.priority, other.name]
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



48
49
50
51
52
53
54
55
56
57
# File 'lib/trestle/navigation/item.rb', line 48

def admin
  case options[:admin]
  when nil, false
    return
  when Symbol, String
    Trestle.lookup(options[:admin]) or raise ActionController::UrlGenerationError, "No admin found named #{options[:admin].inspect}"
  else
    options[:admin]
  end
end

#badgeObject



71
72
73
# File 'lib/trestle/navigation/item.rb', line 71

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

#badge?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/trestle/navigation/item.rb', line 67

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



75
76
77
# File 'lib/trestle/navigation/item.rb', line 75

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

#iconObject



63
64
65
# File 'lib/trestle/navigation/item.rb', line 63

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

#labelObject



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

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

#pathObject



38
39
40
41
42
43
44
45
46
# File 'lib/trestle/navigation/item.rb', line 38

def path
  if @path
    @path
  elsif admin = self.admin
    admin.path(options[:action])
  else
    "#"
  end
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)


79
80
81
82
83
84
85
86
87
# File 'lib/trestle/navigation/item.rb', line 79

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