Module: Ekylibre::Navigation

Defined in:
lib/ekylibre/navigation.rb,
lib/ekylibre/navigation/dsl.rb,
lib/ekylibre/navigation/node.rb,
lib/ekylibre/navigation/page.rb,
lib/ekylibre/navigation/tree.rb

Defined Under Namespace

Classes: DSL, Node, Page, ReverseImpossible, Tree

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.treeObject (readonly)

Returns the value of attribute tree


12
13
14
# File 'lib/ekylibre/navigation.rb', line 12

def tree
  @tree
end

Class Method Details

.config_fileObject


14
15
16
# File 'lib/ekylibre/navigation.rb', line 14

def config_file
  Rails.root.join('config', 'navigation.xml')
end

.exec_dsl(&block) ⇒ Object

Execute code with DSL


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

def exec_dsl(&block)
  DSL.run!(@tree, &block)
end

.group_human_name(part, group) ⇒ Object

Returns the human name of a group


94
95
96
# File 'lib/ekylibre/navigation.rb', line 94

def group_human_name(part, group)
  ::I18n.translate(('menus.' + [part, group].join('.')).to_sym, default: ["menus.#{group}".to_sym, "labels.menus.#{group}".to_sym, "labels.#{group}".to_sym])
end

.group_of(controller, action) ⇒ Object

Returns the name of the group corresponding to an URL


60
61
62
# File 'lib/ekylibre/navigation.rb', line 60

def group_of(controller, action)
  reverse(controller, action)[:group]
end

.groups_in(part) ⇒ Object

Returns the group hash corresponding to the part


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

def groups_in(part)
  @tree[part] || {}
end

.groups_of(controller, action) ⇒ Object

Returns the group hash corresponding to the current part


70
71
72
# File 'lib/ekylibre/navigation.rb', line 70

def groups_of(controller, action)
  @tree[part_of(controller, action)] || {}
end

.human_name(*args) ⇒ Object

Returns a human name corresponding to the arguments 1: part 2: group 3: item


83
84
85
86
# File 'lib/ekylibre/navigation.rb', line 83

def human_name(*args)
  levels = [nil, :part, :group, :item]
  send("#{levels[args.count]}_human_name", *args)
end

.icon(*args) ⇒ Object

Returns icon name


105
106
107
108
109
110
# File 'lib/ekylibre/navigation.rb', line 105

def icon(*args)
  if node = @tree.get(*args)
    return node.icon
  end
  'question-sign'
end

.item_human_name(part, group, item) ⇒ Object

Returns the human name of an item


99
100
101
102
# File 'lib/ekylibre/navigation.rb', line 99

def item_human_name(part, group, item)
  p = hash[part][group][item].first
  ::I18n.translate(('menus.' + [part, group, item].join('.')).to_sym, default: ["menus.#{item}".to_sym, "labels.menus.#{item}".to_sym, "actions.#{p[:controller][1..-1]}.#{p[:action]}".to_sym, "labels.#{item}".to_sym])
end

.item_of(controller, action) ⇒ Object

Returns the name of the item corresponding to an URL


65
66
67
# File 'lib/ekylibre/navigation.rb', line 65

def item_of(controller, action)
  reverse(controller, action)[:item]
end

.load_file(file) ⇒ Object


18
19
20
21
22
23
24
25
26
27
# File 'lib/ekylibre/navigation.rb', line 18

def load_file(file)
  # Parse XML file
  f = File.open(file)
  doc = Nokogiri::XML(f) do |config|
    config.strict.nonet.noblanks
  end
  f.close

  @tree = Tree.load_file(file, :navigation, %i[part group item])
end

.part_human_name(part) ⇒ Object

Returns the human name of a group


89
90
91
# File 'lib/ekylibre/navigation.rb', line 89

def part_human_name(part)
  ::I18n.translate("menus.#{part}".to_sym, default: ["labels.menus.#{part}".to_sym, "labels.#{part}".to_sym])
end

.part_of(controller, action) ⇒ Object

Returns the name of the part corresponding to an URL


52
53
54
55
56
57
# File 'lib/ekylibre/navigation.rb', line 52

def part_of(controller, action)
  if r = reverse(controller, action)
    return r[:part]
  end
  nil
end

.partsObject


29
30
31
# File 'lib/ekylibre/navigation.rb', line 29

def parts
  @tree.children
end

.reverse(controller, action) ⇒ Object

Returns the path (part, group, item) from an action


39
40
41
# File 'lib/ekylibre/navigation.rb', line 39

def reverse(controller, action)
  @tree.reverse(controller, action)
end

.reverse!(controller, action) ⇒ Object

Returns the path (part, group, item) from an action


44
45
46
47
48
49
# File 'lib/ekylibre/navigation.rb', line 44

def reverse!(controller, action)
  unless path = reverse(controller, action)
    raise ReverseImpossible, "Cannot reverse action #{controller}##{action}"
  end
  path
end