semantic_navigation 0.0.14 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/Changelog.md +6 -1
  2. data/README.md +3 -3
  3. data/lib/generators/semantic_navigation/breadcrumb_renderer/breadcrumb_renderer_generator.rb +19 -0
  4. data/lib/generators/semantic_navigation/breadcrumb_renderer/templates/breadcrumb_renderer.rb +62 -0
  5. data/lib/generators/semantic_navigation/install/install_generator.rb +2 -2
  6. data/lib/generators/semantic_navigation/install/templates/semantic_navigation.rb +5 -5
  7. data/lib/generators/semantic_navigation/list_renderer/list_renderer_generator.rb +19 -0
  8. data/lib/generators/semantic_navigation/list_renderer/templates/list_renderer.rb +64 -0
  9. data/lib/semantic_navigation/configuration.rb +10 -10
  10. data/lib/semantic_navigation/core.rb +8 -4
  11. data/lib/semantic_navigation/core/base.rb +8 -8
  12. data/lib/semantic_navigation/core/leaf.rb +5 -33
  13. data/lib/semantic_navigation/core/name_methods.rb +24 -0
  14. data/lib/semantic_navigation/core/navigation.rb +23 -12
  15. data/lib/semantic_navigation/core/node.rb +6 -34
  16. data/lib/semantic_navigation/core/url_methods.rb +24 -0
  17. data/lib/semantic_navigation/railtie.rb +17 -7
  18. data/lib/semantic_navigation/renderers/acts_as_breadcrumb.rb +8 -8
  19. data/lib/semantic_navigation/renderers/acts_as_list.rb +34 -33
  20. data/lib/semantic_navigation/renderers/bread_crumb.rb +5 -5
  21. data/lib/semantic_navigation/renderers/list.rb +9 -9
  22. data/lib/semantic_navigation/renderers/render_helpers.rb +19 -32
  23. data/lib/semantic_navigation/twitter_bootstrap/breadcrumb.rb +8 -8
  24. data/lib/semantic_navigation/twitter_bootstrap/list.rb +15 -15
  25. data/lib/semantic_navigation/twitter_bootstrap/tabs.rb +14 -14
  26. data/lib/semantic_navigation/version.rb +1 -1
  27. data/spec/lib/semantic_navigation/configuration_spec.rb +50 -42
  28. data/spec/lib/semantic_navigation/core/navigation_spec.rb +58 -0
  29. data/spec/lib/semantic_navigation/helper_methods_spec.rb +31 -31
  30. data/spec/lib/semantic_navigation/twitter_bootstrap/breadcrumb_spec.rb +46 -46
  31. data/spec/lib/semantic_navigation/twitter_bootstrap/list_spec.rb +49 -49
  32. data/spec/lib/semantic_navigation_spec.rb +4 -1
  33. metadata +14 -9
  34. data/spec/lib/semantic_navigation/custom_renderer.rb +0 -3
@@ -2,15 +2,17 @@ module SemanticNavigation
2
2
  module Core
3
3
  class Navigation < Base
4
4
  attr_accessor :sub_elements
5
-
5
+
6
6
  def initialize(options, level = 0)
7
7
  @sub_elements = []
8
+ @scope_options = {}
8
9
  super options, level
9
10
  end
10
11
 
11
12
  def item(id, url=nil, options={}, &block)
12
13
  options[:id] = id.to_sym
13
-
14
+ options[:render_if] ||= @scope_options[:render_if]
15
+
14
16
  if url.is_a?(Array)
15
17
  options[:url] = [url].flatten(1).map{|url| decode_url(url)}
16
18
  else
@@ -18,7 +20,7 @@ module SemanticNavigation
18
20
  end
19
21
 
20
22
  options[:i18n_name] = @i18n_name
21
-
23
+
22
24
  if block_given?
23
25
  element = Node.new(options, @level+1)
24
26
  element.instance_eval &block
@@ -32,21 +34,24 @@ module SemanticNavigation
32
34
  puts 'Warning: do not define `dividers` using `item` method. Use `divider` instead. This logic will be deprecated soon.'
33
35
  end
