navGATE 0.1.03 → 0.1.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/navgate/base.rb CHANGED
@@ -1,35 +1,33 @@
1
- class Base
2
- attr_accessor :selection, :default, :namespace, :controller, :prefix, :by_id
1
+ module NavGate
2
+ class Base
3
+ attr_accessor :selection, :default, :prefix, :controller, :by_id, :css_class, :css_selected
3
4
 
4
- def initialize(&block)
5
- options = {selection: nil,default: nil, controller: nil, namespace: nil}
6
- yield(options)
7
- self.selection = pull_data(options[:selection])
8
- self.default = options[:default] || self.selection.first
9
- self.namespace = options[:namespace]
10
- self.prefix = options[:prefix]
11
- self.controller = "#{namespace?}#{options[:controller]}"
12
- self.by_id = pull_data({options[:selection].to_a.first.first => :id }) if options[:by_id]
13
- end
14
- private
15
- def namespace?
16
- self.namespace ? "#{self.namespace}/" : ""
5
+ def initialize(&block)
6
+ options = {selection: nil,default: nil, controller: nil, css_class: nil, css_selected: nil}
7
+ yield(options)
8
+ self.selection = pull_data(options[:selection])
9
+ self.default = options[:default] || self.selection.first
10
+ self.prefix = options[:prefix]
11
+ self.controller = options[:controller]
12
+ self.by_id = pull_data({options[:selection].to_a.first.first => :id }) if options[:by_id]
13
+ self.css_class = options[:css_class]
14
+ self.css_selected = options[:css_selected]
17
15
  end
18
-
19
- def pull_data selection
20
- if selection.is_a?(Array)
21
- output = selection
22
- elsif selection.is_a?(Hash)
23
- output = []
24
- selection.each do |key,value|
25
- key.to_s.singularize.classify.constantize.all.each do |item|
26
- output.push(item.send(value))
16
+ private
17
+ def pull_data selection
18
+ if selection.is_a?(Array)
19
+ output = selection
20
+ elsif selection.is_a?(Hash)
21
+ output = []
22
+ selection.each do |key,value|
23
+ key.to_s.singularize.classify.constantize.all.each do |item|
24
+ output.push(item.send(value))
25
+ end
27
26
  end
27
+ else
28
+ raise TypeError, " Selection was a #{selection.class}, expecting a (Array,Hash)"
28
29
  end
29
- else
30
- raise TypeError, " Selection was a #{selection.class}, expecting a (Array,Hash)"
30
+ output
31
31
  end
32
- output
33
- end
34
-
32
+ end
35
33
  end
