trejo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in trejo.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Trejo
2
+
3
+ Trejo provides `nav_item` helper to render navigation links. An `active` class is applied to the link when the requested path matches the link url.
4
+
5
+ ## Installation
6
+
7
+ Add it to your Gemfile and run the `bundle` command to install it.
8
+
9
+ ```ruby
10
+ gem 'trejo'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ If the current path is `/home`, then the following
16
+
17
+ ```
18
+ <nav>
19
+ <%= nav_item 'Home', '/home' %>
20
+ <%= nav_item 'Blog', '/blog' %>
21
+ </nav>
22
+ ```
23
+
24
+ generates
25
+
26
+ ```
27
+ <nav>
28
+ <a href='/home' class='active'>Home</a>
29
+ <a href='/blog'>Blog</a>
30
+ </nav>
31
+ ```
32
+
33
+ Trejo assumes that the link url is the root of the resource, and ignores query parameters by default. So the above example also works if the requested path is `/home/index?foo=bar`.
34
+
35
+ The default css class applied to the link is `active`. This can be overridden by passing a `class` option with the desired class.
36
+
37
+ ```
38
+ <%= nav_item 'Home', '/home', class: 'current-section' %>
39
+ ```
40
+
41
+ generates
42
+
43
+ ```
44
+ <a href='/home' class='current-section'>Home</a>
45
+ ```
46
+
47
+ If you need more granularity in the criteria for determining an active link, you can supply a regular expression in the `selected ` option. So if the current path is `/home?foo=bar`, then the following
48
+
49
+ ```
50
+ <%= nav_item 'Home', '/home?foo=bar', selected: /^\/home\?foo=\w+/ %>
51
+ ```
52
+
53
+ generates
54
+
55
+ ```
56
+ <a class='active'>Home</a>
57
+ ```
58
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ require 'trejo/view_helpers'
2
+
3
+ module Trejo
4
+ class Railtie < Rails::Railtie
5
+ initializer 'trejo.view_helpers' do
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Trejo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ module Trejo
2
+ module ViewHelpers
3
+ def nav_item name, url, options={}
4
+ current_path = request.fullpath
5
+
6
+ selected = if options[:selected]
7
+ current_path =~ options[:selected]
8
+ else
9
+ current_path =~ /^#{Regexp.escape(url)}.*(?:\?.*)?/
10
+ end
11
+
12
+ link_class = options[:class] || 'active' if selected
13
+ link_to name, url, class: link_class
14
+ end
15
+ end
16
+ end
data/lib/trejo.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'trejo/version'
2
+ require 'trejo/railtie' if defined?(Rails)
@@ -0,0 +1,2 @@
1
+ require 'action_view'
2
+ require 'trejo/view_helpers'
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ include ActionView::Helpers::UrlHelper
4
+ class TrejoHelperTestClass; end
5
+
6
+ describe Trejo::ViewHelpers do
7
+ before do
8
+ @trejo = TrejoHelperTestClass.new
9
+ @trejo.extend Trejo::ViewHelpers
10
+ end
11
+
12
+ describe '#nav_item' do
13
+ context 'by default' do
14
+ context 'when the link path matches the requested path' do
15
+ before do
16
+ request = stub fullpath: '/home'
17
+ @trejo.stub(:request).and_return(request)
18
+
19
+ @nav_item = @trejo.nav_item('Home', '/home')
20
+ end
21
+
22
+ it 'returns a selected nav item' do
23
+ expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
24
+ end
25
+ end
26
+
27
+ context 'when the link path does not match the requested path' do
28
+ before do
29
+ request = stub fullpath: '/home'
30
+ @trejo.stub(:request).and_return(request)
31
+
32
+ @nav_item = @trejo.nav_item('Blog', '/blog')
33
+ end
34
+
35
+ it 'returns an unselected nav item' do
36
+ expect(@nav_item).to eq('<a href="/blog">Blog</a>')
37
+ end
38
+ end
39
+
40
+ context 'ignores query parameters' do
41
+ context 'and when the link path matches the requested path' do
42
+ before do
43
+ request = stub fullpath: '/home?page=2'
44
+ @trejo.stub(:request).and_return(request)
45
+
46
+ @nav_item = @trejo.nav_item('Home', '/home')
47
+ end
48
+
49
+ it 'returns a selected nav item' do
50
+ expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
51
+ end
52
+ end
53
+ end
54
+
55
+ context 'assumes the link path is the root' do
56
+ context 'and when the requested path includes a nested path' do
57
+ before do
58
+ request = stub fullpath: '/home/index'
59
+ @trejo.stub(:request).and_return(request)
60
+ @nav_item = @trejo.nav_item('Home', '/home')
61
+ end
62
+
63
+ it 'returns a selected nav item' do
64
+ expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
65
+ end
66
+ end
67
+
68
+ context 'and when the requested path includes a different root path' do
69
+ before do
70
+ request = stub fullpath: '/blog/index'
71
+ @trejo.stub(:request).and_return(request)
72
+ @nav_item = @trejo.nav_item('Home', '/home')
73
+ end
74
+
75
+ it 'returns an unselected nav item' do
76
+ expect(@nav_item).to eq('<a href="/home">Home</a>')
77
+ end
78
+ end
79
+
80
+ context 'and when query parameters are present' do
81
+ before do
82
+ request = stub fullpath: '/home/index?foo=bar&walter=sobchak'
83
+ @trejo.stub(:request).and_return(request)
84
+ @nav_item = @trejo.nav_item('Home', '/home')
85
+ end
86
+
87
+ it 'returns a selected nav item' do
88
+ expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ context 'when the selection criteria is a regex' do
95
+ before do
96
+ request = stub fullpath: '/home?page=2'
97
+ @trejo.stub(:request).and_return(request)
98
+ end
99
+
100
+ context 'when the requested path matches the criteria' do
101
+ before do
102
+ @nav_item = @trejo.nav_item('Home', '/home', selected: /^\/home/)
103
+ end
104
+
105
+ it 'returns a selected nav item' do
106
+ expect(@nav_item).to eq('<a href="/home" class="active">Home</a>')
107
+ end
108
+ end
109
+
110
+ context 'when the requested path does not match the criteria' do
111
+ before do
112
+ @nav_item = @trejo.nav_item('Blog', '/blog', selected: /^\/blog/)
113
+ end
114
+
115
+ it 'returns a unselected nav item' do
116
+ expect(@nav_item).to eq('<a href="/blog">Blog</a>')
117
+ end
118
+ end
119
+ end
120
+
121
+ context 'when a css class is supplied' do
122
+ before do
123
+ request = stub fullpath: '/home'
124
+ @trejo.stub(:request).and_return(request)
125
+
126
+ @nav_item = @trejo.nav_item('Home', '/home', class: 'current-section')
127
+ end
128
+
129
+ it 'returns a selected nav item with that class' do
130
+ expect(@nav_item).to eq('<a href="/home" class="current-section">Home</a>')
131
+ end
132
+ end
133
+ end
134
+ end
data/trejo.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "trejo/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "trejo"
7
+ s.version = Trejo::VERSION
8
+ s.authors = ["Boram Yoon"]
9
+ s.email = ["boram@me.com"]
10
+ s.homepage = "http://github.com/boram/trejo"
11
+ s.summary = %q{Navigation link helper}
12
+ s.description = %q{Navigation links with active states based on current path}
13
+
14
+ s.rubyforge_project = "trejo"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.required_ruby_version = '>= 1.9.3'
22
+ s.add_dependency 'actionpack', ['>= 3.0.0']
23
+ s.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trejo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Boram Yoon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Navigation links with active states based on current path
47
+ email:
48
+ - boram@me.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - CHANGELOG
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - lib/trejo.rb
60
+ - lib/trejo/railtie.rb
61
+ - lib/trejo/version.rb
62
+ - lib/trejo/view_helpers.rb
63
+ - spec/spec_helper.rb
64
+ - spec/trejo/view_helpers_spec.rb
65
+ - trejo.gemspec
66
+ homepage: http://github.com/boram/trejo
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: trejo
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Navigation link helper
90
+ test_files:
91
+ - spec/spec_helper.rb
92
+ - spec/trejo/view_helpers_spec.rb