34
36
  end
35
-
37
+
36
38
  @sub_elements.push element
37
39
  end
38
40
 
39
41
  def header(id, options={})
40
42
  options[:id] = id.to_sym
43
+ options[:render_if] ||= @scope_options[:render_if]
41
44
  options[:url] = nil
42
45
  options[:i18n_name] = @i18n_name
43
46
  @sub_elements.push Leaf.new(options, @level+1)
44
47
  end
45
48
 
46
- def divider
47
- options = {:id => :divider,
48
- :url => nil,
49
- :i18n_name => nil}
49
+ def divider(options = {})
50
+ options[:id] = :divider
51
+ options[:render_if] ||= @scope_options[:render_if]
52
+ options[:url] = nil
53
+ options[:i18n_name] = nil
54
+ options[:name] = nil
50
55
  @sub_elements.push Leaf.new(options, @level+1)
51
56
  end
52
57
 
@@ -58,8 +63,14 @@ module SemanticNavigation
58
63
  end
59
64
  end
60
65
 
66
+ def scope(options = {}, &block)
67
+ @scope_options = options
68
+ self.instance_eval &block
69
+ @scope_options = {}
70
+ end
71
+
61
72
  def mark_active
62
- @sub_elements.each do |element|
73
+ @sub_elements.each do |element|
63
74
  element.mark_active
64
75
  end
65
76
  @active = !@sub_elements.find{|element| element.active}.nil?
@@ -71,12 +82,12 @@ module SemanticNavigation
71
82
  if url.is_a? String
72
83
  controller_name, action_name = url.split('#')
73
84
  if controller_name && action_name
74
- decoded_url = {:controller => controller_name, :action => action_name}
75
- end
85
+ decoded_url = {:controller => controller_name, :action => action_name}
86
+ end
76
87
  end
77
88
  decoded_url || url
78
89
  end
79
-
90
+
80
91
  end
81
92
  end
82
93
  end
@@ -1,50 +1,22 @@
1
1
  module SemanticNavigation
2
2
  module Core
3
- class Node < Navigation
3
+ class Node < Navigation
4
+ include UrlMethods
5
+ include NameMethods
6
+
4
7
  attr_accessor :link_classes, :node_classes
5
-
6
- def url
7
- urls.first
8
- end
9
-
8
+
10
9
  def initialize(options, level)
11
10
  @url = []
12
11
  super options, level
13
12
  end
14
-
15
- def name(renderer_name = nil)
16
- rendering_name = @name
17
- rendering_name = rendering_name[renderer_name.to_sym] || rendering_name[:default] if rendering_name.is_a?(Hash)
18
- rendering_name = view_object.instance_eval(&rendering_name).to_s if rendering_name.is_a?(Proc)
19
- rendering_name || i18n_name(renderer_name)
20
- end
21
-
13
+
22
14
  def mark_active
23
15
  @sub_elements.each{|element| element.mark_active}
24
16
  @active = urls.map{|u| current_page?(u) rescue false}.reduce(:"|")
25
17
  @active |= !@sub_elements.find{|element| element.active}.nil?
26
18
  end
27
-
28
- private
29
-
30
- def i18n_name(renderer_name = nil)
31
- name = I18n.t("#{@i18n_name}.#{@id}", :default => '')
32
- if name.is_a? Hash
33
- name = name[renderer_name.to_sym] || name[:default]
34
- end
35
- name || ''
36
- end
37
19
 
38
- def urls
39
- [@url].flatten(1).map do |url|
40
- if url.is_a?(Proc)
41
- view_object.instance_eval(&url) rescue ''
42
- else
43
- url
44
- end
45
- end
46
- end
47
-
48
20
  end
49
21
  end
50
22
  end
@@ -0,0 +1,24 @@
1
+ module SemanticNavigation
2
+ module Core
3
+ module UrlMethods
4
+
5
+ def url
6
+ urls.first
7
+ end
8
+
9
+ private
10
+
11
+ def urls
12
+ [@url].flatten(1).map do |url|
13
+ if url.is_a?(Proc)
14
+ view_object.instance_eval(&url) rescue ''
15
+ else
16
+ url
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -2,7 +2,7 @@ require 'semantic_navigation/helper_methods'
2
2
 
