site_map 0.2.5 → 0.2.6

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.
@@ -3,6 +3,13 @@ module SiteMap
3
3
 
4
4
  module Mapping
5
5
 
6
+ DEFAULT_ALIAS = {
7
+ :index => [:new, :create],
8
+ :new => [:create],
9
+ :show => [:edit, :update, :destroy],
10
+ :edit => [:update]
11
+ }
12
+
6
13
  def group(new_index, options={})
7
14
  view_node = self.add_node(new_index, :group, options)
8
15
  yield view_node if block_given?
@@ -19,23 +26,28 @@ module SiteMap
19
26
  end
20
27
 
21
28
  def collection(resource, options={})
22
- base_index = "#{resource}__:action"
23
- new_index = base_index.gsub(':action', 'index').to_sym
24
- view_node = self.add_node(new_index, :view, options)
25
- [:new, :create].each do |action|
26
- action_index = base_index.gsub(':action', action.to_s).to_sym
27
- view_node.alias(action_index, new_index)
28
- end
29
+ view_node = self.collection_view(resource, :index, options)
29
30
  yield view_node if block_given?
30
31
  end
32
+ def collection_view(resource, action, options={})
33
+ options.merge!({:resource => resource.to_sym, :action => action.to_sym})
34
+ view_node = self.add_node([resource, action].join('__').to_sym, :collection, options)
35
+ DEFAULT_ALIAS[action].each do |action|
36
+ view_node.alias([resource, action].join('__').to_sym, view_node.index)
37
+ end
38
+ block_given? ? (yield view_node) : view_node
39
+ end
31
40
  def member(resource, options={})
32
- base_index = "#{resource}__:action"
33
- new_index = base_index.gsub(':action', 'show').to_sym
34
- view_node = self.add_node(new_index, :view, options)
35
- [:edit, :update, :destroy].each do |action|
36
- action_index = base_index.gsub(':action', action.to_s).to_sym
37
- view_node.alias(action_index, new_index)
41
+ view_node = self.member_view(resource, :show, options)
42
+ yield view_node if block_given?
43
+ end
44
+ def member_view(resource, action, options={})
45
+ options.merge!({:resource => resource.to_sym, :action => action.to_sym})
46
+ view_node = self.add_node([resource, action].join('__').to_sym, :member, options)
47
+ DEFAULT_ALIAS[action].each do |action|
48
+ view_node.alias([resource, action].join('__').to_sym, view_node.index)
38
49
  end
50
+ block_given? ? (yield view_node) : view_node
39
51
  end
40
52
 
41
53
  protected
@@ -3,7 +3,7 @@ module SiteMap
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 2
6
- TINY = 5
6
+ TINY = 6
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -3,10 +3,22 @@ module SiteMap
3
3
 
4
4
  class ViewNode
5
5
  include SiteMap::Helpers::Mapping
6
- ATTRIBUTES = [ :map, :index, :label, :url, :visible, :parent, :type ]
6
+ ATTRIBUTES = [ :map, :index, :label, :url, :visible, :resource, :parent, :type ]
7
7
  ATTRIBUTES.each{|attribute| attr_reader attribute }
8
8
 
9
- TYPES = [ :view, :group ]
9
+ TYPES = [ :view, :group, :member, :collection ]
10
+ LABEL_ACTION_TEMPLATES = {
11
+ :index => ":resource List",
12
+ :new => "New :resource",
13
+ :show => "::resource_name",
14
+ :edit => "Edit ::resource_name"
15
+ }
16
+ URL_ACTION_TEMPLATES = {
17
+ :index => ":resource_path",
18
+ :new => "new_:resource_path",
19
+ :show => ":resource_path(@:resource)",
20
+ :edit => "edit_:resource_path(@:resource)"
21
+ }
10
22
 
11
23
  def initialize(index, map, type, options={})
12
24
  raise(ArgumentError, "index, map and type arguements required") unless index && map && type
@@ -24,14 +36,10 @@ module SiteMap
24
36
  end
25
37
 
26
38
  def label
27
- @label ? @label : @index.to_s
39
+ @label ? @label : self.default_label
28
40
  end
29
41
  def url
30
- if !@url && self.group?
31
- self.children.empty? ? @url : self.children.first.url
32
- else
33
- @url
34
- end
42
+ @url ? @url : self.default_url
35
43
  end
36
44
  def visible
37
45
  @visible ? @visible : 'true'
@@ -75,6 +83,58 @@ module SiteMap
75
83
 
76
84
  protected
77
85
 
86
+ def default_label
87
+ case(@type)
88
+ when :group
89
+ self.title_string(@index)
90
+ when :collection
91
+ self.resource_label((@action == :new ? self.single_string : self.title_string))
92
+ when :member
93
+ self.resource_label(self.single_string)
94
+ else
95
+ @index.to_s
96
+ end
97
+ end
98
+ def default_url
99
+ case(@type)
100
+ when :group
101
+ self.children.empty? ? @url : self.children.first.url
102
+ when :collection
103
+ self.resource_url
104
+ when :member
105
+ self.resource_url
106
+ else
107
+ @url
108
+ end
109
+ end
110
+
111
+ def resource_label(resource_text)
112
+ LABEL_ACTION_TEMPLATES[@action].gsub(':resource', resource_text)
113
+ end
114
+ def resource_url
115
+ resource_text = if @type == :collection
116
+ parent_sym = if [:member, :collection].include?(self.parent.type)
117
+ self.single_string(self.parent.resource)
118
+ elsif self.parent.type == :group
119
+ self.parent.index
120
+ end
121
+ resource_with_parent = [parent_sym, (@action == :new ? self.single_string : @resource.to_s)].compact.join('_')
122
+ resourced_url = URL_ACTION_TEMPLATES[@action].gsub(':resource', resource_with_parent)
123
+ [resourced_url, ("(@#{parent_sym})" if parent_sym)].compact.join
124
+ else
125
+ URL_ACTION_TEMPLATES[@action].gsub(':resource', self.single_string)
126
+ end
127
+ end
128
+
129
+ def single_string(string = nil)
130
+ string ||= @resource
131
+ string.to_s.respond_to?(:singularize) ? string.to_s.singularize : string.to_s
132
+ end
133
+ def title_string(string = nil)
134
+ string ||= @resource
135
+ string.to_s.respond_to?(:titleize) ? string.to_s.titleize : string.to_s
136
+ end
137
+
78
138
  def view_node_params(new_index, type, options)
79
139
  [new_index, self.map, type, options.merge(:parent => self)]
80
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collin Redding