navGATE 0.1.02
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Manifest +11 -0
- data/Rakefile +15 -0
- data/app/controller/application_controller.rb +9 -0
- data/app/helpers/application_helper.rb +5 -0
- data/config/build_menu.yml +11 -0
- data/config/initializers/build_menu.rb +26 -0
- data/init.rb +1 -0
- data/lib/navgate.rb +119 -0
- data/lib/navgate/base.rb +35 -0
- data/navGATE.gemspec +20 -0
- data/readme.rdoc +94 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e66426dd904e86b05d16a07ca50c75e047bd37d
|
4
|
+
data.tar.gz: b5e25262592f619b2a80224ab5b40afe68f76ec7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abc92a0bd85c1823e80cb7357062c1db423c4c090f1b414fbf26b97dc764573631c99751af96429a53f0c2233fec403171147720f7882b66fc03fc5a9715ae12
|
7
|
+
data.tar.gz: 37cbcf0c6fd5bb5fba2d1033e657fac1a537fd55c0155004eff4f75b346a0fd114e2108b31d4f188e1a9738f789d6d2b227d09b780db926336a2149e41b04873
|
data/Manifest
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
|
6
|
+
Echoe.new('navGATE','0.1.02') do |p|
|
7
|
+
p.description = "Allows the easy creation of menus with config files"
|
8
|
+
p.url = "https://github.com/Thermatix/navGATE"
|
9
|
+
p.author = "Martin Becker"
|
10
|
+
p.email = "mbeckerwork@gmail.com"
|
11
|
+
p.ignore_pattern = []
|
12
|
+
p.development_dependencies =[]
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'navgate'
|
2
|
+
#building menu object from scratch
|
3
|
+
# NAVGATE = Navgate.new do |build|
|
4
|
+
# build.navs = [ Navgate::Builder.new do |options|
|
5
|
+
# options[:selection] = %w(selection site_settings users images misc)
|
6
|
+
# options[:namespace] = 'admin'
|
7
|
+
# options[:controller] = 'admin_panel'
|
8
|
+
# end
|
9
|
+
# ]
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
#building menu object from database fields be sure to pass it as {Model_name: field}
|
13
|
+
# NAVGATE = Navgate.new do |build|
|
14
|
+
# build.navs = [ Navgate::Builder.new do |options|
|
15
|
+
# options[:selection] = {categories: :title }
|
16
|
+
# options[:prefix] = 'shop_category'
|
17
|
+
# options[:controller] = 'front_page'
|
18
|
+
# options[:by_id] = true
|
19
|
+
# end
|
20
|
+
# ]
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
#building from yaml file, look through the yaml file for an example
|
24
|
+
# NAVGATE = Navgate.new do |build|
|
25
|
+
# build.navs = "#{Rails.root}/config/build_menu.yml"
|
26
|
+
# end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'navgate'
|
data/lib/navgate.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'navgate/base'
|
2
|
+
class Navgate
|
3
|
+
class Builder < Base
|
4
|
+
|
5
|
+
def render_it_with(options)
|
6
|
+
options_to_render = ""
|
7
|
+
if options
|
8
|
+
options.each do |key,value|
|
9
|
+
options_to_render += ("#{key}=#{value}" + " ") unless ignoring key
|
10
|
+
end
|
11
|
+
end
|
12
|
+
style = styling(options)
|
13
|
+
@text_to_render = ""
|
14
|
+
if !self.by_id
|
15
|
+
self.selection.each do |select|
|
16
|
+
wrap_with options[:wrap] do
|
17
|
+
@text_to_render += "<a href=\"#{path_for(select)}\" #{options_to_render}>#{select}</a>#{style}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
self.selection.each_with_index do |select,i|
|
22
|
+
wrap_with options[:wrap] do
|
23
|
+
@text_to_render += "<a href=\"#{path_for(self.by_id[i])}\" #{options_to_render}>#{select}</a>#{style}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
@text_to_render
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def wrap_with tag, &block
|
32
|
+
if tag.is_a?(Array)
|
33
|
+
tag_beggining = "#{tag[0]} class='#{tag[1]}'"
|
34
|
+
tag_end = tag[0]
|
35
|
+
else
|
36
|
+
tag_beggining = tag
|
37
|
+
tag_end = tag
|
38
|
+
end
|
39
|
+
if tag
|
40
|
+
@text_to_render += "<#{tag_beggining}>"
|
41
|
+
yield
|
42
|
+
@text_to_render += "</#{tag_end}>"
|
43
|
+
else
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
def path_for link_to
|
51
|
+
if self.namespace
|
52
|
+
return "/#{self.namespace}/#{link_to}"
|
53
|
+
elsif self.prefix
|
54
|
+
return "/#{self.prefix}/#{link_to}"
|
55
|
+
else
|
56
|
+
return "/#{link_to}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def styling options
|
61
|
+
if options
|
62
|
+
return "<br>" if options[:styling] == :verticle
|
63
|
+
return options[:styling] if options[:styling]
|
64
|
+
end
|
65
|
+
" "
|
66
|
+
end
|
67
|
+
|
68
|
+
def ignoring k
|
69
|
+
[:styling,:wrap].include?(k)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
attr_accessor :controllers, :navs
|
75
|
+
|
76
|
+
def initialize
|
77
|
+
self.controllers = Rails.application.routes.routes.map do |route|
|
78
|
+
route.defaults[:controller]
|
79
|
+
end.uniq.compact
|
80
|
+
yield(self)
|
81
|
+
raise TypeError, "Expected Navgate:Builder or string" unless not_bad_type?(self.navs)
|
82
|
+
if self.navs.is_a?(String)
|
83
|
+
setup = YAML.load_file(self.navs)
|
84
|
+
temp = []
|
85
|
+
setup.each do |menu|
|
86
|
+
temp.push(Navgate::Builder.new do |options|
|
87
|
+
options[:selection] = menu[1]['selection'].split(" ")
|
88
|
+
options[:default] = menu[1]['default'] || nill
|
89
|
+
options[:namespace] = menu[1]['namespace'] || nil
|
90
|
+
options[:prefix] = menu[1]['prefix'] || nil
|
91
|
+
options[:controller] = menu[1]['controller'] || nil
|
92
|
+
end
|
93
|
+
)
|
94
|
+
end
|
95
|
+
self.navs = temp
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
def render_nav params, options
|
102
|
+
select_nav(params[:controller]).render_it_with(options).html_safe
|
103
|
+
end
|
104
|
+
|
105
|
+
def select params
|
106
|
+
nav = select_nav(params[:controller])
|
107
|
+
return params[:selection] ? params[:selection] : nav.default
|
108
|
+
end
|
109
|
+
private
|
110
|
+
def select_nav controller
|
111
|
+
self.navs.each do |nav|
|
112
|
+
return nav if nav.controller == controller
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def not_bad_type? navs
|
117
|
+
navs.is_a?(String) || navs.map{ |n| n.is_a?(Navgate::Builder)}.any?
|
118
|
+
end
|
119
|
+
end
|
data/lib/navgate/base.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Base
|
2
|
+
attr_accessor :selection, :default, :namespace, :controller, :prefix, :by_id
|
3
|
+
|
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}/" : ""
|
17
|
+
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))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
raise TypeError, " Selection was a #{selection.class}, expecting a (Array,Hash)"
|
31
|
+
end
|
32
|
+
output
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/navGATE.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "navGATE"
|
5
|
+
s.version = "0.1.02"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Martin Becker"]
|
9
|
+
s.date = "2013-10-14"
|
10
|
+
s.description = "Allows the easy creation of menus with config files"
|
11
|
+
s.email = "mbeckerwork@gmail.com"
|
12
|
+
s.extra_rdoc_files = ["lib/navgate.rb", "lib/navgate/base.rb"]
|
13
|
+
s.files = ["Manifest", "Rakefile", "app/controller/application_controller.rb", "app/helpers/application_helper.rb", "config/build_menu.yml", "config/initializers/build_menu.rb", "init.rb", "lib/navgate.rb", "lib/navgate/base.rb", "navGATE.gemspec", "readme.rdoc"]
|
14
|
+
s.homepage = "https://github.com/Thermatix/navGATE"
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "navGATE", "--main", "readme.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = "navgate"
|
18
|
+
s.rubygems_version = "2.0.6"
|
19
|
+
s.summary = "Allows the easy creation of menus with config files"
|
20
|
+
end
|
data/readme.rdoc
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
= navGATE
|
2
|
+
|
3
|
+
This gem is provided as is, please read through the intializer file "build_menu.rb" for examples on how you can use this gem to build a nav menu
|
4
|
+
|
5
|
+
note: this is my first gem, I am still trying to set it up, so be carefull when downloading this.
|
6
|
+
|
7
|
+
==Building the menu
|
8
|
+
|
9
|
+
When building the menu there are multiple options available, building the menu is done in an initializer file in the configs directory.
|
10
|
+
|
11
|
+
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.
|
12
|
+
|
13
|
+
===Options
|
14
|
+
|
15
|
+
selection: This is used to build the menu options.
|
16
|
+
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
|
17
|
+
|
18
|
+
default: 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 defualt selection, if no string is passed then the first item from selection is used.
|
19
|
+
|
20
|
+
prefix: 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". This is not for namespacing, for namespacing see the namespace option.
|
21
|
+
|
22
|
+
namespace: This is used for when you have namespacing. It works like prefix however unlike prefix it will also search for the controller within the namespace unlike prefix which doesn't.
|
23
|
+
|
24
|
+
controller: This is used to match the menu to a controller, when deciding which menu to render, it matches this attribute to the current controller. If you have namespacing, the 'namespace' options MUST be used before the 'controller' option otherwise it won't recognise the namespace
|
25
|
+
|
26
|
+
by_id: 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.
|
27
|
+
|
28
|
+
examples:
|
29
|
+
|
30
|
+
===Building menu object from scratch
|
31
|
+
The default option doesn't have to be the first in the selection list.
|
32
|
+
NAVGATE = Navgate.new do |build|
|
33
|
+
build.navs = [ Navgate::Builder.new do |options|
|
34
|
+
options[:selection] = %w(selection site_settings users images misc)
|
35
|
+
options[:default] = 'users'
|
36
|
+
options[:namespace] = 'admin'
|
37
|
+
options[:controller] = 'admin_panel'
|
38
|
+
end
|
39
|
+
]
|
40
|
+
end
|
41
|
+
|
42
|
+
===Building menu object from database fields
|
43
|
+
Be sure to pass it as {model_name: :field}
|
44
|
+
|
45
|
+
NAVGATE = Navgate.new do |build|
|
46
|
+
build.navs = [ Navgate::Builder.new do |options|
|
47
|
+
options[:selection] = {categories: :title }
|
48
|
+
options[:prefix] = 'shop_category'
|
49
|
+
options[:controller] = 'front_page'
|
50
|
+
options[:by_id] = true
|
51
|
+
end
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
=== Using a yml file to build the menu
|
57
|
+
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 dirrectory called "build_menu.yml".
|
58
|
+
when using this method you are unable to use a database model to create the menu.
|
59
|
+
|
60
|
+
===Building from yaml file,
|
61
|
+
Initializing the object:
|
62
|
+
NAVGATE = Navgate.new do |build|
|
63
|
+
build.navs = "#{Rails.root}/config/build_menu.yml"
|
64
|
+
end
|
65
|
+
The yaml file:
|
66
|
+
nav_1:
|
67
|
+
selection: welcome about_us gallery
|
68
|
+
default: welcome
|
69
|
+
prefix: main
|
70
|
+
namespace: front_end
|
71
|
+
controller: front_page
|
72
|
+
nav_2:
|
73
|
+
selection: settings users misc
|
74
|
+
default: settings
|
75
|
+
namespace: back_end
|
76
|
+
controller: admin_panel
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
==Rendering the menu
|
81
|
+
|
82
|
+
To render the menu use the provided helper "render_navigation(options)"
|
83
|
+
options is a hash that is used to build any html options you might want such as
|
84
|
+
class, it can also take two extra options, 'styling:' and 'wrap:'.
|
85
|
+
|
86
|
+
===Options
|
87
|
+
|
88
|
+
Styling: This is how the navigation can be styled it can either be ':verticle' or a character that you wish to use for spacing such as '|' or ':' and so on, it can only be verticle or a spaceing character.
|
89
|
+
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.
|
90
|
+
|
91
|
+
example:
|
92
|
+
render_navigation({"class" => "'nav button'",styling: :verticle, wrap: ['li','test']}) %>
|
93
|
+
|
94
|
+
note: class has to be in "" due to it being a keyword in ruby.
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: navGATE
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.02
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Becker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allows the easy creation of menus with config files
|
14
|
+
email: mbeckerwork@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files:
|
18
|
+
- lib/navgate.rb
|
19
|
+
- lib/navgate/base.rb
|
20
|
+
files:
|
21
|
+
- Manifest
|
22
|
+
- Rakefile
|
23
|
+
- app/controller/application_controller.rb
|
24
|
+
- app/helpers/application_helper.rb
|
25
|
+
- config/build_menu.yml
|
26
|
+
- config/initializers/build_menu.rb
|
27
|
+
- init.rb
|
28
|
+
- lib/navgate.rb
|
29
|
+
- lib/navgate/base.rb
|
30
|
+
- navGATE.gemspec
|
31
|
+
- readme.rdoc
|
32
|
+
homepage: https://github.com/Thermatix/navGATE
|
33
|
+
licenses: []
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --line-numbers
|
38
|
+
- --inline-source
|
39
|
+
- --title
|
40
|
+
- navGATE
|
41
|
+
- --main
|
42
|
+
- readme.rdoc
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project: navgate
|
57
|
+
rubygems_version: 2.0.6
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Allows the easy creation of menus with config files
|
61
|
+
test_files: []
|