3
3
  module SemanticNavigation
4
4
  class Railtie < Rails::Railtie
5
-
5
+
6
6
  initializer "semantic_navigation.extend_helper_methods" do
7
7
  ActiveSupport.on_load :action_view do
8
8
  ActionView::Base.send :include, HelperMethods
@@ -16,7 +16,7 @@ module SemanticNavigation
16
16
  conf.register_renderer :bootstrap_tabs, TwitterBootstrap::Tabs
17
17
  conf.register_renderer :bootstrap_pills, TwitterBootstrap::Tabs
18
18
  conf.register_renderer :bootstrap_simple_nav, SemanticNavigation::Renderers::List
19
-
19
+
20
20
  conf.styles_for :bootstrap_pills do
21
21
  navigation_default_classes [:nav, 'nav-pills']
22
22
  end
@@ -24,16 +24,26 @@ module SemanticNavigation
24
24
  navigation_default_classes [:nav]
25
25
  end
26
26
  end
27
-
27
+
28
+ def self.actual_config_location
29
+ locations = ["#{Rails.root}/config/initializers/semantic_navigation.rb",
30
+ "#{Rails.root}/config/semantic_navigation.rb"]
31
+ actual_location = locations.find{|l| File.exists?(l)}
32
+ throw "Please create a semantic_navigation configuration file before starting the project!" unless actual_location
33
+ puts "DEPRECATION WARNING: Please move the configuration file from #{locations.second}
34
+ to #{locations.first}! Current configuration file path will be deprecated soon!" if locations.find_index(actual_location) == 1
35
+ actual_location
36
+ end
37
+
28
38
  if Rails.env == "production"
29
39
  config.after_initialize {
30
- load "#{Rails.root}/config/semantic_navigation.rb"
40
+ load SemanticNavigation::Railtie.actual_config_location
31
41
  }
32
42
  else
33
43
  ActionDispatch::Callbacks.before {
34
- load "#{Rails.root}/config/semantic_navigation.rb"
35
- }
44
+ load SemanticNavigation::Railtie.actual_config_location
45
+ }
36
46
  end
37
-
47
+
38
48
  end
39
49
  end
@@ -1,7 +1,7 @@
1
1
  module SemanticNavigation
2
2
  module Renderers
3
3
  module ActsAsBreadcrumb
4
-
4
+
5
5
  def render_navigation(object)
6
6
  return '' unless object.render_if
7
7
  navigation(object) do
@@ -13,30 +13,30 @@ module SemanticNavigation
13
13
  active_element = object.sub_elements.find{|e| e.active}
14
14
  active_element.render(self) if active_element
15
15
  end
16
- end
16
+ end
17
17
  end
18
18
 
19
19
  def render_node(object)
20
20
  active_element = object.sub_elements.find{|e| e.active}
21
21
  if active_element && !active_element.id.in?([except_for].flatten) && active_element.render_if
22
- render_element = active_element.render(self)
22
+ render_element = active_element.render(self)
23
23
  end
24
24
  if render_element
25
25
  node(object) do
26
26
  render_element
27
- end
27
+ end
28
28
  else
29
29
  render_leaf(object)
30
- end
30
+ end
31
31
  end
32
-
32
+
33
33
  def render_leaf(object)
34
34
  show = !until_level.nil? ? object.level <= until_level+1 : true
35
35
  return nil unless show
36
-
36
+
37
37
  leaf(object)
38
38
  end
39
-
39
+
40
40
  end
41
41
  end
42
42
  end
@@ -1,47 +1,48 @@
1
1
  module SemanticNavigation
2
2
  module Renderers
3
3
  module ActsAsList
4
-
5
- def render_navigation(object)
6
- return '' unless object.render_if
7
- navigation(object) do
8
- while !object.class.in?(SemanticNavigation::Core::Leaf, NilClass) &&
9
- from_level.to_i > object.level
10
- object = object.sub_elements.find{|e| e.active}
11
- end
12
- show = !until_level.nil? && !object.nil? ? object.level <= until_level : true
13
- if !object.class.in?(SemanticNavigation::Core::Leaf, NilClass) && show
14
- object.sub_elements.map{|element| element.render(self)}.compact.sum
15
- end
16
- end
17
- end
18
4
 
