active_link_to 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Oleg Khabarov, The Working Group Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,105 @@
1
+ = active_link_to
2
+
3
+ Creates a link tag of the given +name+ using a URL created by the set of
4
+ +options+. Please see documentation for link_to, as active_link_to is
5
+ basically a wrapper for it. This method accepts an optional +:active+
6
+ parameter that dictates if the given link will have an extra css class
7
+ attached that marks it as 'active'.
8
+
9
+ === Super Simple Example
10
+ Here's a link that will have class attached if it happens to be rendered
11
+ on page with path /people or any child of that page like /people/123
12
+
13
+ active_link_to 'People', '/people'
14
+ # => <a href="/people" class="active">People</a>
15
+
16
+ This is exactly the same as:
17
+
18
+ active_link_to 'People', '/people', :active => { :when => :self }
19
+ # => <a href="/people" class="active">People</a>
20
+
21
+ === Options
22
+ Here's available options that can be inserted into :active hash
23
+ * <tt>:when => predefined symbol || Regexp || Array tuple of controller/actions || Boolean</tt> - This controls
24
+ when link is considered to be 'active'
25
+ * <tt>:active_class => 'class_name'</tt> - When link is 'active', css
26
+ class of that link is set to the default 'active'. This parameter
27
+ allows it to be changed
28
+ * <tt>:inactive_class => 'class_name'</tt> - Opposite of the :active_class
29
+ By default it's blank. However you can change it to whatever you want.
30
+ * <tt>:disable_link => Boolean</tt> - When link is active, sometimes you
31
+ want to render span tag instead of the link. This parameter is for that.
32
+
33
+ === Examples
34
+ Most of the functionality of active_link_helper depends on the current
35
+ url. Specifically, request.request_uri value. We covered the basic example
36
+ already, so let's try sometihng more fun
37
+
38
+ We want to highlight the link that matches immidiate url, and not the children
39
+ nodes as well
40
+
41
+ # For URL: /people/24
42
+ active_link_to 'People', people_path, :active => { :when => :self_only }
43
+ # => <a href="/people">People</a>
44
+
45
+ # For URL: /people
46
+ active_link_to 'People', people_path, :active => { :when => :self_only }
47
+ # => <a href="/people" class="active">People</a>
48
+
49
+ If we need to set link to be active based on some regular expression, we can do
50
+ that as well. Let's try to activate links urls of which begin with 'peop':
51
+
52
+ # For URL: /people/44
53
+ active_link_to 'People', people_path, :active => { :when => /^peop/ }
54
+ # => <a href="/people" class="active">People</a>
55
+
56
+ # For URL: /aliens/9
57
+ active_link_to 'People', people_path, :active => { :when => /^peop/ }
58
+ # => <a href="/people">People</a>
59
+
60
+ What if we need to mark link active for all URLs that match a particular controller,
61
+ or action, or both? Or any number of those at the same time? Sure, why not:
62
+
63
+ # For URL: /people/56/edit
64
+ # For matching multiple controllers and actions:
65
+ active_link_to 'Person Edit', edit_person_path(@person), :active => {
66
+ :when => [['people', 'aliens'], ['show', 'edit']]
67
+ }
68
+
69
+ # for matching all actions under given controllers:
70
+ active_link_to 'Person Edit', edit_person_path(@person), :active => {
71
+ :when => [['people', 'aliens'], []]
72
+ }
73
+
74
+ # for matching all controllers for a particular action
75
+ active_link_to 'Person Edit', edit_person_path(@person), :active => {
76
+ :when => [[], ['edit']]
77
+ }
78
+ # => <a href="/people" class="active">People</a>
79
+
80
+ Sometimes it should be easy as setting a true or false:
81
+
82
+ active_link_to 'People', people_path, :active => { :when => true }
83
+ # => <a href="/people" class="active">People</a>
84
+
85
+ active_link_to 'People', people_path, :active => { :when => false }
86
+ # => <a href="/people">People</a>
87
+
88
+ Lets see what happens when we push some css class modifier parameters
89
+
90
+ # For URL: /people/12
91
+ active_link_to 'People', people_path, :active => { :active_link => 'awesome_selected' }
92
+ # => <a href="/people" class="awesome_selected">People</a>
93
+
94
+ active_link_to 'Aliens', aliens_path, :active => { :inactive_link => 'bummer_inactive' }
95
+ # => <a href="/aliens" class="bummer_inactive">Aliens</a>
96
+
97
+ Some wierd people think that link should change to a span if it indicated current page:
98
+
99
+ # For URL: /people
100
+ active_link_to 'People', people_path, :active => { :disable_link => true }
101
+ # => <span class="active">People</span>
102
+
103
+ == Copyright
104
+
105
+ Copyright (c) 2009 Oleg Khabarov, The Working Group Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'active_link_to'
8
+ gem.summary = 'Marks currently active links'
9
+ gem.description = 'Extremely helpful when you need to add some logic that figures out if the link (or more often navigation item) is selected based on the current page or other arbitrary condition'
10
+ gem.email = "oleg@theworkinggroup.ca"
11
+ gem.homepage = "http://github.com/OlegK/active_link_to"
12
+ gem.authors = ["Oleg Khabarov"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "active_link_to #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,159 @@
1
+ module ActiveLinkTo
2
+
3
+ # Creates a link tag of the given +name+ using a URL created by the set of
4
+ # +options+. Please see documentation for link_to, as active_link_to is
5
+ # basically a wrapper for it. This method accepts an optional +:active+
6
+ # parameter that dictates if the given link will have an extra css class
7
+ # attached that marks it as 'active'.
8
+ #
9
+ # === Super Simple Example
10
+ # Here's a link that will have class attached if it happens to be rendered
11
+ # on page with path /people or any child of that page like /people/123
12
+ #
13
+ # active_link_to 'People', '/people'
14
+ # # => <a href="/people" class="active">People</a>
15
+ #
16
+ # This is exactly the same as:
17
+ #
18
+ # active_link_to 'People', '/people', :active => { :when => :self }
19
+ # # => <a href="/people" class="active">People</a>
20
+ #
21
+ # === Options
22
+ # Here's available options that can be inserted into :active hash
23
+ # * <tt>:when => predefined symbol || Regexp || Array tuple of controller/actions || Boolean</tt> - This controls
24
+ # when link is considered to be 'active'
25
+ # * <tt>:active_class => 'class_name'</tt> - When link is 'active', css
26
+ # class of that link is set to the default 'active'. This parameter
27
+ # allows it to be changed
28
+ # * <tt>:inactive_class => 'class_name'</tt> - Opposite of the :active_class
29
+ # By default it's blank. However you can change it to whatever you want.
30
+ # * <tt>:disable_link => Boolean</tt> - When link is active, sometimes you
31
+ # want to render span tag instead of the link. This parameter is for that.
32
+ #
33
+ # === Examples
34
+ # Most of the functionality of active_link_helper depends on the current
35
+ # url. Specifically, request.request_uri value. We covered the basic example
36
+ # already, so let's try sometihng more fun
37
+ #
38
+ # We want to highlight the link that matches immidiate url, and not the children
39
+ # nodes as well
40
+ #
41
+ # # For URL: /people/24
42
+ # active_link_to 'People', people_path, :active => { :when => :self_only }
43
+ # # => <a href="/people">People</a>
44
+ #
45
+ # # For URL: /people
46
+ # active_link_to 'People', people_path, :active => { :when => :self_only }
47
+ # # => <a href="/people" class="active">People</a>
48
+ #
49
+ # If we need to set link to be active based on some regular expression, we can do
50
+ # that as well. Let's try to activate links urls of which begin with 'peop':
51
+ #
52
+ # # For URL: /people/44
53
+ # active_link_to 'People', people_path, :active => { :when => /^peop/ }
54
+ # # => <a href="/people" class="active">People</a>
55
+ #
56
+ # # For URL: /aliens/9
57
+ # active_link_to 'People', people_path, :active => { :when => /^peop/ }
58
+ # # => <a href="/people">People</a>
59
+ #
60
+ # What if we need to mark link active for all URLs that match a particular controller,
61
+ # or action, or both? Or any number of those at the same time? Sure, why not:
62
+ #
63
+ # # For URL: /people/56/edit
64
+ # # For matching multiple controllers and actions:
65
+ # active_link_to 'Person Edit', edit_person_path(@person), :active => {
66
+ # :when => [['people', 'aliens'], ['show', 'edit']]
67
+ # }
68
+ #
69
+ # # for matching all actions under given controllers:
70
+ # active_link_to 'Person Edit', edit_person_path(@person), :active => {
71
+ # :when => [['people', 'aliens'], []]
72
+ # }
73
+ #
74
+ # # for matching all controllers for a particular action
75
+ # active_link_to 'Person Edit', edit_person_path(@person), :active => {
76
+ # :when => [[], ['edit']]
77
+ # }
78
+ # # => <a href="/people" class="active">People</a>
79
+ #
80
+ # Sometimes it should be easy as setting a true or false:
81
+ #
82
+ # active_link_to 'People', people_path, :active => { :when => true }
83
+ # # => <a href="/people" class="active">People</a>
84
+ #
85
+ # active_link_to 'People', people_path, :active => { :when => false }
86
+ # # => <a href="/people">People</a>
87
+ #
88
+ # Lets see what happens when we push some css class modifier parameters
89
+ #
90
+ # # For URL: /people/12
91
+ # active_link_to 'People', people_path, :active => { :active_link => 'awesome_selected' }
92
+ # # => <a href="/people" class="awesome_selected">People</a>
93
+ #
94
+ # active_link_to 'Aliens', aliens_path, :active => { :inactive_link => 'bummer_inactive' }
95
+ # # => <a href="/aliens" class="bummer_inactive">Aliens</a>
96
+ #
97
+ # Some wierd people think that link should change to a span if it indicated current page:
98
+ #
99
+ # # For URL: /people
100
+ # active_link_to 'People', people_path, :active => { :disable_link => true }
101
+ # # => <span class="active">People</span>
102
+ #
103
+ def active_link_to(*args, &block)
104
+ if block_given?
105
+ name = capture(&block)
106
+ options = args[0] || {}
107
+ html_options = args[1] || {}
108
+ else
109
+ name = args[0]
110
+ options = args[1] || {}
111
+ html_options = args[2] || {}
112
+ end
113
+
114
+ url = url_for(options)
115
+ active_link_options = html_options.delete(:active) || {}
116
+ css_class = active_class(url, active_link_options)
117
+
118
+ html_options[:class] ||= ''
119
+ html_options[:class] += " #{css_class}" if !css_class.blank?
120
+ html_options[:class].blank? ? html_options.delete(:class) : html_options[:class].lstrip!
121
+
122
+ if active_link_options[:disable_link] === true
123
+ content_tag(:span, name, html_options)
124
+ else
125
+ link_to(name, url, html_options)
126
+ end
127
+ end
128
+
129
+ # Returns css class name. Takes the link's URL and :active paramers
130
+ def active_class(url, options = {})
131
+ if is_active_link?(url, options)
132
+ options[:active_class] || 'active'
133
+ else
134
+ options[:inactive_class] || ''
135
+ end
136
+ end
137
+
138
+ # Returns true or false. Takes the link's URL and :active parameters
139
+ def is_active_link?(url, options = {})
140
+ case options[:when]
141
+ when :self, nil
142
+ !request.request_uri.match(/^#{Regexp.escape(url)}(\/.*)?$/).blank?
143
+ when :self_only
144
+ !request.request_uri.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
145
+ when Regexp
146
+ !request.request_uri.match(options[:when]).blank?
147
+ when Array
148
+ controllers = options[:when][0]
149
+ actions = options[:when][1]
150
+ (controllers.blank? || controllers.member?(params[:controller])) &&
151
+ (actions.blank? || actions.member?(params[:action]))
152
+ when TrueClass
153
+ true
154
+ when FalseClass
155
+ false
156
+ end
157
+ end
158
+
159
+ end
@@ -0,0 +1,76 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveLinkToTest < Test::Unit::TestCase
4
+
5
+ def test_matching_self
6
+ request.request_uri = '/test'
7
+ out = active_link_to 'name', '/test'
8
+ assert_equal '<a href="/test" class="active">name</a>', out
9
+ out = active_link_to 'name', '/test', :active => { :when => :self }
10
+ assert_equal '<a href="/test" class="active">name</a>', out
11
+ out = active_link_to 'name', '/not-test'
12
+ assert_equal '<a href="/not-test">name</a>', out
13
+ end
14
+
15
+ def test_matching_self_only
16
+ request.request_uri = '/test/fail'
17
+ out = active_link_to 'name', '/test/fail', :active => { :when => :self_only }
18
+ assert_equal '<a href="/test/fail" class="active">name</a>', out
19
+ out = active_link_to 'name', '/test', :active => { :when => :self_only }
20
+ assert_equal '<a href="/test">name</a>', out
21
+ end
22
+
23
+ def test_matching_self_only_with_extra_parameters
24
+ request.request_uri = '/test/fail?why=because'
25
+ out = active_link_to 'name', '/test/fail', :active => { :when => :self_only }
26
+ assert_equal '<a href="/test/fail" class="active">name</a>', out
27
+ end
28
+
29
+ def test_matching_custom_regex
30
+ request.request_uri = '/test/something_else'
31
+ out = active_link_to 'name', '/test', :active => { :when => /^\/te/}
32
+ assert_equal '<a href="/test" class="active">name</a>', out
33
+ out = active_link_to 'name', '/test', :active => { :when => /^\/no/}
34
+ assert_equal '<a href="/test">name</a>', out
35
+ end
36
+
37
+ def test_matching_controller_action_touples
38
+ request.request_uri = '/test/23'
39
+ params[:controller], params[:action] = 'tests', 'show'
40
+ out = active_link_to 'name', '/test/23', :active => { :when => [['tests'], ['show', 'edit']]}
41
+ assert_equal '<a href="/test/23" class="active">name</a>', out
42
+ out = active_link_to 'name', '/test/23', :active => { :when => [['tests'], []]}
43
+ assert_equal '<a href="/test/23" class="active">name</a>', out
44
+ out = active_link_to 'name', '/test/23', :active => { :when => [[], ['show']]}
45
+ assert_equal '<a href="/test/23" class="active">name</a>', out
46
+ out = active_link_to 'name', '/test/23', :active => { :when => [['tests'], ['update']]}
47
+ assert_equal '<a href="/test/23">name</a>', out
48
+ end
49
+
50
+ def test_matching_booleans
51
+ request.request_uri = 'doesnotmatter'
52
+ out = active_link_to 'name', '/test', :active => { :when => true }
53
+ assert_equal '<a href="/test" class="active">name</a>', out
54
+ out = active_link_to 'name', '/test', :active => { :when => false }
55
+ assert_equal '<a href="/test">name</a>', out
56
+ end
57
+
58
+ def test_setting_active_class
59
+ request.request_uri = '/test'
60
+ out = active_link_to 'name', '/test', :active => { :active_class => 'new_active'}
61
+ assert_equal '<a href="/test" class="new_active">name</a>', out
62
+ end
63
+
64
+ def test_setting_inactive_class
65
+ request.request_uri = '/test'
66
+ out = active_link_to 'name', '/not-test', :active => { :inactive_class => 'new_inactive'}
67
+ assert_equal '<a href="/not-test" class="new_inactive">name</a>', out
68
+ end
69
+
70
+ def test_transforming_to_span
71
+ request.request_uri = '/test'
72
+ out = active_link_to 'name', '/test', :active => { :disable_link => true }
73
+ assert_equal '<span class="active">name</span>', out
74
+ end
75
+
76
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'action_controller'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'active_link_to'
8
+
9
+ # need this to simulate requests that drive active_link_helper
10
+ module FakeRequest
11
+ class Request
12
+ attr_accessor :request_uri
13
+ end
14
+ def request
15
+ @request ||= Request.new
16
+ end
17
+ def params
18
+ @params ||= {}
19
+ end
20
+ end
21
+
22
+ ActiveLinkTo.send :include, FakeRequest
23
+
24
+ class Test::Unit::TestCase
25
+
26
+ include ActionView::Helpers::UrlHelper
27
+ include ActionView::Helpers::TagHelper
28
+ include ActiveLinkTo
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_link_to
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Khabarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-01 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Extremely helpful when you need to add some logic that figures out if the link (or more often navigation item) is selected based on the current page or other arbitrary condition
17
+ email: oleg@theworkinggroup.ca
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/active_link_to.rb
33
+ - test/active_link_to_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/OlegK/active_link_to
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Marks currently active links
63
+ test_files:
64
+ - test/active_link_to_test.rb
65
+ - test/test_helper.rb