link_to_active_state 0.1.2
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.
- data/.gitignore +17 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +4 -0
- data/lib/link_to_active_state/railtie.rb +12 -0
- data/lib/link_to_active_state/version.rb +3 -0
- data/lib/link_to_active_state/view_helpers/url_helper.rb +58 -0
- data/lib/link_to_active_state.rb +5 -0
- data/link_to_active_state.gemspec +25 -0
- data/spec/link_to_active_state/view_helpers_spec.rb +7 -0
- data/spec/link_to_active_state_spec.rb +57 -0
- data/spec/spec_helper.rb +7 -0
- data.tar.gz.sig +0 -0
- metadata +122 -0
- metadata.gz.sig +0 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Robert May
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# LinkToActiveState
|
2
|
+
|
3
|
+
[](https://travis-ci.org/robotmay/link_to_active_state) [](https://codeclimate.com/github/robotmay/link_to_active_state) [](https://gemnasium.com/robotmay/link_to_active_state)
|
4
|
+
|
5
|
+
A simple gem to implement active states on links using the standard Rails `link_to` helper.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'link_to_active_state'
|
12
|
+
|
13
|
+
*NOTE*: RubyGems.org is currently in read-only mode, so the gem doesn't yet exist there. Install from the git repo instead:
|
14
|
+
|
15
|
+
gem 'link_to_active_state', git: 'git://github.com/robotmay/link_to_active_state.git', tag: 'v0.1.2'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install link_to_active_state
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
This gem adds a small bit of extra functionality to the default Rails `link_to` view helper. It provides a very simple way of adding classes to links based on the current path.
|
28
|
+
|
29
|
+
### Examples
|
30
|
+
|
31
|
+
Test the path using a string/path helper:
|
32
|
+
```ruby
|
33
|
+
link_to "Account", account_path, active_on: account_path
|
34
|
+
```
|
35
|
+
|
36
|
+
Using a regular expression:
|
37
|
+
```ruby
|
38
|
+
link_to "Account", account_path, active_on: /\/account/i
|
39
|
+
```
|
40
|
+
|
41
|
+
Or a proc:
|
42
|
+
```ruby
|
43
|
+
link_to "Account", account_path, active_on: lambda { request.fullpath == account_path }
|
44
|
+
```
|
45
|
+
|
46
|
+
### Custom options
|
47
|
+
|
48
|
+
By default the class "active" will be added to the existing classes of the link. However you can specify your own:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
link_to "Account", account_path, active_on: /\/account/i, active_state: "highlighted"
|
52
|
+
```
|
53
|
+
|
54
|
+
You can also customise other options by using a proc:
|
55
|
+
```ruby
|
56
|
+
link_to "Account", account_path, active_on: /\/account/i, active_state: lambda { |html_options|
|
57
|
+
html_options.merge({ "data-active" => "true" })
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "link_to_active_state"
|
2
|
+
|
3
|
+
module LinkToActiveState
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "link_to_active_state.view_helpers" do
|
6
|
+
ActiveSupport.on_load(:action_view) do
|
7
|
+
require "link_to_active_state/view_helpers/url_helper"
|
8
|
+
include LinkToActiveState::ViewHelpers::UrlHelper
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'link_to_active_state'
|
3
|
+
|
4
|
+
module LinkToActiveState
|
5
|
+
module ViewHelpers
|
6
|
+
module UrlHelper
|
7
|
+
def self.included(base)
|
8
|
+
base.send(:alias_method_chain, :link_to, :active_state)
|
9
|
+
end
|
10
|
+
|
11
|
+
def link_to_with_active_state(*args, &block)
|
12
|
+
html_options = if block_given?
|
13
|
+
args.second
|
14
|
+
else
|
15
|
+
args[2]
|
16
|
+
end
|
17
|
+
|
18
|
+
if html_options.present? && html_options[:active_on].present?
|
19
|
+
active_on = html_options[:active_on]
|
20
|
+
|
21
|
+
if is_active?(active_on)
|
22
|
+
active_state = html_options[:active_state] || "active"
|
23
|
+
case active_state
|
24
|
+
when Proc
|
25
|
+
html_options.merge(active_state.call(html_options))
|
26
|
+
when String
|
27
|
+
html_options[:class] = merge_class(html_options[:class], active_state)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
html_options.delete(:active_on)
|
32
|
+
html_options.delete(:active_state)
|
33
|
+
end
|
34
|
+
|
35
|
+
link_to_without_active_state(*args, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def is_active?(active_on)
|
40
|
+
case active_on
|
41
|
+
when String
|
42
|
+
request.fullpath == active_on
|
43
|
+
when Array
|
44
|
+
active_on.include?(request.fullpath)
|
45
|
+
when Regexp
|
46
|
+
request.fullpath =~ active_on
|
47
|
+
when Proc
|
48
|
+
active_on.arity == 1 ? active_on.call(request) : active_on.call
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def merge_class(original, new)
|
53
|
+
original ||= ""
|
54
|
+
[original, new].delete_if(&:blank?).join(" ")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'link_to_active_state/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "link_to_active_state"
|
8
|
+
gem.version = LinkToActiveState::VERSION
|
9
|
+
gem.authors = ["Robert May"]
|
10
|
+
gem.email = ["robotmay@gmail.com"]
|
11
|
+
gem.description = %q{A simple gem to implement active states on links using the standard Rails `link_to` helper.}
|
12
|
+
gem.summary = %q{Active states for links using the Rails link_to helper.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.cert_chain = ["gem-public_cert.pem"]
|
21
|
+
gem.signing_key = ENV['PRIVATE_KEY']
|
22
|
+
|
23
|
+
gem.add_development_dependency "rails", [">= 3.2.11"]
|
24
|
+
gem.add_development_dependency "rspec", ["~> 2.12.0"]
|
25
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'link_to_active_state/view_helpers/url_helper'
|
3
|
+
|
4
|
+
module App
|
5
|
+
class Helper < ActionView::Base
|
6
|
+
include LinkToActiveState::ViewHelpers::UrlHelper
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe LinkToActiveState::ViewHelpers::UrlHelper do
|
11
|
+
let(:helper) { App::Helper.new }
|
12
|
+
|
13
|
+
context "integration with ActionView" do
|
14
|
+
it "aliases the original link_to helper" do
|
15
|
+
helper.should respond_to(:link_to_without_active_state)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "responds to link_to_with_active_state" do
|
19
|
+
helper.should respond_to(:link_to_with_active_state)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "aliases link_to_with_active_state to link_to" do
|
23
|
+
lt = helper.link_to "Test", "/", :active_on => lambda { true }
|
24
|
+
ltwas = helper.link_to_with_active_state "Test", "/", :active_on => lambda { true }
|
25
|
+
ltwoas = helper.link_to_without_active_state "Test", "/", :active_on => lambda { true }
|
26
|
+
lt.should eq(ltwas)
|
27
|
+
lt.should_not eq(ltwoas)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "active states on links" do
|
32
|
+
let(:request) do
|
33
|
+
class Request
|
34
|
+
def fullpath
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Request.new
|
39
|
+
end
|
40
|
+
|
41
|
+
before(:each) do
|
42
|
+
helper.stub!(:request).and_return(request)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "adds an active state when the current request path matches" do
|
46
|
+
request.stub!(:fullpath).and_return("/")
|
47
|
+
lt = helper.link_to "Home", "/", :active_on => "/"
|
48
|
+
lt.should match(/class=\"active\"/i)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "doesn't add an active state when the current request doesn't match" do
|
52
|
+
request.stub!(:fullpath).and_return("/wibble")
|
53
|
+
lt = helper.link_to "Madness", "/wobble", :active_on => "/wobble"
|
54
|
+
lt.should_not match(/class=\"active\"/i)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: link_to_active_state
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert May
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- !binary |-
|
13
|
+
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURNakNDQWhxZ0F3SUJB
|
14
|
+
Z0lCQURBTkJna3Foa2lHOXcwQkFRVUZBREEvTVJFd0R3WURWUVFEREFoeWIy
|
15
|
+
SnYKZEcxaGVURVZNQk1HQ2dtU0pvbVQ4aXhrQVJrV0JXZHRZV2xzTVJNd0VR
|
16
|
+
WUtDWkltaVpQeUxHUUJHUllEWTI5dApNQjRYRFRFek1ESXdNVEV5TWpBek9G
|
17
|
+
b1hEVEUwTURJd01URXlNakF6T0Zvd1B6RVJNQThHQTFVRUF3d0ljbTlpCmIz
|
18
|
+
UnRZWGt4RlRBVEJnb0praWFKay9Jc1pBRVpGZ1ZuYldGcGJERVRNQkVHQ2dt
|
19
|
+
U0pvbVQ4aXhrQVJrV0EyTnYKYlRDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFB
|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R
|
35
|
+
MXhQOVd5UEpxVExWCmNFdkMxTGlMU0NTYi92MmxGVnBBNTF3ZGlJQzUvQ1RB
|
36
|
+
Z3lJTUNjN0xtVG9uY3FndkVZM2hRRnNMTlpsZkd0VnoKNU83Y0YzQ0RIdVpJ
|
37
|
+
RmtIbEdCZzNuQy82bjdYVjlucDhwZTk2ODhvbjROYi8yekNPd3pjd09DTTZo
|
38
|
+
SnluOElVZgp6ajVkUTNwMAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
|
39
|
+
date: 2013-02-01 00:00:00.000000000 Z
|
40
|
+
dependencies:
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.2.11
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.2.11
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rspec
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ~>
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.12.0
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ~>
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.12.0
|
73
|
+
description: A simple gem to implement active states on links using the standard Rails
|
74
|
+
`link_to` helper.
|
75
|
+
email:
|
76
|
+
- robotmay@gmail.com
|
77
|
+
executables: []
|
78
|
+
extensions: []
|
79
|
+
extra_rdoc_files: []
|
80
|
+
files:
|
81
|
+
- .gitignore
|
82
|
+
- .travis.yml
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- lib/link_to_active_state.rb
|
88
|
+
- lib/link_to_active_state/railtie.rb
|
89
|
+
- lib/link_to_active_state/version.rb
|
90
|
+
- lib/link_to_active_state/view_helpers/url_helper.rb
|
91
|
+
- link_to_active_state.gemspec
|
92
|
+
- spec/link_to_active_state/view_helpers_spec.rb
|
93
|
+
- spec/link_to_active_state_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
homepage: ''
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.23
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Active states for links using the Rails link_to helper.
|
119
|
+
test_files:
|
120
|
+
- spec/link_to_active_state/view_helpers_spec.rb
|
121
|
+
- spec/link_to_active_state_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
metadata.gz.sig
ADDED
Binary file
|