actionview-link_to_block 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +11 -0
- data/Appraisals +12 -0
- data/Gemfile +0 -1
- data/README.md +48 -3
- data/Rakefile +10 -0
- data/gemfiles/actionpack_3_2.gemfile +9 -0
- data/gemfiles/actionpack_4_0.gemfile +9 -0
- data/gemfiles/actionview_4_1.gemfile +10 -0
- data/lib/action_view/link_to_block/link_to_block.rb +30 -0
- data/lib/action_view/link_to_block/version.rb +1 -1
- data/test/test_actionview-link_to_block.rb +187 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c59c5d3a44fdccf1396bff331de2d057e31d429
|
4
|
+
data.tar.gz: 94c77fc1da940bad5fa765ac6a5ca6753f611c99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90ae3020ed3ce28558a35c765075af2232c338ea2a2fa6b2c2b543a3b5f70fea738f2b3c0a9647bdf5f8a523804a35ba77e9df7e27d17987a458aea091f825a4
|
7
|
+
data.tar.gz: 1fc0bd7d7655110b8cd96a0cd01ac38cf320bc8d654083d279e09ab8e50f909daec8cf352a0dc48f6913ca841eca876e8a529642a24dce42d328be385ac1dee8
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
appraise 'actionview_4_1' do
|
2
|
+
gem 'actionview', github: 'rails', branch: 'master'
|
3
|
+
gem 'actionpack', github: 'rails', branch: 'master'
|
4
|
+
end
|
5
|
+
|
6
|
+
appraise 'actionpack_4_0' do
|
7
|
+
gem 'actionpack', '>= 4.0.0'
|
8
|
+
end
|
9
|
+
|
10
|
+
appraise 'actionpack_3_2' do
|
11
|
+
gem 'actionpack', '>= 3.0', '< 4.0'
|
12
|
+
end
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,36 @@
|
|
1
1
|
# Actionview::LinkToBlock
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/actionview-link_to_block.png)](http://badge.fury.io/rb/actionview-link_to_block)
|
4
|
+
[![Build Status](https://api.travis-ci.org/sanemat/actionview-link_to_block.png?branch=master)](https://travis-ci.org/sanemat/actionview-link_to_block)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/sanemat/actionview-link_to_block.png)](https://codeclimate.com/github/sanemat/actionview-link_to_block)
|
6
|
+
|
7
|
+
Add helper method, `link_to_block`, `link_to_block_if`, `link_to_block_unless`, `link_to_block_unless_current`.
|
8
|
+
This is symmetrical to `link_to`, `link_to_if`, `link_to_unless`, `link_to_unless_current`.
|
9
|
+
|
10
|
+
`link_to_block*` always accepts block.
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
`link_to` accepts complex html as block, like below:
|
15
|
+
|
16
|
+
<%= link_to user_path(@user) do %>
|
17
|
+
<i class="icon-ok icon-white"></i> Do it@
|
18
|
+
<% end %>
|
19
|
+
# http://stackoverflow.com/questions/9401942/using-link-to-with-embedded-html
|
20
|
+
|
21
|
+
But `link_to_if` with block behavior is below:
|
22
|
+
|
23
|
+
<%= link_to_if condition, user_path(@user) do %>
|
24
|
+
Appear if condition falsy
|
25
|
+
<% end %>
|
26
|
+
|
27
|
+
Then use `link_to_block_if` below:
|
28
|
+
|
29
|
+
<%= link_to_block_if condition, user_path(@user) do %>
|
30
|
+
<i class="icon-ok icon-white"></i> Do it@
|
31
|
+
<% end %>
|
32
|
+
|
33
|
+
#=> if condition truthy, then shows html and link, else if condition falsy, then show only html.
|
4
34
|
|
5
35
|
## Installation
|
6
36
|
|
@@ -16,9 +46,24 @@ Or install it yourself as:
|
|
16
46
|
|
17
47
|
$ gem install actionview-link_to_block
|
18
48
|
|
19
|
-
##
|
49
|
+
## Requirement
|
50
|
+
|
51
|
+
`actionview-link_to_block` has no gem dependency in gemspec, but this is not correctly.
|
52
|
+
`actionview` extracts from `actionpack` on rails4.1.
|
53
|
+
You can see `Appraisals` file and `/gemfiles` directory.
|
54
|
+
|
55
|
+
actionview v4.1(rails v4.1)
|
56
|
+
|
57
|
+
gem 'actionview', github: 'rails', branch: 'master'
|
58
|
+
gem 'actionpack', github: 'rails', branch: 'master'
|
59
|
+
|
60
|
+
actionpack v4.0(rails v4.0)
|
61
|
+
|
62
|
+
gem 'actionpack', '>= 4.0.0'
|
63
|
+
|
64
|
+
actionpack v3.2(rails v3.2)
|
20
65
|
|
21
|
-
|
66
|
+
gem 'actionpack', '>= 3.0', '< 4.0'
|
22
67
|
|
23
68
|
## Contributing
|
24
69
|
|
data/Rakefile
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "pry"
|
6
|
+
gem "appraisal", :github=>"thoughtbot/appraisal"
|
7
|
+
gem "actionview", :github=>"rails", :branch=>"master"
|
8
|
+
gem "actionpack", :github=>"rails", :branch=>"master"
|
9
|
+
|
10
|
+
gemspec :path=>".././"
|
@@ -3,6 +3,36 @@ module LinkToBlock
|
|
3
3
|
module Helpers
|
4
4
|
module UrlHelper
|
5
5
|
alias_method :link_to_block, :link_to
|
6
|
+
|
7
|
+
def link_to_block_unless(condition, name = nil, options = {}, html_options = {}, &block)
|
8
|
+
if condition
|
9
|
+
if block_given?
|
10
|
+
capture(&block)
|
11
|
+
else
|
12
|
+
ERB::Util.html_escape(name)
|
13
|
+
end
|
14
|
+
else
|
15
|
+
if block_given?
|
16
|
+
html_options, options = options, name
|
17
|
+
link_to capture(&block), options, html_options
|
18
|
+
else
|
19
|
+
link_to name, options, html_options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def link_to_block_if(condition, name = nil, options = {}, html_options = {}, &block)
|
25
|
+
link_to_block_unless !condition, name, options, html_options, &block
|
26
|
+
end
|
27
|
+
|
28
|
+
def link_to_block_unless_current(name = nil, options = {}, html_options = {}, &block)
|
29
|
+
if block_given?
|
30
|
+
html_options, options = options, name
|
31
|
+
link_to_block_unless current_page?(options), options, html_options, &block
|
32
|
+
else
|
33
|
+
link_to_block_unless current_page?(options), name, options, html_options
|
34
|
+
end
|
35
|
+
end
|
6
36
|
end
|
7
37
|
end
|
8
38
|
end
|
@@ -1,11 +1,197 @@
|
|
1
|
-
|
1
|
+
#coding: utf-8
|
2
|
+
require 'active_support/version'
|
3
|
+
autorun_path = ActiveSupport::VERSION::STRING.start_with?('3')\
|
4
|
+
? 'minitest/autorun'
|
5
|
+
: 'active_support/testing/autorun'
|
6
|
+
require autorun_path
|
2
7
|
require 'action_view'
|
3
8
|
require 'action_view/link_to_block/link_to_block'
|
9
|
+
require 'action_dispatch'
|
10
|
+
|
11
|
+
# copy from action_view/test/abstract_unit.rb
|
12
|
+
module RenderERBUtils
|
13
|
+
def view
|
14
|
+
@view ||= begin
|
15
|
+
path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
|
16
|
+
view_paths = ActionView::PathSet.new([path])
|
17
|
+
ActionView::Base.new(view_paths)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def render_erb(string)
|
22
|
+
@virtual_path = nil
|
23
|
+
|
24
|
+
template = ActionView::Template.new(
|
25
|
+
string.strip,
|
26
|
+
"test template",
|
27
|
+
ActionView::Template::Handlers::ERB,
|
28
|
+
{})
|
29
|
+
|
30
|
+
template.render(self, {}).strip
|
31
|
+
end
|
32
|
+
end
|
4
33
|
|
5
34
|
class LinkToBlockTest < ActiveSupport::TestCase
|
35
|
+
attr_accessor :controller, :request
|
36
|
+
|
37
|
+
routes = ActionDispatch::Routing::RouteSet.new
|
38
|
+
routes.draw do
|
39
|
+
get "/" => "foo#bar"
|
40
|
+
get "/other" => "foo#other"
|
41
|
+
get "/article/:id" => "foo#article", :as => :article
|
42
|
+
end
|
43
|
+
|
44
|
+
include ActionView::Helpers::UrlHelper
|
45
|
+
include routes.url_helpers
|
46
|
+
|
47
|
+
include ActionDispatch::Assertions::DomAssertions
|
48
|
+
include ActionView::Context
|
49
|
+
include RenderERBUtils
|
50
|
+
|
51
|
+
def hash_for(options = {})
|
52
|
+
{ controller: "foo", action: "bar" }.merge!(options)
|
53
|
+
end
|
54
|
+
alias url_hash hash_for
|
55
|
+
|
6
56
|
def test_initialization
|
7
57
|
[:link_to_block].each do |method|
|
8
58
|
assert_includes ActionView::Helpers::UrlHelper.instance_methods, method
|
9
59
|
end
|
10
60
|
end
|
61
|
+
|
62
|
+
def test_link_tag_with_straight_url
|
63
|
+
assert_dom_equal %{<a href="http://www.example.com">Hello</a>}, link_to_block("Hello", "http://www.example.com")
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_link_tag_without_host_option
|
67
|
+
assert_dom_equal(%{<a href="/">Test Link</a>}, link_to_block('Test Link', url_hash))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_link_tag_with_host_option
|
71
|
+
hash = hash_for(host: "www.example.com")
|
72
|
+
expected = %{<a href="http://www.example.com/">Test Link</a>}
|
73
|
+
assert_dom_equal(expected, link_to_block('Test Link', hash))
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_link_tag_with_query
|
77
|
+
expected = %{<a href="http://www.example.com?q1=v1&q2=v2">Hello</a>}
|
78
|
+
assert_dom_equal expected, link_to_block("Hello", "http://www.example.com?q1=v1&q2=v2")
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_link_tag_with_query_and_no_name
|
82
|
+
expected = %{<a href="http://www.example.com?q1=v1&q2=v2">http://www.example.com?q1=v1&q2=v2</a>}
|
83
|
+
assert_dom_equal expected, link_to_block(nil, "http://www.example.com?q1=v1&q2=v2")
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_link_tag_with_block
|
87
|
+
assert_dom_equal %{<a href="/"><span>Example site</span></a>},
|
88
|
+
link_to_block('/') { content_tag(:span, 'Example site') }
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_link_tag_with_block_and_html_options
|
92
|
+
assert_dom_equal %{<a class="special" href="/"><span>Example site</span></a>},
|
93
|
+
link_to_block('/', class: "special") { content_tag(:span, 'Example site') }
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_link_tag_using_block_in_erb
|
97
|
+
out = render_erb %{<%= link_to_block('/') do %>Example site<% end %>}
|
98
|
+
assert_equal '<a href="/">Example site</a>', out
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_link_tag_with_html_safe_string
|
102
|
+
assert_dom_equal(
|
103
|
+
%{<a href="/article/Gerd_M%C3%BCller">Gerd Müller</a>},
|
104
|
+
link_to_block("Gerd Müller", article_path("Gerd_Müller".html_safe))
|
105
|
+
)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_link_tag_escapes_content
|
109
|
+
assert_dom_equal %{<a href="/">Malicious <script>content</script></a>},
|
110
|
+
link_to_block("Malicious <script>content</script>", "/")
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_link_tag_does_not_escape_html_safe_content
|
114
|
+
assert_dom_equal %{<a href="/">Malicious <script>content</script></a>},
|
115
|
+
link_to_block("Malicious <script>content</script>".html_safe, "/")
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_link_to_unless
|
119
|
+
assert_equal "Showing", link_to_block_unless(true, "Showing", url_hash)
|
120
|
+
|
121
|
+
assert_dom_equal %{<a href="/">Listing</a>},
|
122
|
+
link_to_block_unless(false, "Listing", url_hash)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_link_tag_unless_with_block
|
126
|
+
assert_dom_equal %{<a href="/"><span>Example site</span></a>},
|
127
|
+
link_to_block_unless(false, '/') { content_tag(:span, 'Example site') }
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_link_tag_unless_with_block_and_html_options
|
131
|
+
assert_dom_equal %{<a class="special" href="/"><span>Example site</span></a>},
|
132
|
+
link_to_block_unless(false, '/', class: "special") { content_tag(:span, 'Example site') }
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_link_tag_unless_using_block_in_erb
|
136
|
+
out = render_erb %{<%= link_to_block_unless(false, '/') do %>Example site<% end %>}
|
137
|
+
assert_equal '<a href="/">Example site</a>', out
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_link_to_block_if
|
141
|
+
assert_equal "Showing", link_to_block_if(false, "Showing", url_hash)
|
142
|
+
assert_dom_equal %{<a href="/">Listing</a>}, link_to_block_if(true, "Listing", url_hash)
|
143
|
+
end
|
144
|
+
|
145
|
+
def request_for_url(url, opts = {})
|
146
|
+
env = Rack::MockRequest.env_for("http://www.example.com#{url}", opts)
|
147
|
+
ActionDispatch::Request.new(env)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_link_unless_current
|
151
|
+
@request = request_for_url("/")
|
152
|
+
|
153
|
+
assert_equal "Showing",
|
154
|
+
link_to_block_unless_current("Showing", url_hash)
|
155
|
+
assert_equal "Showing",
|
156
|
+
link_to_block_unless_current("Showing", "http://www.example.com/")
|
157
|
+
assert_equal %{<span>Example site</span>},
|
158
|
+
link_to_block_unless_current(url_hash) { content_tag(:span, 'Example site') }
|
159
|
+
|
160
|
+
@request = request_for_url("/?order=desc")
|
161
|
+
|
162
|
+
assert_equal "Showing",
|
163
|
+
link_to_block_unless_current("Showing", url_hash)
|
164
|
+
assert_equal "Showing",
|
165
|
+
link_to_block_unless_current("Showing", "http://www.example.com/")
|
166
|
+
|
167
|
+
@request = request_for_url("/?order=desc&page=1")
|
168
|
+
|
169
|
+
assert_equal "Showing",
|
170
|
+
link_to_block_unless_current("Showing", hash_for(order: 'desc', page: '1'))
|
171
|
+
assert_equal "Showing",
|
172
|
+
link_to_block_unless_current("Showing", "http://www.example.com/?order=desc&page=1")
|
173
|
+
|
174
|
+
@request = request_for_url("/?order=desc")
|
175
|
+
|
176
|
+
assert_equal %{<a href="/?order=asc">Showing</a>},
|
177
|
+
link_to_block_unless_current("Showing", hash_for(order: :asc))
|
178
|
+
assert_equal %{<a href="http://www.example.com/?order=asc">Showing</a>},
|
179
|
+
link_to_block_unless_current("Showing", "http://www.example.com/?order=asc")
|
180
|
+
|
181
|
+
@request = request_for_url("/?order=desc")
|
182
|
+
assert_equal %{<a href="/?order=desc&page=2\">Showing</a>},
|
183
|
+
link_to_block_unless_current("Showing", hash_for(order: "desc", page: 2))
|
184
|
+
assert_equal %{<a href="http://www.example.com/?order=desc&page=2">Showing</a>},
|
185
|
+
link_to_block_unless_current("Showing", "http://www.example.com/?order=desc&page=2")
|
186
|
+
|
187
|
+
@request = request_for_url("/show")
|
188
|
+
|
189
|
+
assert_equal %{<a href="/">Listing</a>},
|
190
|
+
link_to_block_unless_current("Listing", url_hash)
|
191
|
+
assert_equal %{<a href="http://www.example.com/">Listing</a>},
|
192
|
+
link_to_block_unless_current("Listing", "http://www.example.com/")
|
193
|
+
assert_equal %{<a href="/"><span>Example site</span></a>},
|
194
|
+
link_to_block_unless_current(url_hash) { content_tag(:span, 'Example site') }
|
195
|
+
end
|
196
|
+
|
11
197
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionview-link_to_block
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sanemat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -46,11 +46,16 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
+
- .travis.yml
|
50
|
+
- Appraisals
|
49
51
|
- Gemfile
|
50
52
|
- LICENSE.txt
|
51
53
|
- README.md
|
52
54
|
- Rakefile
|
53
55
|
- actionview-link_to_block.gemspec
|
56
|
+
- gemfiles/actionpack_3_2.gemfile
|
57
|
+
- gemfiles/actionpack_4_0.gemfile
|
58
|
+
- gemfiles/actionview_4_1.gemfile
|
54
59
|
- lib/action_view/link_to_block/link_to_block.rb
|
55
60
|
- lib/action_view/link_to_block/railtie.rb
|
56
61
|
- lib/action_view/link_to_block/version.rb
|