19
- def render_node(object)
20
- if !object.id.in?([except_for].flatten) && object.render_if
21
- content = render_node_content(object)
22
- if content
23
- node(object) do
24
- content
25
- end
26
- else
27
- render_leaf(object)
28
- end
5
+ def render_navigation(object)
6
+ return '' unless object.render_if
7
+ navigation(object) do
8
+ while !object.class.in?(SemanticNavigation::Core::Leaf, NilClass) &&
9
+ from_level.to_i > object.level
10
+ object = object.sub_elements.find{|e| e.active}
11
+ end
12
+ show = !until_level.nil? && !object.nil? ? object.level <= until_level : true
13
+ if !object.class.in?(SemanticNavigation::Core::Leaf, NilClass) && show
14
+ object.sub_elements.map{|element| element.render(self)}.compact.sum
29
15
  end
30
16
  end
31
-
32
- def render_node_content(object)
33
- if (!until_level.nil? && until_level >= object.level) || until_level.nil?
34
- node_content(object) do
35
- object.sub_elements.map{|element| element.render(self)}.compact.sum
17
+ end
18
+
19
+ def render_node(object)
20
+ if !object.id.in?([except_for].flatten) && object.render_if
21
+ content = render_node_content(object)
22
+ if content
23
+ node(object) do
24
+ content
36
25
  end
26
+ else
27
+ render_leaf(object)
37
28
  end
38
29
  end
30
+ end
39
31
 
40
- def render_leaf(object)
41
- if !object.id.in?([except_for].flatten) && object.render_if
42
- leaf(object)
32
+ def render_node_content(object)
33
+ if (!until_level.nil? && until_level >= object.level) || until_level.nil?
34
+ node_content(object) do
35
+ object.sub_elements.map{|element| element.render(self)}.compact.sum
43
36
  end
44
- end
37
+ end
38
+ end
39
+
40
+ def render_leaf(object)
41
+ if !object.id.in?([except_for].flatten) && object.render_if
42
+ leaf(object)
43
+ end
44
+ end
45
+
45
46
  end
46
47
  end
47
48
  end
@@ -6,22 +6,22 @@ module SemanticNavigation
6
6
 
7
7
  style_accessor :last_as_link => false,
8
8
  :breadcrumb_separator => '/'
9
-
9
+
10
10
  navigation_default_classes [:breadcrumb]
11
11
  show_navigation_active_class false
12
12
  show_node_active_class false
13
13
  show_leaf_active_class false
14
14
  show_link_active_class false
15
-
15
+
16
16
  private
17
-
17
+
18
18
  def navigation(object)
19
19
  content_tag :ul, nil, :id => show_id(:navigation, object.id),
20
20
  :class => merge_classes(:navigation, object.active, object.classes) do
21
21
  yield
22
22
  end
23
23
  end
24
-
24
+
25
25
  def node(object)
26
26
  content_tag(:li, nil, :id => show_id(:leaf, object.id),
27
27
  :class => merge_classes(:leaf, object.active, object.classes)) do
@@ -33,7 +33,7 @@ module SemanticNavigation
33
33
  end +
34
34
  yield
35
35
  end
36
-
36
+
37
37
  def leaf(object)
38
38
  content_tag :li, nil, :id => show_id(:leaf, object.id),
39
39
  :class => merge_classes(:leaf, object.active, object.classes) do
@@ -3,34 +3,34 @@ module SemanticNavigation
3
3
  class List
4
4
  include RenderHelpers
5
5
  include ActsAsList
6
-
6
+
7
7
  navigation_default_classes [:list]
8
-
8
+
9
9
  private
10
-
10
+
11
11
  def navigation(object)
12
12
  content_tag :ul, nil, :id => show_id(:navigation, object.id),
13
13
  :class => merge_classes(:navigation, object.active, object.classes) do
