ryana-link_block 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.
- data/README.rdoc +76 -0
- data/Rakefile +18 -0
- data/init.rb +1 -0
- data/lib/link_block/config.rb +9 -0
- data/lib/link_block/view_helpers.rb +56 -0
- data/lib/link_block.rb +17 -0
- data/link_block.gemspec +32 -0
- data/test/link_block_test.rb +39 -0
- metadata +69 -0
data/README.rdoc
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
= LinkBlock
|
2
|
+
|
3
|
+
LinkBlock lets you create a bunch of links that share options.
|
4
|
+
|
5
|
+
== Examples
|
6
|
+
|
7
|
+
<%- link_block :controller => 'main' do |lb| -%>
|
8
|
+
<%= lb.link_to 'Home', :action => 'index' %>
|
9
|
+
<%= lb.link_to 'Products', :action => 'products' %>
|
10
|
+
<%= lb.link_to 'Locations', :action => 'locations' %>
|
11
|
+
<%= lb.link_to 'Contact', :action => 'contact' %>
|
12
|
+
<%- end -%>
|
13
|
+
|
14
|
+
is the same as doing:
|
15
|
+
|
16
|
+
<%= link_to 'Home', :controller => 'main', :action => 'index' %>
|
17
|
+
<%= link_to 'Products', :controller => 'main', :action => 'products' %>
|
18
|
+
<%= link_to 'Locations', :controller => 'main', :action => 'locations' %>
|
19
|
+
<%= link_to 'Contact', :controller => 'main', :action => 'contact' %>
|
20
|
+
|
21
|
+
It also supports merging html options:
|
22
|
+
|
23
|
+
<%- link_block {:controller => 'main'}, {:class => 'side_bar_link'} do |lb| -%>
|
24
|
+
<%= lb.link_to 'Home', :action => 'index' %>
|
25
|
+
<%- end -%>
|
26
|
+
|
27
|
+
And appending to string routes (if you INSIST on using string routes) instead of hashes:
|
28
|
+
|
29
|
+
<%- link_block '/main/' do |lb| -%>
|
30
|
+
<%= lb.link_to 'Index Page', 'index' %>
|
31
|
+
<%- end -%>
|
32
|
+
|
33
|
+
The current link gets a link_block_current appended to the class list. This can be changed through
|
34
|
+
the LinkBlock::Config object:
|
35
|
+
|
36
|
+
LinkBlock::Config.current_link_class = "different_name"
|
37
|
+
|
38
|
+
or turned off via:
|
39
|
+
|
40
|
+
LinkBlock::Config.current_link_enabled = false
|
41
|
+
|
42
|
+
== Todo
|
43
|
+
|
44
|
+
Now relies on PlugTest for testing. http://github.com/ryana/plug_test
|
45
|
+
|
46
|
+
== Thanks
|
47
|
+
|
48
|
+
* Dan Pickett (http://dpickett.github.com) for providing the Gem template and moral support.
|
49
|
+
|
50
|
+
== Shameless Plug
|
51
|
+
|
52
|
+
Recommend me on Working With Rails (http://workingwithrails.com/recommendation/new/person/14860-ryan-angilly) or give
|
53
|
+
me a shout out on Twitter (http://twitter.com/angilly) @angilly if you think this is awesome.
|
54
|
+
|
55
|
+
== MIT License
|
56
|
+
|
57
|
+
=== Copyright (c) 2008 Ryan Angilly
|
58
|
+
|
59
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
60
|
+
this software and associated documentation files (the "Software"), to deal in
|
61
|
+
the Software without restriction, including without limitation the rights to
|
62
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
63
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
64
|
+
so, subject to the following conditions:
|
65
|
+
|
66
|
+
The above copyright notice and this permission notice shall be included in all
|
67
|
+
copies or substantial portions of the Software.
|
68
|
+
|
69
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
70
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
71
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
72
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
73
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
74
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
75
|
+
SOFTWARE.
|
76
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Lifted directly from http://railscasts.com/episodes/135-making-a-gem
|
2
|
+
#
|
3
|
+
# Thanks Ryan B.
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'rake'
|
7
|
+
require 'echoe'
|
8
|
+
|
9
|
+
Echoe.new('link_block', '0.1.0') do |p|
|
10
|
+
p.description = "Generate a block of link_to's that share parameters."
|
11
|
+
p.url = "http://github.com/ryana/link_block"
|
12
|
+
p.author = "Ryan Angilly"
|
13
|
+
p.email = "ryan@angilly.com"
|
14
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
15
|
+
p.development_dependencies = []
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'link_block'
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module LinkBlock
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
class LinkBlockBuilder
|
5
|
+
|
6
|
+
def initialize(controller, options)
|
7
|
+
@controller = controller
|
8
|
+
@url_options = options[:url_options]
|
9
|
+
@html_options = options[:html_options]
|
10
|
+
end
|
11
|
+
|
12
|
+
def link_to(*args, &block)
|
13
|
+
if block_given?
|
14
|
+
url_options = get_url_options(args.first)
|
15
|
+
html_options = args.second.merge(@html_options)
|
16
|
+
link_to(capture(&block), url_options, html_options)
|
17
|
+
else
|
18
|
+
link_text = args.first
|
19
|
+
url_options = get_url_options(args.second)
|
20
|
+
html_options = (args.third || {}).merge(@html_options)
|
21
|
+
end
|
22
|
+
|
23
|
+
if LinkBlock::Config.current_class_enabled and @controller.current_page?(url_options)
|
24
|
+
html_options[:class] ||= ""
|
25
|
+
html_options[:class] += " #{LinkBlock::Config.current_class_name}"
|
26
|
+
end
|
27
|
+
|
28
|
+
@controller.link_to(link_text, url_options, html_options)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def get_url_options(url)
|
33
|
+
raise 'url params mismatch' unless url.class == @url_options.class
|
34
|
+
|
35
|
+
@url_options.class == Hash ? url.merge(@url_options) : @url_options + url
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def link_block(*args, &block)
|
41
|
+
url_options = args.first
|
42
|
+
html_options = args.second || {}
|
43
|
+
|
44
|
+
if html_options and html_options.class != Hash
|
45
|
+
raise 'link_block requires html_options to be a Hash'
|
46
|
+
end
|
47
|
+
|
48
|
+
if url_options.class != Hash and url_options.class != String
|
49
|
+
raise 'link_block requires url_options to be Hash or String -- and for your sake it really should be a Hash :-)'
|
50
|
+
end
|
51
|
+
|
52
|
+
yield LinkBlockBuilder.new(self, :url_options => url_options, :html_options => html_options)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/lib/link_block.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# The dirname is important, otherwise if rubygems is loaded before this,
|
2
|
+
# (as is the case if you are using PlugTest), then the require will get
|
3
|
+
# the installed gem instead of what you are developing.
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + '/link_block/config'
|
6
|
+
require File.dirname(__FILE__) + '/link_block/view_helpers'
|
7
|
+
|
8
|
+
module LinkBlock
|
9
|
+
def self.enable
|
10
|
+
ActionView::Base.send :include, LinkBlock::ViewHelpers
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
#thanks will_paginate (via dpickett-tab_menu) for this snippet
|
15
|
+
if defined?(Rails) and defined?(ActionController)
|
16
|
+
LinkBlock.enable
|
17
|
+
end
|
data/link_block.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{link_block}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ryan Angilly"]
|
9
|
+
s.date = %q{2008-12-19}
|
10
|
+
s.description = %q{Generate a block of link_to calls that share parameters.}
|
11
|
+
s.email = %q{ryan@angilly.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/link_block.rb", "lib/link_block/config.rb", "lib/link_block/view_helpers.rb"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "init.rb", "lib/link_block.rb", "lib/link_block/config.rb", "lib/link_block/view_helpers.rb", "test/link_block_test.rb", "Rakefile", "link_block.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/ryana/link_block}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Link_block", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{link_block}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Generate a block of link_to's that share parameters.}
|
21
|
+
s.test_files = ["test/link_block_test.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
else
|
29
|
+
end
|
30
|
+
else
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.dirname(__FILE__) + '/test_helper'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/link_block'
|
4
|
+
|
5
|
+
class LinkBlockTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
pt_setup
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_link_block_exists
|
12
|
+
# Make sure we're sane
|
13
|
+
assert @helper.respond_to?(:link_to)
|
14
|
+
|
15
|
+
# Make sure we're included
|
16
|
+
assert @helper.respond_to?(:link_block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_link_block_yields_simple_link_tos
|
20
|
+
# Tried to make this a block... was taking too long.
|
21
|
+
res = pt_helper do |a|
|
22
|
+
a.link_block(:controller => 'regular') { |lb| lb.link_to 'Sweet', :action => 'test' }
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_select 'a', 'Sweet'
|
26
|
+
assert_select HTML::Selector.new("a[href=/regular/test]")
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_link_block_attaches_class_when_link_is_current
|
30
|
+
res = pt_helper do |a|
|
31
|
+
a.link_block(:controller => 'plug_test') { |lb| lb.link_to 'Sweet', :action => 'test_action' }
|
32
|
+
end
|
33
|
+
|
34
|
+
assert_select 'a', 'Sweet'
|
35
|
+
assert_select HTML::Selector.new("a[class~=#{LinkBlock::Config.current_class_name}]")
|
36
|
+
assert_select HTML::Selector.new("a[href=/plug_test/test_action]")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ryana-link_block
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Angilly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-19 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Generate a block of link_to calls that share parameters.
|
17
|
+
email: ryan@angilly.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- lib/link_block.rb
|
25
|
+
- lib/link_block/config.rb
|
26
|
+
- lib/link_block/view_helpers.rb
|
27
|
+
files:
|
28
|
+
- Manifest
|
29
|
+
- README.rdoc
|
30
|
+
- init.rb
|
31
|
+
- lib/link_block.rb
|
32
|
+
- lib/link_block/config.rb
|
33
|
+
- lib/link_block/view_helpers.rb
|
34
|
+
- test/link_block_test.rb
|
35
|
+
- Rakefile
|
36
|
+
- link_block.gemspec
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/ryana/link_block
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --line-numbers
|
42
|
+
- --inline-source
|
43
|
+
- --title
|
44
|
+
- Link_block
|
45
|
+
- --main
|
46
|
+
- README.rdoc
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "1.2"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project: link_block
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: Generate a block of link_to's that share parameters.
|
68
|
+
test_files:
|
69
|
+
- test/link_block_test.rb
|