site_map 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,7 +12,7 @@ SiteMap provides a way to model out your site's views in a hierarchal fashion. Y
12
12
 
13
13
  === Basic Definition
14
14
 
15
- SiteMap provides a simple syntax for defining a site's views.
15
+ SiteMap provides a simple syntax for defining a site's views. Add the following to +config/site_map.rb+ (site_map will load this when required) or an initializer for your app:
16
16
 
17
17
  require 'site_map'
18
18
  SiteMap.define do |map|
@@ -41,45 +41,9 @@ Every site map view node also has a +label+, +url+ and +visible+ attribute. The
41
41
  })
42
42
  end
43
43
 
44
- So same basic configuration, but I've added custom options for the previously mentioned attributes. Firstly, url and visible attributes are intended to be eav'd within the view space of an app. This allows you to specify very custom ways to determine a path or check if a node should be shown. Label's are not intended to be eval'd, but are dynamic in a different way, by subbing out symbol names with an options hash, again in the view space. This is explained more and used within site map's view helpers.
44
+ So same basic configuration, but I've added custom options for the previously mentioned attributes. Firstly, url and visible attributes are intended to be eval'd within the view space (scope) of an app. This allows you to specify very custom ways to determine a path or check if a node should be shown. Label's are not intended to be eval'd, but are dynamic in a different way, by subbing out symbol names with an options hash, again in the view space (scope). This is explained more and used within site map's view helpers.
45
45
 
46
- Secondly, not every attribute has to be set. By default, a site map's visible is always +true+, and it's url is +nil+. Label also has a default, based on the index. For labels, site map will try to use a +titleize+ method on the string version of it's index. If defined, the titleized string is returned, otherwise just the string version of the index is.
47
-
48
- === Groups and nesting
49
-
50
- Site map provides other ways to define nodes and also allows nesting nodes within other nodes.
51
-
52
- require 'site_map'
53
- SiteMap.define do |map|
54
- map.view :home
55
- map.view :about
56
- map.group :community do |community_group|
57
- community_group.view :forum
58
- community_group.view :blog
59
- end
60
- end
61
-
62
- In this definition, we are using a new method +group+. A group node is very similar to a regular view node, and is intended to group similar views. In this case, we have created a group, called community with a blog and forum view as children. A group's definition syntax is the same as a view's, but it has different defaulting logic and behavior within the site map. A group's url will default to it's first child's url, in this case the forum view's url. I'll explain the differences in how a group behaves in the looking up views and traversing the site map sections later.
63
-
64
- === Looking up a view node
65
-
66
- Enough we defining the site map, on to actually retrieving what's been defined. Assuming the previous definition:
67
-
68
- view_node = SiteMap[:forum]
69
- view_node = SiteMap[:community] # => returns nil, can't look up group nodes
70
-
71
- Specifying the index of a node will return that view node. When you look up a group node's index, it isn't returned. Group node's are not intended to be looked up, but still exist in the relationships of a view node.
72
-
73
- === View node relationships
74
-
75
- After looking up a view node, you can access it's relationships.
76
-
77
- view_node = SiteMap[:forum]
78
- view_node.parent # => returns community group node
79
- view_node.siblings # => returns an array with blog view node in it, [SiteMap[:blog]]
80
- view_node.parent.children # => returns an array with blog and forum view nodes, [SiteMap[:forum], SiteMap[:blog]]
81
-
82
- Other relationship methods include +ancestors+, +self_and_siblings+ and +self_and_ancestors+.
46
+ SiteMap also has other ways to define your site map. Check out the wiki for more help: http://wiki.github.com/jcredding/site_map/
83
47
 
84
48
  Copyright (c) 2010 John Collin Redding
85
49
  See the attached MIT License.
@@ -3,7 +3,7 @@ module SiteMap
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 3
6
- TINY = 1
6
+ TINY = 2
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -8,14 +8,14 @@ module SiteMap
8
8
 
9
9
  # subbing hash options into view node label, code thanks to kelredd-useful
10
10
  def view_node_label(view_node)
11
- label_string = view_node.label
11
+ label_string = view_node.label.dup
12
12
  view_node_label_options.each do |label_key, value|
13
13
  label_string.gsub!(":#{label_key}", value.to_s)
14
14
  end
