linkety 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/lib/linkety.rb +3 -0
- data/lib/linkety/railtie.rb +9 -0
- data/lib/linkety/version.rb +3 -0
- data/lib/linkety/view_helpers.rb +112 -0
- data/linkety.gemspec +21 -0
- data/test/linkety/view_helpers_test.rb +188 -0
- data/test/test_helper.rb +6 -0
- metadata +99 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Derrick Reimer
|
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,29 @@
|
|
1
|
+
# Linkety
|
2
|
+
|
3
|
+
Linkety is a collection of handy link helpers for Rails views.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'linkety'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install linkety
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/linkety.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
module Linkety
|
2
|
+
module ViewHelpers
|
3
|
+
# Public: Generates an HTML anchor tag in either an active or inactive
|
4
|
+
# state, depending on a given truth value.
|
5
|
+
#
|
6
|
+
# If the truth value is true, this method will generate a link
|
7
|
+
# that has an 'active' class (or whatever class name you optionally
|
8
|
+
# specify for :active_class).
|
9
|
+
#
|
10
|
+
# If the truth value is false, this method will generate a link by
|
11
|
+
# default with an href of '#' (or whatever you specify for :inactive_url)
|
12
|
+
# and with an added 'inactive' class (or whatever you specify for
|
13
|
+
# :inactive_class).
|
14
|
+
#
|
15
|
+
# truth - A Boolean truth value.
|
16
|
+
# text - A String of anchor text.
|
17
|
+
# url - A String URL.
|
18
|
+
# options - A Hash of options. In addition to the regular #link_to options,
|
19
|
+
# you may set the following:
|
20
|
+
# :active_class - A String class to add if the link is active
|
21
|
+
# (default: 'active').
|
22
|
+
# :inactive_class - A String class to add if the link is inactive
|
23
|
+
# (default: 'inactive').
|
24
|
+
# :inactive_url - A String URL for when the link is inactive
|
25
|
+
# (default: '#').
|
26
|
+
#
|
27
|
+
# Returns a String anchor tag.
|
28
|
+
def active_link_to_if(truth, text, url, options = {})
|
29
|
+
active_class = options[:active_class] || "active"
|
30
|
+
inactive_class = options[:inactive_class] || "inactive"
|
31
|
+
inactive_url = options[:inactive_url] || "#"
|
32
|
+
klasses = (options.delete(:class) || "").split(' ')
|
33
|
+
|
34
|
+
klasses << (truth ? active_class : inactive_class)
|
35
|
+
url = inactive_url unless truth
|
36
|
+
|
37
|
+
link_to(text, url, options.merge(:class => klasses.join(' ')))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Public: Generates an HTML anchor tag in either an inactive or active
|
41
|
+
# state, depending on a given truth value. (See #active_link_to_if for more
|
42
|
+
# details.)
|
43
|
+
#
|
44
|
+
# truth - A Boolean truth value.
|
45
|
+
# text - A String of anchor text.
|
46
|
+
# url - A String URL.
|
47
|
+
# options - A Hash of options. In addition to the regular #link_to options,
|
48
|
+
# you may set the following:
|
49
|
+
# :active_class - A String class to add if the link is active
|
50
|
+
# (default: 'active').
|
51
|
+
# :inactive_class - A String class to add if the link is inactive
|
52
|
+
# (default: 'inactive').
|
53
|
+
# :inactive_url - A String URL for when the link is inactive
|
54
|
+
# (default: '#').
|
55
|
+
#
|
56
|
+
# Returns a String anchor tag.
|
57
|
+
def inactive_link_to_if(truth, text, url, options = {})
|
58
|
+
active_link_to_if(!truth, text, url, options)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Public: Generates an HTML anchor tag that has a 'current' class if
|
62
|
+
# the current request path matches the link path.
|
63
|
+
#
|
64
|
+
# text - A String of anchor text.
|
65
|
+
# url - A String URL.
|
66
|
+
# options - A Hash of options. In addition to the regular #link_to options,
|
67
|
+
# you may set the following:
|
68
|
+
# :current_class - The String class to add if the link is current
|
69
|
+
# (default: 'current').
|
70
|
+
# :pattern - A Regexp pattern to match against the current
|
71
|
+
# path (default: the link URL path).
|
72
|
+
#
|
73
|
+
# Returns a String anchor tag.
|
74
|
+
def current_link_to(text, url, options = {})
|
75
|
+
current_class = options[:current_class] || "current"
|
76
|
+
klasses = (options.delete(:class) || "").split(' ')
|
77
|
+
href_path = extract_path(url)
|
78
|
+
pattern = options[:pattern] || Regexp.new(href_path)
|
79
|
+
|
80
|
+
klasses << current_class if current_path =~ pattern
|
81
|
+
link_to(text, url, options.merge(:class => klasses.join(' ')))
|
82
|
+
end
|
83
|
+
|
84
|
+
# Private: The path of the current request.
|
85
|
+
#
|
86
|
+
# Returns a String.
|
87
|
+
def current_path
|
88
|
+
raise "A Request object must be present" unless request
|
89
|
+
fullpath = request.fullpath.split("?")[0]
|
90
|
+
extract_path(fullpath)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Private: Extract just the path portion of a URL without the query string.
|
94
|
+
#
|
95
|
+
# uri - A String URL.
|
96
|
+
#
|
97
|
+
# Examples
|
98
|
+
#
|
99
|
+
# extract_path("http://google.com/foo/bar")
|
100
|
+
# => "/foo/bar"
|
101
|
+
#
|
102
|
+
# extract_path("/foo/bar/que?order=asc")
|
103
|
+
# => "/foo/bar/que"
|
104
|
+
#
|
105
|
+
# Returns a String.
|
106
|
+
def extract_path(uri)
|
107
|
+
matches = /(^\w+)*(:\/\/){0,1}(\w|\.)*(\/.*)/.match(uri)
|
108
|
+
path = matches ? matches[4] : ""
|
109
|
+
path.split("?")[0] || ""
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/linkety.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/linkety/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Derrick Reimer"]
|
6
|
+
gem.email = ["derrickreimer@gmail.com"]
|
7
|
+
gem.description = %q{A collection of handy link helpers for Rails views}
|
8
|
+
gem.summary = %q{Linkety is a collection of handy link helpers for Rails views.}
|
9
|
+
gem.homepage = "https://github.com/djreimer/linkety"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "linkety"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Linkety::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "shoulda-context", "~> 1.0"
|
19
|
+
gem.add_development_dependency "activesupport", "~> 3.0"
|
20
|
+
gem.add_development_dependency "actionpack", "~> 3.0"
|
21
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper.rb'
|
2
|
+
|
3
|
+
class Linkety::ViewHelpersTest < Test::Unit::TestCase
|
4
|
+
include ActionView::Helpers::UrlHelper
|
5
|
+
include Linkety::ViewHelpers
|
6
|
+
|
7
|
+
attr_accessor :request
|
8
|
+
|
9
|
+
def request_for_path(path)
|
10
|
+
request = MiniTest::Mock.new
|
11
|
+
request.expect(:fullpath, path)
|
12
|
+
request
|
13
|
+
end
|
14
|
+
|
15
|
+
def extract_text(tag)
|
16
|
+
groups = /<a.*>(.*)<\/a>/.match(tag)
|
17
|
+
text = groups ? groups[1].to_s : ""
|
18
|
+
end
|
19
|
+
|
20
|
+
def extract_attribute(attribute, tag)
|
21
|
+
groups = /#{attribute}=("|')([^"']*)("|')/.match(tag)
|
22
|
+
klasses = groups ? groups[2] : ""
|
23
|
+
klasses.split(' ') || []
|
24
|
+
end
|
25
|
+
|
26
|
+
def extract_classes(tag)
|
27
|
+
extract_attribute("class", tag)
|
28
|
+
end
|
29
|
+
|
30
|
+
def extract_href(tag)
|
31
|
+
extract_attribute("href", tag)[0] || ""
|
32
|
+
end
|
33
|
+
|
34
|
+
context "test helpers" do
|
35
|
+
context "#extract_classes" do
|
36
|
+
should "work with single quotes" do
|
37
|
+
tag = "<a href='http://google.com' class='one two three'>Google</a>"
|
38
|
+
assert_equal ["one", "two", "three"], extract_classes(tag)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "work with double quotes" do
|
42
|
+
tag = "<a href='http://google.com' class=\"one two three\">Google</a>"
|
43
|
+
assert_equal ["one", "two", "three"], extract_classes(tag)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "return an empty array if no classes" do
|
47
|
+
tag = "<a href='http://google.com' class=''>Google</a>"
|
48
|
+
assert_equal [], extract_classes(tag)
|
49
|
+
end
|
50
|
+
|
51
|
+
should "return an empty array if class attribute not set" do
|
52
|
+
tag = "<a href='http://google.com'>Google</a>"
|
53
|
+
assert_equal [], extract_classes(tag)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "#extract_href" do
|
58
|
+
should "return the URL" do
|
59
|
+
tag = "<a href='http://google.com' class='one two three'>Google</a>"
|
60
|
+
assert_equal "http://google.com", extract_href(tag)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "return an empty string if href is not set" do
|
64
|
+
tag = "<a class='one two three'>Google</a>"
|
65
|
+
assert_equal "", extract_href(tag)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "#extract_text" do
|
70
|
+
should "return the anchor text" do
|
71
|
+
tag = "<a href='http://google.com' class='one two three'>Google</a>"
|
72
|
+
assert_equal "Google", extract_text(tag)
|
73
|
+
end
|
74
|
+
|
75
|
+
should "return an empty string if link has not text" do
|
76
|
+
tag = "<a href='http://google.com' class='one two three'></a>"
|
77
|
+
assert_equal "", extract_text(tag)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "#active_link_to_if" do
|
83
|
+
should "be active if true" do
|
84
|
+
tag = active_link_to_if(true, "Foobar", "http://google.com")
|
85
|
+
assert extract_classes(tag).include?("active")
|
86
|
+
end
|
87
|
+
|
88
|
+
should "be inactive if false" do
|
89
|
+
tag = active_link_to_if(false, "Foobar", "http://google.com")
|
90
|
+
assert extract_classes(tag).include?("inactive")
|
91
|
+
end
|
92
|
+
|
93
|
+
should "change inactive URL to a hash" do
|
94
|
+
tag = active_link_to_if(false, "Foobar", "http://google.com")
|
95
|
+
assert_equal "#", extract_href(tag)
|
96
|
+
end
|
97
|
+
|
98
|
+
should "append to given classes" do
|
99
|
+
tag = active_link_to_if(false, "Foobar", "http://google.com", :class => "one two")
|
100
|
+
klasses = extract_classes(tag)
|
101
|
+
assert klasses.include?("one")
|
102
|
+
assert klasses.include?("two")
|
103
|
+
assert klasses.include?("inactive")
|
104
|
+
end
|
105
|
+
|
106
|
+
should "use custom active class" do
|
107
|
+
tag = active_link_to_if(true, "Foobar", "http://google.com", :active_class => "enabled")
|
108
|
+
assert extract_classes(tag).include?("enabled")
|
109
|
+
end
|
110
|
+
|
111
|
+
should "use custom inactive class" do
|
112
|
+
tag = active_link_to_if(false, "Foobar", "http://google.com", :active_class => "disabled")
|
113
|
+
assert extract_classes(tag).include?("disabled")
|
114
|
+
end
|
115
|
+
|
116
|
+
should "use custom inactive URL" do
|
117
|
+
tag = active_link_to_if(false, "Foobar", "http://google.com", :inactive_url => "http://yahoo.com")
|
118
|
+
assert_equal "http://yahoo.com", extract_href(tag)
|
119
|
+
end
|
120
|
+
|
121
|
+
should "have the right text" do
|
122
|
+
tag = active_link_to_if(true, "Foobar", "http://google.com")
|
123
|
+
assert_equal "Foobar", extract_text(tag)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "#current_link_to_if" do
|
128
|
+
should "match current for full URL links" do
|
129
|
+
@request = request_for_path("/search")
|
130
|
+
tag = current_link_to("Google", "http://google.com/search")
|
131
|
+
assert extract_classes(tag).include?("current")
|
132
|
+
end
|
133
|
+
|
134
|
+
should "not match different full URL links" do
|
135
|
+
@request = request_for_path("http://google.com/foo")
|
136
|
+
tag = current_link_to("Google", "http://google.com/search")
|
137
|
+
assert !extract_classes(tag).include?("current")
|
138
|
+
end
|
139
|
+
|
140
|
+
should "match current for path links" do
|
141
|
+
@request = request_for_path("/search")
|
142
|
+
tag = current_link_to("Google", "/search")
|
143
|
+
assert extract_classes(tag).include?("current")
|
144
|
+
end
|
145
|
+
|
146
|
+
should "not match different path links" do
|
147
|
+
@request = request_for_path("/foo")
|
148
|
+
tag = current_link_to("Google", "http://google.com/search")
|
149
|
+
assert !extract_classes(tag).include?("current")
|
150
|
+
end
|
151
|
+
|
152
|
+
should "match root path" do
|
153
|
+
@request = request_for_path("/")
|
154
|
+
tag = current_link_to("Google", "http://google.com/")
|
155
|
+
assert extract_classes(tag).include?("current")
|
156
|
+
end
|
157
|
+
|
158
|
+
should "match current with a query string" do
|
159
|
+
@request = request_for_path("/search?q=ruby")
|
160
|
+
tag = current_link_to("Google", "http://google.com/search")
|
161
|
+
assert extract_classes(tag).include?("current")
|
162
|
+
end
|
163
|
+
|
164
|
+
should "match current with additional segments" do
|
165
|
+
@request = request_for_path("/search/foo/bar")
|
166
|
+
tag = current_link_to("Google", "http://google.com/search")
|
167
|
+
assert extract_classes(tag).include?("current")
|
168
|
+
end
|
169
|
+
|
170
|
+
should "use custom current class" do
|
171
|
+
@request = request_for_path("/search")
|
172
|
+
tag = current_link_to("Google", "http://google.com/search", :current_class => "active")
|
173
|
+
assert extract_classes(tag).include?("active")
|
174
|
+
end
|
175
|
+
|
176
|
+
should "use custom pattern" do
|
177
|
+
@request = request_for_path("/foobar")
|
178
|
+
tag = current_link_to("Google", "http://google.com/search", :pattern => /foobar/)
|
179
|
+
assert extract_classes(tag).include?("current")
|
180
|
+
end
|
181
|
+
|
182
|
+
should "have the right text" do
|
183
|
+
@request = request_for_path("/")
|
184
|
+
tag = current_link_to("Foobar", "http://google.com/")
|
185
|
+
assert_equal "Foobar", extract_text(tag)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linkety
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Derrick Reimer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-07-22 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda-context
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activesupport
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "3.0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: actionpack
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "3.0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
description: A collection of handy link helpers for Rails views
|
49
|
+
email:
|
50
|
+
- derrickreimer@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- lib/linkety.rb
|
64
|
+
- lib/linkety/railtie.rb
|
65
|
+
- lib/linkety/version.rb
|
66
|
+
- lib/linkety/view_helpers.rb
|
67
|
+
- linkety.gemspec
|
68
|
+
- test/linkety/view_helpers_test.rb
|
69
|
+
- test/test_helper.rb
|
70
|
+
homepage: https://github.com/djreimer/linkety
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.24
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Linkety is a collection of handy link helpers for Rails views.
|
97
|
+
test_files:
|
98
|
+
- test/linkety/view_helpers_test.rb
|
99
|
+
- test/test_helper.rb
|