semantic_navigation 0.0.14 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog.md +6 -1
- data/README.md +3 -3
- data/lib/generators/semantic_navigation/breadcrumb_renderer/breadcrumb_renderer_generator.rb +19 -0
- data/lib/generators/semantic_navigation/breadcrumb_renderer/templates/breadcrumb_renderer.rb +62 -0
- data/lib/generators/semantic_navigation/install/install_generator.rb +2 -2
- data/lib/generators/semantic_navigation/install/templates/semantic_navigation.rb +5 -5
- data/lib/generators/semantic_navigation/list_renderer/list_renderer_generator.rb +19 -0
- data/lib/generators/semantic_navigation/list_renderer/templates/list_renderer.rb +64 -0
- data/lib/semantic_navigation/configuration.rb +10 -10
- data/lib/semantic_navigation/core.rb +8 -4
- data/lib/semantic_navigation/core/base.rb +8 -8
- data/lib/semantic_navigation/core/leaf.rb +5 -33
- data/lib/semantic_navigation/core/name_methods.rb +24 -0
- data/lib/semantic_navigation/core/navigation.rb +23 -12
- data/lib/semantic_navigation/core/node.rb +6 -34
- data/lib/semantic_navigation/core/url_methods.rb +24 -0
- data/lib/semantic_navigation/railtie.rb +17 -7
- data/lib/semantic_navigation/renderers/acts_as_breadcrumb.rb +8 -8
- data/lib/semantic_navigation/renderers/acts_as_list.rb +34 -33
- data/lib/semantic_navigation/renderers/bread_crumb.rb +5 -5
- data/lib/semantic_navigation/renderers/list.rb +9 -9
- data/lib/semantic_navigation/renderers/render_helpers.rb +19 -32
- data/lib/semantic_navigation/twitter_bootstrap/breadcrumb.rb +8 -8
- data/lib/semantic_navigation/twitter_bootstrap/list.rb +15 -15
- data/lib/semantic_navigation/twitter_bootstrap/tabs.rb +14 -14
- data/lib/semantic_navigation/version.rb +1 -1
- data/spec/lib/semantic_navigation/configuration_spec.rb +50 -42
- data/spec/lib/semantic_navigation/core/navigation_spec.rb +58 -0
- data/spec/lib/semantic_navigation/helper_methods_spec.rb +31 -31
- data/spec/lib/semantic_navigation/twitter_bootstrap/breadcrumb_spec.rb +46 -46
- data/spec/lib/semantic_navigation/twitter_bootstrap/list_spec.rb +49 -49
- data/spec/lib/semantic_navigation_spec.rb +4 -1
- metadata +14 -9
- data/spec/lib/semantic_navigation/custom_renderer.rb +0 -3
data/Changelog.md
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
### 0.0
|
1
|
+
### 0.1.0
|
2
|
+
* Making register_renderer function to catch already registered renderer class by received symbol name
|
3
|
+
* Added scoping for items definitions
|
4
|
+
* Added renderers generators
|
5
|
+
|
6
|
+
### 0.0.14 / August 29, 2012
|
2
7
|
* Now in render_if proc there is possibility to catch self(rendering element)
|
3
8
|
* Items accept procs for links definitions
|
4
9
|
* Fixed bug in active_item_for method
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ This is semantic_navigation gem
|
|
4
4
|
Forget fat layouts, views and controllers. This gem will do all the menu staff for you.
|
5
5
|
You simply just have to generate configuration file, fill it with your menu hierarchy and renderer it wherever you want.
|
6
6
|
|
7
|
-
|
7
|
+
Simply and customizable it will make for you all the routins you were spending a lot of time before.
|
8
8
|
|
9
9
|
* semantic_navigation supports defining as many separate menus as you want and they can be as deep as you need.
|
10
10
|
* supports different styles of url definitions ('route#like', :symbols, 'strings', {hash: 'like'})
|
@@ -35,7 +35,7 @@ $ rails generate semantic_navigation:install
|
|
35
35
|
|
36
36
|
###Quick start
|
37
37
|
|
38
|
-
Configure your navigation in config/semantic_navigation.rb
|
38
|
+
Configure your navigation in config/initializers/semantic_navigation.rb
|
39
39
|
|
40
40
|
<pre><code>
|
41
41
|
SemanticNavigation::Configuration.run do
|
@@ -66,4 +66,4 @@ For the information of how to configure and render your navigation read the <a h
|
|
66
66
|
Dependence:
|
67
67
|
|
68
68
|
* Ruby >= 1.9.2
|
69
|
-
* Rails >= 3.0.0
|
69
|
+
* Rails >= 3.0.0
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SemanticNavigation
|
2
|
+
module Generators
|
3
|
+
class BreadcrumbRendererGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
desc "This generator creates list like rendering class"
|
6
|
+
|
7
|
+
def generate_list_renderer
|
8
|
+
template "breadcrumb_renderer.rb", "app/models/renderers/#{file_name}.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
def register_renderer
|
12
|
+
semantic_navigation_config = Railtie.actual_config_location
|
13
|
+
register_string = " register_renderer :#{file_name}, Renderers::#{class_name}\n"
|
14
|
+
inject_into_file semantic_navigation_config, register_string, :after => "SemanticNavigation::Configuration.run do\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class Renderers::<%= class_name %>
|
2
|
+
#Default render helpers. Do not delete this if don't want to write your own.
|
3
|
+
include RenderHelpers
|
4
|
+
#The default list rendering logic. Do not delete if don't want to write your own.
|
5
|
+
include ActsAsBreadcrumb
|
6
|
+
style_accessor :last_as_link => false,
|
7
|
+
:breadcrumb_separator => '/'
|
8
|
+
|
9
|
+
#Default navigation classes
|
10
|
+
navigation_active_class [:active]
|
11
|
+
node_active_class [:active]
|
12
|
+
leaf_active_class [:active]
|
13
|
+
link_active_class [:active]
|
14
|
+
|
15
|
+
show_navigation_active_class true
|
16
|
+
show_node_active_class true
|
17
|
+
show_leaf_active_class true
|
18
|
+
show_link_active_class true
|
19
|
+
|
20
|
+
show_navigation_id true
|
21
|
+
show_node_id true
|
22
|
+
show_leaf_id true
|
23
|
+
show_link_id true
|
24
|
+
|
25
|
+
navigation_default_classes ['<%= file_name %>']
|
26
|
+
node_default_classes ['<%= file_name %>']
|
27
|
+
leaf_default_classes ['<%= file_name %>']
|
28
|
+
link_default_classes ['<%= file_name %>']
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def navigation(object)
|
33
|
+
content_tag :ul, nil, :id => show_id(:navigation, object.id),
|
34
|
+
:class => merge_classes(:navigation, object.active, object.classes) do
|
35
|
+
yield
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def node(object)
|
40
|
+
content_tag(:li, nil, :id => show_id(:leaf, object.id),
|
41
|
+
:class => merge_classes(:leaf, object.active, object.classes)) do
|
42
|
+
link_to(object_name(object), object.url, :id => show_id(:link, object.id),
|
43
|
+
:class => merge_classes(:link, object.active, object.link_classes))
|
44
|
+
end +
|
45
|
+
content_tag(:li) do
|
46
|
+
breadcrumb_separator
|
47
|
+
end +
|
48
|
+
yield
|
49
|
+
end
|
50
|
+
|
51
|
+
def leaf(object)
|
52
|
+
content_tag :li, nil, :id => show_id(:leaf, object.id),
|
53
|
+
:class => merge_classes(:leaf, object.active, object.classes) do
|
54
|
+
if last_as_link
|
55
|
+
link_to object_name(object), object.url, :id => show_id(:link, object.id),
|
56
|
+
:class => merge_classes(:link, object.active, object.link_classes)
|
57
|
+
else
|
58
|
+
object_name(object)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -3,7 +3,7 @@ module SemanticNavigation
|
|
3
3
|
class InstallGenerator < ::Rails::Generators::Base
|
4
4
|
source_root File.expand_path("../templates", __FILE__)
|
5
5
|
desc "This generator creates config file for your navigation"
|
6
|
-
|
6
|
+
|
7
7
|
def add_semantic_navigation
|
8
8
|
puts <<-EOM
|
9
9
|
+===============================================================+
|
@@ -12,7 +12,7 @@ module SemanticNavigation
|
|
12
12
|
| http://github.com/fr33z3/semantic_navigation/wiki. |
|
13
13
|
+===============================================================+
|
14
14
|
EOM
|
15
|
-
copy_file "semantic_navigation.rb", "config/semantic_navigation.rb"
|
15
|
+
copy_file "semantic_navigation.rb", "config/initializers/semantic_navigation.rb"
|
16
16
|
copy_file "semantic_navigation.en.yml", "config/locales/semantic_navigation.en.yml"
|
17
17
|
end
|
18
18
|
end
|
@@ -2,20 +2,20 @@
|
|
2
2
|
SemanticNavigation::Configuration.run do
|
3
3
|
# Read wiki https://github.com/fr33z3/semantic_navigation/wiki to lear about
|
4
4
|
# semantic_navigation configuration
|
5
|
-
|
5
|
+
|
6
6
|
#Override renderer default styles:
|
7
7
|
#styles_for :list do
|
8
8
|
# show_navigation_active_class true
|
9
9
|
# navigation_active_class [:some_active_class]
|
10
10
|
#end
|
11
|
-
|
11
|
+
|
12
12
|
# You can register your custom renderer:
|
13
13
|
#register_renderer :some_renderer, SomeRendererClass
|
14
|
-
|
14
|
+
|
15
15
|
# The example of the navigation:
|
16
16
|
#navigate :navigation do
|
17
17
|
# header :header_item, :name => 'Header Item'
|
18
|
-
# item :first_item, :first_item_route, :ico => 'user' do
|
18
|
+
# item :first_item, :first_item_route, :ico => 'user' do
|
19
19
|
# item :sub_item, :sub_item_route do
|
20
20
|
# item :sub_sub_item, :sub_sub_item_route
|
21
21
|
# end
|
@@ -24,6 +24,6 @@ SemanticNavigation::Configuration.run do
|
|
24
24
|
# divider
|
25
25
|
# item :second_item, :second_item_route
|
26
26
|
#end
|
27
|
-
|
27
|
+
|
28
28
|
end
|
29
29
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SemanticNavigation
|
2
|
+
module Generators
|
3
|
+
class ListRendererGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
desc "This generator creates list like rendering class"
|
6
|
+
|
7
|
+
def generate_list_renderer
|
8
|
+
template "list_renderer.rb", "app/models/renderers/#{file_name}.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
def register_renderer
|
12
|
+
semantic_navigation_config = Railtie.actual_config_location
|
13
|
+
register_string = " register_renderer :#{file_name}, Renderers::#{class_name}\n"
|
14
|
+
inject_into_file semantic_navigation_config, register_string, :after => "SemanticNavigation::Configuration.run do\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class Renderers::<%= class_name %>
|
2
|
+
#Default render helpers. Do not delete this if don't want to write your own.
|
3
|
+
include SemanticNavigation::Renderers::RenderHelpers
|
4
|
+
#The default list rendering logic. Do not delete if don't want to write your own.
|
5
|
+
include SemanticNavigation::Renderers::ActsAsList
|
6
|
+
|
7
|
+
#Default navigation classes
|
8
|
+
navigation_active_class [:active]
|
9
|
+
node_active_class [:active]
|
10
|
+
leaf_active_class [:active]
|
11
|
+
link_active_class [:active]
|
12
|
+
|
13
|
+
show_navigation_active_class true
|
14
|
+
show_node_active_class true
|
15
|
+
show_leaf_active_class true
|
16
|
+
show_link_active_class true
|
17
|
+
|
18
|
+
show_navigation_id true
|
19
|
+
show_node_id true
|
20
|
+
show_leaf_id true
|
21
|
+
show_link_id true
|
22
|
+
|
23
|
+
navigation_default_classes ['<%= file_name %>']
|
24
|
+
node_default_classes ['<%= file_name %>']
|
25
|
+
leaf_default_classes ['<%= file_name %>']
|
26
|
+
link_default_classes ['<%= file_name %>']
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
#Main navigation block
|
31
|
+
def navigation(object)
|
32
|
+
content_tag :ul, nil, :id => show_id(:navigation, object.id),
|
33
|
+
:class => merge_classes(:navigation, object.active, object.classes) do
|
34
|
+
yield
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
#Navigation node block
|
39
|
+
def node(object)
|
40
|
+
content_tag :li, nil, :id => show_id(:leaf, object.id),
|
41
|
+
:class => merge_classes(:leaf, object.active, object.classes) do
|
42
|
+
link_to(object_name(object), object.url, :id => show_id(:link, object.id),
|
43
|
+
:class => merge_classes(:link, object.active, object.link_classes))+
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#Block rendering inside the node block
|
49
|
+
def node_content(object)
|
50
|
+
content_tag(:ul, nil, :id => show_id(:node, object.id),
|
51
|
+
:class => merge_classes(:node, object.active, object.node_classes)) do
|
52
|
+
yield
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#Navigation leaf block
|
57
|
+
def leaf(object)
|
58
|
+
content_tag :li, nil, :id => show_id(:leaf, object.id),
|
59
|
+
:class => merge_classes(:leaf, object.active, object.classes) do
|
60
|
+
link_to object_name(object), object.url, :id => show_id(:link, object.id),
|
61
|
+
:class => merge_classes(:link, object.active, object.link_classes)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SemanticNavigation
|
2
2
|
class Configuration
|
3
|
-
|
3
|
+
|
4
4
|
@@view_object = nil
|
5
5
|
@@navigations = {}
|
6
6
|
@@renderers = {}
|
@@ -9,7 +9,7 @@ module SemanticNavigation
|
|
9
9
|
def self.run(&block)
|
10
10
|
self.class_eval &block if block_given?
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def self.navigate(id, options = {}, &block)
|
14
14
|
options[:id] = id.to_sym
|
15
15
|
options[:i18n_name] = "semantic_navigation.#{id}"
|
@@ -17,7 +17,7 @@ module SemanticNavigation
|
|
17
17
|
navigation.instance_eval &block if block_given?
|
18
18
|
@@navigations[id.to_sym] = navigation
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def render(menu_id, renderer_name, options, view_object)
|
22
22
|
@@view_object = view_object
|
23
23
|
renderer = @@renderers[renderer_name].new
|
@@ -25,23 +25,23 @@ module SemanticNavigation
|
|
25
25
|
renderer.instance_eval &@@render_styles[renderer_name]
|
26
26
|
end
|
27
27
|
options.keys.each{|key| renderer.send "#{key}=", options[key]}
|
28
|
-
renderer.name = renderer_name
|
28
|
+
renderer.name = renderer_name
|
29
29
|
navigation = @@navigations[menu_id]
|
30
30
|
navigation.mark_active
|
31
31
|
navigation.render(renderer)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def self.styles_for(name)
|
35
35
|
@@render_styles[name.to_sym] = proc
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def self.register_renderer(*options)
|
39
39
|
if options.count == 1
|
40
40
|
name = options[0].name.demodulize.underscore.to_sym
|
41
41
|
renderer_class = options[0]
|
42
|
-
elsif options.count == 2
|
42
|
+
elsif options.count == 2
|
43
43
|
name = options[0].to_sym
|
44
|
-
renderer_class = options[1]
|
44
|
+
renderer_class = options[1].is_a?(Symbol) ? @@renderers[options[1]] : options[1]
|
45
45
|
end
|
46
46
|
@@renderers[name] = renderer_class
|
47
47
|
SemanticNavigation::HelperMethods.class_eval "
|
@@ -51,7 +51,7 @@ module SemanticNavigation
|
|
51
51
|
end
|
52
52
|
"
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def self.view_object
|
56
56
|
@@view_object
|
57
57
|
end
|
@@ -59,7 +59,7 @@ module SemanticNavigation
|
|
59
59
|
def self.view_object=(view_object)
|
60
60
|
@@view_object = view_object
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def self.navigation(name)
|
64
64
|
@@navigations[name]
|
65
65
|
end
|
@@ -1,4 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
required_files = %w(base
|
2
|
+
url_methods
|
3
|
+
name_methods
|
4
|
+
navigation
|
5
|
+
leaf node)
|
6
|
+
required_files.each do |file|
|
7
|
+
require "semantic_navigation/core/#{file}"
|
8
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module SemanticNavigation
|
2
2
|
module Core
|
3
|
-
class Base
|
4
|
-
|
3
|
+
class Base
|
4
|
+
|
5
5
|
attr_accessor :id, :level, :classes, :active
|
6
6
|
attr_writer :render_if
|
7
|
-
|
7
|
+
|
8
8
|
def render_if
|
9
9
|
!@render_if.nil? ? view_object.instance_exec(self, &@render_if) : true
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def initialize(options, level)
|
13
13
|
@level = level
|
14
14
|
options.keys.each do |option_name|
|
@@ -20,9 +20,9 @@ module SemanticNavigation
|
|
20
20
|
class_name = self.class.name.split('::').last.downcase
|
21
21
|
renderer.send :"render_#{class_name}", self
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
private
|
25
|
-
|
25
|
+
|
26
26
|
def view_object
|
27
27
|
SemanticNavigation::Configuration.view_object
|
28
28
|
end
|
@@ -38,7 +38,7 @@ module SemanticNavigation
|
|
38
38
|
end
|
39
39
|
result
|
40
40
|
end
|
41
|
-
|
42
|
-
end
|
41
|
+
|
42
|
+
end
|
43
43
|
end
|
44
44
|
end
|
@@ -1,23 +1,15 @@
|
|
1
1
|
module SemanticNavigation
|
2
2
|
module Core
|
3
3
|
class Leaf < Base
|
4
|
+
include UrlMethods
|
5
|
+
include NameMethods
|
6
|
+
|
4
7
|
attr_accessor :link_classes
|
5
|
-
|
6
|
-
def url
|
7
|
-
urls.first
|
8
|
-
end
|
9
8
|
|
10
9
|
def initialize(options, level)
|
11
10
|
super options, level
|
12
11
|
end
|
13
|
-
|
14
|
-
def name(renderer_name = nil)
|
15
|
-
rendering_name = @name
|
16
|
-
rendering_name = rendering_name[renderer_name.to_sym] || rendering_name[:default] if rendering_name.is_a?(Hash)
|
17
|
-
rendering_name = view_object.instance_eval(&rendering_name).to_s if rendering_name.is_a?(Proc)
|
18
|
-
rendering_name || i18n_name(renderer_name)
|
19
|
-
end
|
20
|
-
|
12
|
+
|
21
13
|
def mark_active
|
22
14
|
if @url
|
23
15
|
@active = urls.map{|u| current_page?(u) rescue false}.reduce(:"|")
|
@@ -26,26 +18,6 @@ module SemanticNavigation
|
|
26
18
|
end
|
27
19
|
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
def i18n_name(renderer_name = nil)
|
32
|
-
name = I18n.t("#{@i18n_name}.#{@id}", :default => '')
|
33
|
-
if name.is_a? Hash
|
34
|
-
name = name[renderer_name.to_sym] || name[:default]
|
35
|
-
end
|
36
|
-
name || ''
|
37
|
-
end
|
38
|
-
|
39
|
-
def urls
|
40
|
-
[@url].flatten(1).map do |url|
|
41
|
-
if url.is_a?(Proc)
|
42
|
-
view_object.instance_eval(&url) rescue ''
|
43
|
-
else
|
44
|
-
url
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
21
|
+
end
|
50
22
|
end
|
51
23
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SemanticNavigation
|
2
|
+
module Core
|
3
|
+
module NameMethods
|
4
|
+
|
5
|
+
def name(renderer_name = nil)
|
6
|
+
rendering_name = @name
|
7
|
+
rendering_name = rendering_name[renderer_name.to_sym] || rendering_name[:default] if rendering_name.is_a?(Hash)
|
8
|
+
rendering_name = view_object.instance_eval(&rendering_name).to_s if rendering_name.is_a?(Proc)
|
9
|
+
rendering_name || i18n_name(renderer_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def i18n_name(renderer_name = nil)
|
15
|
+
name = I18n.t("#{@i18n_name}.#{@id}", :default => '')
|
16
|
+
if name.is_a? Hash
|
17
|
+
name = name[renderer_name.to_sym] || name[:default]
|
18
|
+
end
|
19
|
+
name || ''
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|