semantic_navigation 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +17 -0
- data/README.md +1 -1
- data/lib/semantic_navigation/configuration.rb +27 -17
- data/lib/semantic_navigation/core/procs.rb +15 -0
- data/lib/semantic_navigation/core/render.rb +67 -0
- data/lib/semantic_navigation/helper_methods.rb +6 -5
- data/lib/semantic_navigation/item.rb +72 -30
- data/lib/semantic_navigation/version.rb +1 -1
- data/lib/tasks/templates/semantic_navigation.rb +52 -10
- metadata +7 -4
data/Changelog.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
### 0.0.2 / Unknown
|
2
|
+
|
3
|
+
* Definition of default render options (active_class and show_ids)
|
4
|
+
* Definition of the private render options to each menu item
|
5
|
+
* Definition of default option that says render submenu always or only then parent is active
|
6
|
+
* Raise error on undefined helper methods calls
|
7
|
+
* Raise error on undefined menu render calls
|
8
|
+
* Added helper method to render active name item
|
9
|
+
* Added helper method to render active item parent name
|
10
|
+
* Changed helper method to render menu
|
11
|
+
* Added prefixes for menu, item, name ids
|
12
|
+
* Added conditions for rendering (function show_if)
|
13
|
+
* Added breadcrumbs rendering
|
14
|
+
|
15
|
+
### 0.0.1 / December 23th, 2011
|
16
|
+
|
17
|
+
* Initial release [Sergey Gribovski]
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ The purpose of this gem is to generate simple and usefull navigation. You can de
|
|
8
8
|
|
9
9
|
Write the gem dependency in your Gemfile:
|
10
10
|
```
|
11
|
-
gem 'semantic_navigation'
|
11
|
+
gem 'semantic_navigation'
|
12
12
|
```
|
13
13
|
|
14
14
|
Make the install by bundler
|
@@ -3,34 +3,44 @@ require 'semantic_navigation/item'
|
|
3
3
|
module SemanticNavigation
|
4
4
|
class Configuration
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
instance = self.new
|
11
|
-
yield instance if block_given?
|
12
|
-
instance
|
13
|
-
end
|
6
|
+
def self.run
|
7
|
+
instance = self.new
|
8
|
+
yield instance if block_given?
|
9
|
+
instance
|
14
10
|
end
|
15
11
|
|
16
12
|
def initialize
|
17
|
-
@active_class = 'active'
|
18
|
-
@create_ids = true
|
19
|
-
@show_submenu = false
|
20
13
|
@menus = {}
|
21
14
|
end
|
22
15
|
|
23
16
|
def method_missing(name, *args)
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
name = name.to_s
|
18
|
+
if name.chomp('=') != name
|
19
|
+
SemanticNavigation::Item.set_default_option(name.chop,args[0])
|
20
|
+
else
|
21
|
+
menu = Item.new(name, args, nil)
|
22
|
+
@menus.merge!({name.to_sym => menu})
|
23
|
+
yield menu if block_given?
|
24
|
+
end
|
27
25
|
end
|
28
26
|
|
29
|
-
def render(
|
27
|
+
def render(name, command = :render)
|
30
28
|
if @menus[name.to_sym]
|
31
|
-
|
29
|
+
if command == :render
|
30
|
+
return @menus[name.to_sym].render
|
31
|
+
elsif command == :active_item_name
|
32
|
+
item = @menus[name.to_sym].find_active_item
|
33
|
+
!item.nil? ? item.name : ''
|
34
|
+
elsif command == :active_item_parent_name
|
35
|
+
item = @menus[name.to_sym].find_active_item
|
36
|
+
!item.nil? && !item.parent.nil? ? item.parent.name : ''
|
37
|
+
elsif command == :breadcrumb
|
38
|
+
return @menus[name.to_sym].render_breadcrumb
|
39
|
+
else
|
40
|
+
raise NoMethodError.new("Wrong menu render parameter:`#{command.to_s}`")
|
41
|
+
end
|
32
42
|
else
|
33
|
-
|
43
|
+
raise NoMethodError.new("No such menu name:`#{name}` check your #{Rails.root}/config/semantic_navigation.rb) file")
|
34
44
|
end
|
35
45
|
end
|
36
46
|
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module SemanticNavigation
|
2
|
+
module Core
|
3
|
+
module Render
|
4
|
+
|
5
|
+
def render
|
6
|
+
return nil if !render_item?
|
7
|
+
if @parent
|
8
|
+
sub = @active || show_submenu? ? render_submenu : nil
|
9
|
+
link = view_object.link_to @name, @url_options, :class => classes, :id => a_id
|
10
|
+
view_object.content_tag(:li, link + sub, :id => li_id, :class => classes)
|
11
|
+
else
|
12
|
+
render_submenu
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_as_active
|
17
|
+
@active = true
|
18
|
+
@parent.set_as_active if @parent
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_active_item
|
22
|
+
item = nil
|
23
|
+
if active?
|
24
|
+
item = self
|
25
|
+
elsif @active
|
26
|
+
@sub_items.each do |s|
|
27
|
+
buff = s.find_active_item
|
28
|
+
item = buff if !buff.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
item
|
32
|
+
end
|
33
|
+
|
34
|
+
def render_breadcrumb
|
35
|
+
breadcrumb = nil
|
36
|
+
if @active
|
37
|
+
if @name
|
38
|
+
link = view_object.link_to @name, @url_options, :id => a_id
|
39
|
+
breadcrumb = view_object.content_tag(:li, link + breadcrumb_name, :id => li_id) if @name
|
40
|
+
end
|
41
|
+
buff = @sub_items.map{|s| s.render_breadcrumb}.find{|s| !s.nil?}
|
42
|
+
if !breadcrumb.nil?
|
43
|
+
breadcrumb += buff if !buff.nil?
|
44
|
+
else
|
45
|
+
breadcrumb = buff
|
46
|
+
end
|
47
|
+
end
|
48
|
+
breadcrumb = view_object.content_tag(:ul, breadcrumb, :id => ul_id, :class => breadcrumb_classes) if !@parent
|
49
|
+
breadcrumb
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def render_submenu
|
55
|
+
if @sub_items.count > 0
|
56
|
+
sub = @sub_items.map{|s| s.render}.sum
|
57
|
+
view_object.content_tag(:ul, sub, :id => ul_id) if sub != 0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def active?
|
62
|
+
!@parent.nil? ? view_object.current_page?(@url_options) : false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module SemanticNavigation::HelperMethods
|
2
2
|
|
3
|
-
def method_missing(name)
|
4
|
-
|
5
|
-
if
|
6
|
-
|
3
|
+
def method_missing(name, command = :render)
|
4
|
+
name = name.to_s
|
5
|
+
if name[0..6] == 'render_'
|
6
|
+
SemanticNavigation::Item.set_default_option('view_object',self)
|
7
|
+
semantic_navigation_config.render(name[7..-1], command)
|
7
8
|
else
|
8
|
-
|
9
|
+
raise NoMethodError.new("NoMethodError:#{name}")
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
@@ -1,15 +1,42 @@
|
|
1
|
+
require 'semantic_navigation/core/render'
|
2
|
+
require 'semantic_navigation/core/procs'
|
1
3
|
module SemanticNavigation
|
2
4
|
class Item
|
3
|
-
|
5
|
+
include Core::Render
|
6
|
+
include Core::Procs
|
7
|
+
|
8
|
+
#Menu variables
|
9
|
+
@@active_class = 'active'
|
10
|
+
@@show_active_class = true
|
11
|
+
@@show_menu_id = true
|
12
|
+
@@show_submenu = false
|
13
|
+
@@menu_prefix = ''
|
14
|
+
|
15
|
+
#Item variables
|
16
|
+
@@item_perfix = ''
|
17
|
+
@@name_prefix = ''
|
18
|
+
@@show_name_id = true
|
19
|
+
@@show_item_id = true
|
20
|
+
|
21
|
+
#Breadcrumb variables
|
22
|
+
@@breadcrumb_divider = "/"
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
attr_accessor :active_class, :show_active_class, :show_menu_id,
|
27
|
+
:show_item_id, :show_name_id, :show_submenu,
|
28
|
+
:menu_prefix, :item_prefix, :name_prefix,
|
29
|
+
:item_classes,
|
30
|
+
:name, :parent
|
31
|
+
|
4
32
|
def initialize(id, args, parent)
|
5
33
|
@item_id = id
|
6
34
|
@name = args.first
|
7
35
|
@url_options = args.second
|
8
36
|
@parent = parent
|
9
|
-
|
10
37
|
@sub_items = []
|
11
|
-
|
12
|
-
|
38
|
+
|
39
|
+
set_as_active if active?
|
13
40
|
end
|
14
41
|
|
15
42
|
def method_missing(name, *args)
|
@@ -18,44 +45,59 @@ module SemanticNavigation
|
|
18
45
|
yield item if block_given?
|
19
46
|
end
|
20
47
|
|
21
|
-
def
|
22
|
-
|
23
|
-
if parent
|
24
|
-
set_as_active if active?
|
25
|
-
sub = render_subitems
|
26
|
-
link = view_object.link_to @name, @url_options, :class => classes, :id => @item_id
|
27
|
-
view_object.content_tag(:li, link + sub, :id => @item_id, :class => classes)
|
28
|
-
else
|
29
|
-
render_subitems
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def set_as_active
|
34
|
-
@active = true
|
35
|
-
parent.set_as_active if parent
|
48
|
+
def self.set_default_option(name, value)
|
49
|
+
class_variable_set("@@#{name}".to_sym, value)
|
36
50
|
end
|
37
51
|
|
38
52
|
private
|
39
53
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
54
|
+
def ul_id
|
55
|
+
prefix = @menu_prefix.nil? ? @@menu_prefix : @menu_prefix
|
56
|
+
flag = @show_menu_id.nil? ? @@show_menu_id : @show_menu_id
|
57
|
+
flag ? prefix+@item_id : nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def li_id
|
61
|
+
prefix = @item_prefix.nil? ? @@item_prefix : @item_prefix
|
62
|
+
flag = @show_item_id.nil? ? @@show_item_id : @show_item_id
|
63
|
+
flag ? prefix+@item_id : nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def a_id
|
67
|
+
prefix = @name_prefix.nil? ? @@name_prefix : @name_prefix
|
68
|
+
flag = @show_name_id.nil? ? @@show_name_id : @show_name_id
|
69
|
+
flag ? prefix+@item_id : nil
|
45
70
|
end
|
46
71
|
|
47
72
|
def classes
|
48
|
-
|
49
|
-
|
73
|
+
class_array = []
|
74
|
+
class_array << item_classes
|
75
|
+
active_class = @active_class.nil? ? @@active_class : @active_class
|
76
|
+
flag = @show_active_class.nil? ? @@show_active_class : @show_active_class
|
77
|
+
if @active && flag
|
78
|
+
class_array << active_class
|
50
79
|
end
|
80
|
+
class_array.flatten.join(' ')
|
51
81
|
end
|
52
82
|
|
53
|
-
def
|
54
|
-
|
83
|
+
def view_object
|
84
|
+
@@view_object
|
55
85
|
end
|
56
86
|
|
57
|
-
def
|
58
|
-
@
|
87
|
+
def show_submenu?
|
88
|
+
@show_submenu.nil? ? @@show_submenu : @show_submenu
|
89
|
+
end
|
90
|
+
|
91
|
+
def breadcrumb_name
|
92
|
+
@@breadcrumb_divider.html_safe
|
93
|
+
end
|
94
|
+
|
95
|
+
def breadcrumb_classes
|
96
|
+
if @@breadcrumb_classes.is_a? Array
|
97
|
+
return @@breadcrumb_classes.join(' ')
|
98
|
+
else
|
99
|
+
return @@breadcrumb_classes
|
100
|
+
end
|
59
101
|
end
|
60
102
|
|
61
103
|
end
|
@@ -1,15 +1,57 @@
|
|
1
1
|
#This file need to create the navigation in your app.
|
2
|
+
#You can learn more about how to customize your menu reading the wiki
|
3
|
+
#Wiki pages link: https://github.com/fr33z3/semantic_navigation/wiki/_pages
|
2
4
|
SemanticNavigation::Configuration.run do |config|
|
3
5
|
|
4
|
-
#
|
5
|
-
config.
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
#
|
6
|
+
#What's the name of the active menu class will be (the dafault is 'active')
|
7
|
+
#config.active_class = 'active';
|
8
|
+
|
9
|
+
#Do you want to show active class? (default = true)
|
10
|
+
#config.show_active_class = true
|
11
|
+
|
12
|
+
#Create the item ids (in the `li` tag) automatically (default = true)?
|
13
|
+
#config.show_item_id = true
|
14
|
+
|
15
|
+
#Create the menu ids (in the `ul` tag) automatically (default = true)?
|
16
|
+
#config.show_menu_id = true
|
17
|
+
|
18
|
+
#Create the name id (in the `a` tag) automatically (default = true)?
|
19
|
+
#config.show_name_id = true
|
20
|
+
|
21
|
+
#Show the submenu only when the menu is active (default = false)
|
22
|
+
#config.show_submenu = false
|
23
|
+
|
24
|
+
#Add menu prefix (`ul` tag)
|
25
|
+
#config.menu_prefix = 'menu_'
|
26
|
+
|
27
|
+
#Add item prefix (`li` tag)
|
28
|
+
#config.item_prefix = 'item_'
|
29
|
+
|
30
|
+
#Add name preifx (`a` tag)
|
31
|
+
#config.name_prefix = 'name_'
|
32
|
+
|
33
|
+
#Define breadcrumb divider
|
34
|
+
#config.breadcrumb_divider = "<span class ='divider'>/</span>"
|
35
|
+
|
36
|
+
#Define bread crumb classes
|
37
|
+
#config.breadcrumb_classes = "breadcrumb"
|
38
|
+
|
39
|
+
#That's how you can create your userbar menu
|
40
|
+
#config.userbar do |userbar|
|
41
|
+
# userbar.signin :controller => :session, :action => :signin, :if => current_user.nil?
|
42
|
+
# userbar.signout :controller => :session, :action => :signout, :if => !current_user.nil?
|
43
|
+
#end
|
44
|
+
#so you can use the helper 'render_userbar_menu' to render it.
|
45
|
+
|
46
|
+
#That's the creation of the `navigation` menu
|
47
|
+
#config.navigation {|n|
|
48
|
+
# n.dashboard 'Dashboard', :controller => :dashboard, :action => :index do |d|
|
49
|
+
# d.first 'First', :controller => :first, :action => :index do |f|
|
50
|
+
# f.sub_first 'Sub First', sub_first_index_path
|
51
|
+
# f.sub_second 'Sub Second', :controller => :sub_second, :action => :index
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
# n.second 'Help', :controller => :second, :action => :index
|
55
|
+
#}
|
14
56
|
|
15
57
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semantic_navigation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sergey Gribovski
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-30 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -60,11 +60,14 @@ extra_rdoc_files: []
|
|
60
60
|
|
61
61
|
files:
|
62
62
|
- .gitignore
|
63
|
+
- Changelog.md
|
63
64
|
- Gemfile
|
64
65
|
- README.md
|
65
66
|
- Rakefile
|
66
67
|
- lib/semantic_navigation.rb
|
67
68
|
- lib/semantic_navigation/configuration.rb
|
69
|
+
- lib/semantic_navigation/core/procs.rb
|
70
|
+
- lib/semantic_navigation/core/render.rb
|
68
71
|
- lib/semantic_navigation/helper_methods.rb
|
69
72
|
- lib/semantic_navigation/item.rb
|
70
73
|
- lib/semantic_navigation/railtie.rb
|