15
15
  label_string
16
16
  end
17
17
  def view_node_url(view_node)
18
- eval(view_node.url)
18
+ eval(view_node.url.to_s)
19
19
  end
20
20
  def view_node_visible(view_node)
21
21
  eval(view_node.visible.to_s)
@@ -23,18 +23,40 @@ module SiteMap
23
23
  alias :view_node_visible? :view_node_visible
24
24
 
25
25
  def display_title(options = {})
26
- options = { :connector => " > " }.merge(options)
27
- [ view_node_label(@view_node.parent),
28
- view_node_label(@view_node)
29
- ].join(options[:connector])
26
+ self.display_joined_view_nodes([@view_node.parent, @view_node], options.merge({
27
+ :html => false
28
+ }))
30
29
  end
31
- def display_crumbs
32
-
30
+ def display_crumbs(options = {})
31
+ options[:html] ||= {:class => "breadcrumb"}
32
+ self.display_joined_view_nodes(@view_node.self_and_ancestors, options)
33
33
  end
34
34
  def display_menu
35
35
 
36
36
  end
37
37
 
38
+ def display_joined_view_nodes(view_nodes, options = {})
39
+ options = {
40
+ :connector => " > ",
41
+ :html => true
42
+ }.merge(options)
43
+ display_view_nodes = view_nodes.collect do |view_node|
44
+ if options[:html]
45
+ html_options = options[:html].kind_of?(::Hash) ? options[:html] : {}
46
+ content_tag(:a, view_node_label(view_node), html_options.merge({
47
+ :href => view_node_url(view_node)
48
+ }))
49
+ else
50
+ view_node_label(view_node)
51
+ end
52
+ end
53
+ display_connector = if options[:html]
54
+ content_tag(:span, options[:connector])
55
+ else
56
+ options[:connector]
57
+ end
58
+ display_view_nodes.join(display_connector)
59
+ end
38
60
 
39
61
  end
40
62
  end
@@ -69,6 +69,7 @@ module SiteMap
69
69
  def children
70
70
  @view_nodes
71
71
  end
72
+ alias_method :views, :children
72
73
  def ancestors
73
74
  unless @ancestors
74
75
  node, @ancestors = self, []
@@ -80,12 +81,24 @@ module SiteMap
80
81
  def self_and_ancestors
81
82
  @with_ancestors ||= self.ancestors.dup.push(self)
82
83
  end
84
+ alias_method :sna, :self_and_ancestors
83
85
  def siblings
84
86
  @siblings ||= (self.self_and_siblings - [self])
85
87
  end
86
88
  def self_and_siblings
87
89
  @with_siblings ||= self.parent.children
88
90
  end
91
+ alias_method :sns, :self_and_siblings
92
+ def previous_sibling
93
+ sns[sns.index(self)-1]
94
+ end
95
+ alias_method :previous_view, :previous_sibling
96
+ alias_method :prev, :previous_sibling
97
+ def next_sibling
98
+ sns[(idx = sns.index(self)+1) == sns.length ? 0 : idx]
99
+ end
100
+ alias_method :next_view, :next_sibling
101
+ alias_method :next, :next_sibling
89
102
 
90
103
  TYPES.each do |node_type|
91
104
  self.send(:define_method, "#{node_type}?", lambda{ self.node_type == node_type })
@@ -102,14 +115,12 @@ module SiteMap
102
115
 
103
116
  def default_label
104
117
  case(@node_type)
105
- when :group
106
- self.title_string(@index)
107
118
  when :collection
108
119
  self.resource_label((@action == :new ? self.single_string : self.title_string))
109
120
  when :member
110
121
  self.resource_label(self.single_string)
111
122
  else
112
- @index.to_s.titleize
123
+ self.title_string(@index)
113
124
  end
114
125
  end
115
126
  def default_url
@@ -121,7 +132,7 @@ module SiteMap
121
132
  when :member
122
133
  self.resource_url
123
134
  else
124
- "/#{@index.to_s.underscore.dasherize}"
135
+ "/#{@index.to_s.downcase.gsub(/[^a-z0-9\-_\+]+/, '-')}"
125
136
  end
126
137
  end
127
138
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Collin Redding
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 -05:00
17
+ date: 2010-04-19 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency