Module: Ekylibre::Navigation

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

Defined Under Namespace

Classes: DSL, Node, Page, ReverseImpossible, Tree

Class Method Summary collapse

Class Method Details

.config_fileObject


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

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

.exec_dsl(&block) ⇒ Object

Execute code with DSL


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

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

.group_human_name(part, group) ⇒ Object

Returns the human name of a group


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

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


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

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

.groups_in(part) ⇒ Object

Returns the group hash corresponding to the part


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

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

.groups_of(controller, action) ⇒ Object

Returns the group hash corresponding to the current part


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

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


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

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

.icon(*args) ⇒ Object

Returns icon name


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

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


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

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


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

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

.load_file(file) ⇒ Object


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

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, [:part, :group, :item])
end

.part_human_name(part) ⇒ Object

Returns the human name of a group


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

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


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

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

.partsObject


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

def parts
  @tree.children
end

.reverse(controller, action) ⇒ Object

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


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

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

.reverse!(controller, action) ⇒ Object

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


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

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