nav_helper 0.1.0
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.
- checksums.yaml +7 -0
- data/.editorconfig +11 -0
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +53 -0
- data/Rakefile +6 -0
- data/lib/nav_helper/version.rb +3 -0
- data/lib/nav_helper.rb +33 -0
- data/nav_helper.gemspec +24 -0
- data/spec/helpers/nav_helper_spec.rb +54 -0
- data/spec/micro_dummy_app.rb +14 -0
- data/spec/spec_helper.rb +5 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc636af0eafe2786f497ffe73111698409368252
|
4
|
+
data.tar.gz: c2b2f38b70c8272a6a7aaed53a9154b77b53528f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6d761b6b78fbcc96dd89109be5058382abc406e7a1a8443919dc26e181b2855e0d8439bb90e437ca2831c064a6c6aa15d68a38e6c906e03bc98b242ae260694
|
7
|
+
data.tar.gz: 00d08c1bf7eafb3772fc64d9e17fee3a6cb5830de5d73b86e00a46bcb519a552027690a4d4c4aebe787291858f84c6f556e9b927fb2dff4209c3c8605b329ae1
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# NavHelper
|
2
|
+
|
3
|
+
Helper for building nav links.
|
4
|
+
|
5
|
+
By default, helper generates link, wrapped with `<li>` and sets class "active", if link path is path of the current page.
|
6
|
+
```html
|
7
|
+
<ul>
|
8
|
+
<%= nav_item('Home', '/') %>
|
9
|
+
<%= nav_item('Contacts', '/contacts') %>
|
10
|
+
<%= nav_item('About', '/about') %>
|
11
|
+
</ul>
|
12
|
+
```
|
13
|
+
|
14
|
+
Also, you can customize wrapper tag and navigation root path:
|
15
|
+
```html
|
16
|
+
<div class="nav">
|
17
|
+
<%= nav_item('Posts', admin_posts_path, tag: :div, root: admin_path) %>
|
18
|
+
<%= nav_item('Users', admin_users_path, tag: :div, root: admin_path) %>
|
19
|
+
</div>
|
20
|
+
```
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'nav_helper'
|
28
|
+
```
|
29
|
+
|
30
|
+
## License
|
31
|
+
|
32
|
+
Copyright (c) 2014 Okhlopkov Anatoly
|
33
|
+
|
34
|
+
MIT License
|
35
|
+
|
36
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
37
|
+
a copy of this software and associated documentation files (the
|
38
|
+
"Software"), to deal in the Software without restriction, including
|
39
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
40
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
41
|
+
permit persons to whom the Software is furnished to do so, subject to
|
42
|
+
the following conditions:
|
43
|
+
|
44
|
+
The above copyright notice and this permission notice shall be
|
45
|
+
included in all copies or substantial portions of the Software.
|
46
|
+
|
47
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
48
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
49
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
50
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
51
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
52
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
53
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/nav_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "nav_helper/version"
|
2
|
+
|
3
|
+
module NavigationHelper
|
4
|
+
def current_path
|
5
|
+
request.path
|
6
|
+
end
|
7
|
+
|
8
|
+
def current_path?(path, root = '/')
|
9
|
+
if path == root
|
10
|
+
current_path == path
|
11
|
+
else
|
12
|
+
(/\A#{path}(\/.*)?\Z/ =~ current_path).present?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def nav_item(title, path = nil, options = nil, &blk)
|
17
|
+
path, options = title, path if block_given?
|
18
|
+
|
19
|
+
options ||= {}
|
20
|
+
options.reverse_merge!(tag: :li, root: nil)
|
21
|
+
|
22
|
+
tag = options.delete(:tag)
|
23
|
+
is_active = current_path?(path, options.delete(:root))
|
24
|
+
|
25
|
+
content_tag(tag, class: (:active if is_active)) do
|
26
|
+
if block_given?
|
27
|
+
link_to(path, options, &blk)
|
28
|
+
else
|
29
|
+
link_to(title, path, options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/nav_helper.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'nav_helper/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'nav_helper'
|
7
|
+
spec.version = NavHelper::VERSION
|
8
|
+
spec.authors = ['Okhlopkov Anatoly']
|
9
|
+
spec.email = ['ohlopkov0211@gmail.com']
|
10
|
+
spec.summary = 'Helper for building nav links.'
|
11
|
+
spec.description = 'Helper for building nav links.'
|
12
|
+
spec.homepage = 'https://github.com/VortexGrenade/nav_helper.git'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
23
|
+
spec.add_development_dependency 'rspec-rails'
|
24
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'nav_helper'
|
3
|
+
|
4
|
+
describe NavigationHelper, type: [:helper] do
|
5
|
+
before { helper.request.path = '/foo/bar' }
|
6
|
+
|
7
|
+
describe '#current_path?' do
|
8
|
+
it 'checks whether given path is the page path or path to a sub-page' do
|
9
|
+
expect(helper.current_path?('/foo')).to be true
|
10
|
+
expect(helper.current_path?('/foo/bar')).to be true
|
11
|
+
|
12
|
+
expect(helper.current_path?('/fo')).to be false
|
13
|
+
expect(helper.current_path?('/bar/baz')).to be false
|
14
|
+
expect(helper.current_path?('/foo/bar/baz')).to be false
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'root' do
|
18
|
+
before { helper.request.path = '/foo' }
|
19
|
+
|
20
|
+
it 'checks whether given path is the page path' do
|
21
|
+
expect(helper.current_path?('/foo', '/foo')).to be true
|
22
|
+
expect(helper.current_path?('/foo/bar', '/foo')).to be false
|
23
|
+
|
24
|
+
expect(helper.current_path?('/fo', '/foo')).to be false
|
25
|
+
expect(helper.current_path?('/bar', '/foo')).to be false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#nav_item' do
|
31
|
+
it 'generates nav item' do
|
32
|
+
expect(helper.nav_item('Foo', '/fo')).to eq(%{<li><a href="/fo">Foo</a></li>})
|
33
|
+
expect(helper.nav_item('Bar', '/foo/bar')).to eq(%{<li class="active"><a href="/foo/bar">Bar</a></li>})
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'allows custom root' do
|
37
|
+
helper.request.path = '/admin/foo/bar'
|
38
|
+
|
39
|
+
expect(helper.nav_item('Foo', '/admin/fo', root: '/admin')).to eq(%{<li><a href="/admin/fo">Foo</a></li>})
|
40
|
+
expect(helper.nav_item('Bar', '/admin/foo/bar', root: '/admin')).to eq(%{<li class="active"><a href="/admin/foo/bar">Bar</a></li>})
|
41
|
+
expect(helper.nav_item('Bar', '/admin', root: '/admin')).to eq(%{<li><a href="/admin">Bar</a></li>})
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'generates nav item with block' do
|
45
|
+
expect(helper.nav_item('/fo') do
|
46
|
+
'FOO'
|
47
|
+
end).to eq "<li><a href=\"/fo\">FOO</a></li>"
|
48
|
+
|
49
|
+
expect(helper.nav_item('/foo/bar') do
|
50
|
+
'BAR'
|
51
|
+
end).to eq "<li class=\"active\"><a href=\"/foo/bar\">BAR</a></li>"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
require 'action_controller/railtie'
|
5
|
+
require 'rspec/rails'
|
6
|
+
|
7
|
+
module Dummy
|
8
|
+
class Application < Rails::Application
|
9
|
+
config.secret_key_base = 'dummy_key'
|
10
|
+
config.eager_load = false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Rails.application.initialize!
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nav_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Okhlopkov Anatoly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Helper for building nav links.
|
70
|
+
email:
|
71
|
+
- ohlopkov0211@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".editorconfig"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/nav_helper.rb
|
83
|
+
- lib/nav_helper/version.rb
|
84
|
+
- nav_helper.gemspec
|
85
|
+
- spec/helpers/nav_helper_spec.rb
|
86
|
+
- spec/micro_dummy_app.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/VortexGrenade/nav_helper.git
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Helper for building nav links.
|
112
|
+
test_files:
|
113
|
+
- spec/helpers/nav_helper_spec.rb
|
114
|
+
- spec/micro_dummy_app.rb
|
115
|
+
- spec/spec_helper.rb
|