14
14
  yield
15
15
  end
16
16
  end
17
-
17
+
18
18
  def node(object)
19
19
  content_tag :li, nil, :id => show_id(:leaf, object.id),
20
20
  :class => merge_classes(:leaf, object.active, object.classes) do
21
21
  link_to(object_name(object), object.url, :id => show_id(:link, object.id),
22
22
  :class => merge_classes(:link, object.active, object.link_classes))+
23
23
  yield
24
- end
24
+ end
25
25
  end
26
-
26
+
27
27
  def node_content(object)
28
28
  content_tag(:ul, nil, :id => show_id(:node, object.id),
29
29
  :class => merge_classes(:node, object.active, object.node_classes)) do
30
- yield
30
+ yield
31
31
  end
32
32
  end
33
-
33
+
34
34
  def leaf(object)
35
35
  content_tag :li, nil, :id => show_id(:leaf, object.id),
36
36
  :class => merge_classes(:leaf, object.active, object.classes) do
@@ -38,7 +38,7 @@ module SemanticNavigation
38
38
  :class => merge_classes(:link, object.active, object.link_classes)
39
39
  end
40
40
  end
41
-
41
+
42
42
  end
43
43
  end
44
44
  end
@@ -1,33 +1,20 @@
1
1
  module SemanticNavigation
2
2
  module Renderers
3
3
  module RenderHelpers
4
-
4
+
5
5
  def self.included(base)
6
6
  base.send :include, InstanceMethods
7
7
  base.extend(ClassMethods)
8
8
  base.class_eval do
9
- style_accessor :navigation_active_class => [:active],
10
- :node_active_class => [:active],
11
- :leaf_active_class => [:active],
12
- :link_active_class => [:active],
13
-
14
- :show_navigation_active_class => true,
15
- :show_node_active_class => true,
16
- :show_leaf_active_class => true,
17
- :show_link_active_class => true,
18
-
19
- :show_navigation_id => true,
20
- :show_node_id => true,
21
- :show_leaf_id => true,
22
- :show_link_id => true,
23
-
24
- :navigation_default_classes => [],
25
- :node_default_classes => [],
26
- :leaf_default_classes => [],
27
- :link_default_classes => []
9
+ [:navigation, :node, :leaf, :link].each do |e|
10
+ style_accessor :"#{e}_active_class" => [:active],
11
+ :"show_#{e}_active_class" => true,
12
+ :"show_#{e}_id" => true,
13
+ :"#{e}_default_classes" => []
14
+ end
28
15
  end
29
16
  end
30
-
17
+
31
18
  module ClassMethods
32
19
 
33
20
  def style_accessor(hash)
@@ -50,9 +37,9 @@ module SemanticNavigation
50
37
  end
51
38
  "
52
39
  send key, hash[key]
53
- end
40
+ end
54
41
  end
55
-
42
+
56
43
  def property_for(class_name,name)
57
44
  class_object = "semantic_navigation/core/#{class_name}".classify.constantize
58
45
  class_object.class_eval "
@@ -64,24 +51,24 @@ module SemanticNavigation
64
51
  "
65
52
  end
66
53
  end
67
-
68
- module InstanceMethods
54
+
55
+ module InstanceMethods
69
56
  attr_accessor :from_level, :until_level, :except_for, :name
70
-
57
+
71
58
  def level=(level)
72
59
  @from_level = level
73
60
  @until_level = level
74
61
  end
75
-
62
+
76
63
  def levels=(level_range)
77
64
  @from_level = level_range.first
78
65
  @until_level = level_range.last
79
66
  end
80
-
67
+
81
68
  def initialize
82
69
  @view_object = SemanticNavigation::Configuration.view_object
83
70
  end
84
-
71
+
85
72
  private
86
73
 
87
74
  def content_tag(tag_name, content = nil, options={}, &block)
@@ -107,9 +94,9 @@ module SemanticNavigation
107
94
  def object_name(object)
108
95
  object.name(self.name)
109
96
  end
110
-
97
+
111
98
  end
112
-
113
- end
99
+
100
+ end
114
101
  end
115
102
  end