@@ -0,0 +1,96 @@
1
+ module NavGate
2
+ class Builder < Base
3
+
4
+ def render_it_with(options,selected)
5
+ options_to_render = ""
6
+ if self.css_class
7
+ if options && options[:class]
8
+ options[:class] = self.css_class
9
+ else
10
+ options = { class: self.css_class}
11
+ end
12
+ end
13
+ if options
14
+ options.each do |key,value|
15
+ options_to_render += ("#{key}='#{value}'" + " ") unless ignoring key
16
+ end
17
+ end
18
+ style = styling(options)
19
+ @text_to_render = ""
20
+ selected.gsub!('/',"")
21
+ if !self.by_id
22
+ self.selection.each do |select|
23
+ @text_to_render += select_text_for(select,selected,options[:wrap],options_to_render,style)
24
+ end
25
+ else
26
+ self.selection.each_with_index do |select,i|
27
+ @text_to_render += select_text_for(select,selected,options[:wrap],options_to_render,style,i)
28
+ end
29
+ end
30
+ @text_to_render
31
+ end
32
+
33
+ private
34
+ def select_text_for select, selected, wrap, options_to_render, style, id=nil
35
+ return_temp = ""
36
+ wrap_with(wrap) do
37
+ if select != id && select != selected
38
+ return_temp = generate_text(select,options_to_render,style,id)
39
+ else
40
+ if self.css_selected
41
+ if options_to_render =~ /class='.*'/
42
+ temp = options_to_render.gsub(/class='.*'/,"class='#{self.css_selected}'")
43
+ return_temp = generate_text(select,temp,style,id)
44
+ else
45
+ temp = options_to_render + "class='#{self.css_selected}'"
46
+ return_temp = generate_text(select,temp,style,id)
47
+ end#select which version to use
48
+ else
49
+ return_temp = ""
50
+ end #render nothing or css_selected
51
+ end #for select if
52
+ end #for wrap method
53
+ return_temp
54
+ end #for method
55
+ def generate_text(select,options_to_render,style,id = nil)
56
+ "<a href=\"#{path_for(id || select)}\" #{options_to_render}>#{select.gsub('_'," ")}</a>#{style}"
57
+ end
58
+ def wrap_with tag, &block
59
+ if tag.is_a?(Array)
60
+ tag_beggining = "#{tag[0]} class='#{tag[1]}'"
61
+ tag_end = tag[0]
62
+ else
63
+ tag_beggining = tag
64
+ tag_end = tag
65
+ end
66
+ if tag
67
+ @text_to_render += "<#{tag_beggining}>"
68
+ yield
69
+ @text_to_render += "</#{tag_end}>"
70
+ else
71
+ yield
72
+ end
73
+ end
74
+
75
+ def path_for link_to
76
+ if self.prefix
77
+ return "/#{self.prefix}/#{link_to.downcase}"
78
+ else
79
+ return "/#{link_to.downcase}"
80
+ end
81
+ end
82
+
83
+ def styling options
84
+ if options
85
+ return "<br>" if options[:styling] == :vertical
86
+ return options[:styling] if options[:styling]
87
+ end
88
+ " "
89
+ end
90
+
91
+ def ignoring k
92
+ [:styling,:wrap].include?(k)
93
+ end
94
+
95
+ end
96
+ end
@@ -0,0 +1,66 @@
1
+ module NavGate
2
+ class Navigation
3
+
4
+
5
+
6
+ def select selection, controller
7
+
8
+ split_controller = controller.split('/').last
9
+ if selection
10
+ selection
11
+ else
12
+ if config.ignoring.include?(split_controller)
13
+ nil
14
+ else
15
+ nav_cache(split_controller).default.to_s
16
+ end
17
+ end
18
+ end
19
+
20
+
21
+
22
+ def render_nav selection, controller, options
23
+ split_controller = controller.split('/').last
24
+ if config.ignoring.include?(split_controller)
25
+ nil
26
+ else
27
+ nav = nav_cache(split_controller).render_it_with(options,selection).html_safe
28
+ nav
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def config
35
+ NavGate.configuration
36
+ end
37
+
38
+ def nav_cache controller
39
+ if @selected_nav
40
+ if @selected_nav.controller.is_a?(Array)
41
+ return @selected_nav if @selected_nav.controller.include?(controller)
42
+ else
43
+ return @selected_nav unless @selected_nav.controller != controller
44
+ end
45
+ @selected_nav = nil
46
+ nav_cache(controller)
47
+ else
48
+ @selected_nav ||= select_nav(controller)
49
+ end
50
+ end
51
+
52
+ def select_nav controller
53
+ nav_to_return = nil
54
+ config.navs.each do |nav|
55
+ if nav.controller.is_a?(String)
56
+ nav_to_return = nav if (nav.controller) == controller
57
+ elsif nav.controller.is_a?(Array)
58
+ nav_to_return = nav if nav.controller.include?(controller)
59
+ else
60
+ raise TypeError, "expecting nav.controller to be a String or an Array, got #{nav.controller.class} "
61
+ end
62
+ end
63
+ nav_to_return ? (return nav_to_return) : (raise ArgumentError, "No matching controllers for #{controller}")
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,26 @@
1
+ module NavGate
2
+ module NavGateHelpers
3
+ def NavGateHelpers.included(mod)
4
+ if Module.const_get("Rails").is_a?(Module).inspect
5
+ #with rails
6
+ def make_menu
7
+ @navgate = NAVGATE
8
+ @selected ||= @navgate.select(params[:selection], params[:controller])
9
+ end
10
+ def render_navigation options = nil
11
+ @navgate.render_nav((params[:selection]||request.fullpath), params[:controller], options )
12
+ end
13
+ else
14
+ #without rails
15
+ def make_menu selection, controller
16
+ @navgate = NAVGATE
17
+ @selected ||= @navgate.select(selection, controller)
18
+ end
19
+ def render_navigation controller, options = nil
20
+ @navgate.render_nav( @selected , controller, options )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,26 @@
1
+ module NavGate
2
+ module NavGateHelpers
3
+ def NavGateHelpers.included(mod)
4
+ if Module.const_get("Rails").is_a?(Module).inspect
5
+ #with rails
6
+ def make_menu
7
+ @navgate = NavGate::Navigation
8
+ @selected ||= @navgate.select(params[:selection], params[:controller])
9
+ end
10
+ def render_navigation options = nil
11
+ @navgate.render_nav((params[:selection]||request.fullpath), params[:controller], options )
12
+ end
13
+ else
14
+ #without rails
15
+ def make_menu selection, controller
16
+ @navgate = NavGate::Navigation
17
+ @selected ||= @navgate.select(selection, controller)
18
+ end
19
+ def render_navigation controller, options = nil
20
+ @navgate.render_nav( @selected , controller, options )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
data/lib/readme.rdoc ADDED
@@ -0,0 +1,174 @@
1
+ = navGATE
2
+
3
+ This gem is provided as is.
4
+
5
+ This gem allows for the ease of navigation building, from preset lists, from active model databases (eg, categories), from yaml files; but it's not just
6
+ for the ease of use it's also that you can have multiple navigation menus for differant controllers, or the same menu for differant controllers.
7
+ However you want it, it's up to you.
8
+
9
+ This gem was built with Rails in mind.
10
+
11
+
12
+ lastly the gem is up on rubygems.org
13
+
14
+ ==Setup
15
+ in the Application controller you have to <tt> include NavGateHelpers </tt> first.
16
+
17
+ ===For Rails
18
+ You next have to add a before_filter and helper method to the application controller
19
+
20
+ Just add:
21
+ helper_method :render_navigation
22
+ before_filter :make_menu
23
+
24
+ To your list of filters and helper methods in the application controller, thats it, you can now use the helper method and the gem to build your navigations.
25
+ ==For non Rails
26
+ For non rails version of NavGATE the helpers change, instead they work like so:
27
+
28
+ make_menu(selection, controller)
29
+
30
+ render_navigation(selection, controller, options = nil)
31
+
32
+ You have to pass the controller (or page it matches) and the current selection,
33
+ in rails they would pass automatically as <tt> params[:controller] </tt> and <tt> params[:selection] </tt> respectively (selection being the currently selected nav item).
34
+
35
+ ==Building the menus
36
+
37
+ When building the menu there are multiple options available, building the menu is done in an initializer file in the configs directory.
38
+
39
+ There are several options you can pass through, if you are building the menu with the object builder directly then two options must be present, those being 'selection' and 'controller', the rest are optional.
40
+
41
+ Also note, you can pass multiple Navgate::builders as you need, just match them to there controllers and they should render properly.
42
+ ===Options
43
+
44
+ <b>selection</b>: This is used to build the menu options.
45
+ There are two ways to use this, the first is to use an array of strings containing the menu options a person can select; the second is to pull from a database table, to do this pass a hash with the key being the name of the model and it's value being the field containing it's name
46
+
47
+ <b>Default</b>: This is used to give the menu a default selection for when the user has not selected anything. Pass a string containing the name of the default selection, if no string is passed then the first item from selection is used.
48
+
49
+ <b>prefix</b>: This is used when you have a prefix before the target in the URL, eg: if your links render out as "host.com/books" without a prefix; with a prefix of 'shelf' it will render out as "host.com/shelf/books". Namespacing is ignored within this gem, it only looks at the controller's name and nothing else when controller matching.
50
+
51
+ <b>controller</b>: This is used to match the menu to a controller, when deciding which menu to render, it can also be an array of strings; it matches this attribute to the current controller.
52
+
53
+ <b>by_id</b>: This is used when you are using a database model to build the menu and you want to link with IDs rather then the selection list. To use it simply set it to true.
54
+
55
+ <b>css_class</b>: This is used when you want to hard code the CSS class selector into the menu rather then from the view.
56
+
57
+ <b>css_selected</b>: the css override for the selected that's currently selected. if no override is passed then the link is simply not rendered out, as with css_class it overrides the one passed in the view, but only for the selected link
58
+
59
+ examples:
60
+
61
+ ===Building menu object from scratch
62
+ The default option doesn't have to be the first in the selection list.
63
+ NAVGATE = Navgate.new do |build|
64
+ build.navs = [ Navgate::Builder.new do |options|
65
+ options[:selection] = %w(selection site_settings users images misc)
66
+ options[:default] = 'users'
67
+ options[:prefix] = = 'admin'
68
+ options[:controller] = 'admin_panel'
69
+ options[:css_class] = 'nav button'
70
+ end
71
+ ]
72
+ end
73
+
74
+ ===Building menu object from database fields
75
+ Be sure to pass it as {model_name: :field}.
76
+ Also note you can pass an array of controllers as well as just a string of one controller which in this case is done via a split command, %w() also works
77
+
78
+ NAVGATE = Navgate.new do |build|
79
+ build.navs = [ Navgate::Builder.new do |options|
80
+ options[:selection] = {categories: :title }
81
+ options[:prefix] = 'shop_category'
82
+ options[:controller] = "front_page side_page about_page".split(" ")
83
+ options[:by_id] = true
84
+ end
85
+ ]
86
+ end
87
+ ===Building multiple menus
88
+
89
+ NAVGATE = Navgate.new do |build|
90
+ build.navs = [
91
+ Navgate::Builder.new do |options|
92
+ options[:selection] = %w(selection site_settings users images misc)
93
+ options[:default] = 'users'
94
+ options[:prefix] = = 'admin'
95
+ options[:controller] = 'admin_panel'
96
+ options[:css_class] = 'nav button'
97
+ end,
98
+ Navgate::Builder.new do |options|
99
+ options[:selection] = %w(welcome about_us gallery news)
100
+ options[:default] = 'news'
101
+ options[:controller] = 'front_page'
102
+ options[:css_class] = 'nav button'
103
+ end
104
+ ]
105
+ end
106
+
107
+ === Using a yml file to build the menu
108
+ There is also a third option to build the menu, you can use a structured yml file, there is an example yaml file in the config directory called "build_menu.yml".
109
+ when using this method you are unable to use a database model to create the menu.
110
+
111
+ ===Building from yaml file,
112
+ Initializing the object:
113
+ NAVGATE = Navgate.new do |build|
114
+ build.navs = "#{Rails.root}/config/build_menu.yml"
115
+ end
116
+ The yaml file:
117
+ nav_1:
118
+ selection: welcome about_us gallery
119
+ default: welcome
120
+ prefix: main
121
+ controller: front_page
122
+ nav_2:
123
+ selection: settings users misc
124
+ default: settings
125
+ preix: back_end
126
+ controller: admin_panel
127
+
128
+
129
+ ==Ignoring Controllers
130
+ Sometimes you're going to want to ignore controllers that don't any gui. Doing that is simple, when you're building the menu just pass an Array to build like so
131
+ build.ignoring = ['controllers','to','ignore']
132
+ before or after you pass through the navs.
133
+
134
+ ==Rendering the menu
135
+
136
+ To render the menu use the provided helper <tt>render_navigation(options)</tt>;
137
+ options is a hash that is used to build any html options you might want such as
138
+ 'class='some_css_class', it can also take two extra options, 'styling:' and 'wrap:'.
139
+
140
+ ===Options
141
+
142
+ Styling: This is how the navigation can be styled, it can either be ':vertical’ or a character that you wish to use for spacing such as '|' or ':' and so on, it can only be vertical or a spacing character.
143
+
144
+ Wrap: This allows you to wrap each link in a html tag, wrap can itself take two differant options, either a string containing the tag's name (without "<>", only the tag name) or an Array containing the tag name and it's class.
145
+
146
+ example:
147
+ render_navigation({:class => "'nav button'", styling: :vertical, wrap: ['li','test']}) %>
148
+
149
+ note: There is no point in passing a class here if you have one set when you first build the menu, it will just be overridden,
150
+ unless of course you're using multiple menus and some of them don't have css overides then they will take this option up.
151
+
152
+ ==Using the selection to automatically render a matching partial
153
+
154
+ naveGATE is set up so you can use it to render out a partial using <tt>@selected</tt>, to do this you have to pass a route param of <tt>:selection</tt>
155
+ in the route, then you can use <tt><%= render @selected %></tt> to automatically select either the default selection or the current selection.
156
+
157
+ That said you don't have use this feature, it will still route to whatever url you set up as a normal url, but <tt>@selected</tt> won't work without <tt>:selection</tt>
158
+ example:
159
+
160
+ routes.rb
161
+ get "/:selection", to: "front_page#index"
162
+ root to: "front_page#index"
163
+
164
+ front_page/index.html.erb
165
+ <%= render @selected %>
166
+
167
+ resulting url
168
+ host.com/books
169
+ host.com/games
170
+
171
+ routes to the root but the partials rendered would be respectively
172
+ _books.html.erb
173
+ _games.html.erb
174
+