actionview-link_to_blank 0.0.4 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/action_view/link_to_blank/link_to_blank.rb +20 -0
- data/lib/action_view/link_to_blank/version.rb +1 -1
- data/test/test_actionview-link_to_blank.rb +73 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f3a650615b02d084e7d4026cf8a3217c52f547a
|
4
|
+
data.tar.gz: c510c1f4568b43f465922beba4bc97fe82fa0a1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdb815683e47c13a0e967959f60dd660babae535116145d95e48efc182bcd0a3f2396e0bdc5d4d467289f74cd11a33b12c24762950f43c383f0417b35492a2ad
|
7
|
+
data.tar.gz: 642595ee739f29289a17ce75b03600a56d1678c3144a001996e2050b63dad648e495b80b9e1e0f74b1ba4d56dae861acc171542b3f735853d85a6e6b03404bb4
|
data/README.md
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/sanemat/actionview-link_to_blank)
|
4
4
|
|
5
|
-
Add helper method, link_to with target _blank
|
5
|
+
Add helper method, link_to_blank, equal to link_to with target _blank
|
6
|
+
Add helper method, link_to_blank_if, link_to_blank_unless, link_to_blank_unless_current.
|
7
|
+
This is symmetrical to link_to_if, link_to_unless, link_to_unless_current.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -40,6 +42,17 @@ Use the `link_to_blank` helper method, equal `link_to('foo', target: '_blank')`
|
|
40
42
|
# name
|
41
43
|
end
|
42
44
|
|
45
|
+
link_to_blank_if(condition, name, options = {}, html_options = {})
|
46
|
+
# if condition is true, create a link tag
|
47
|
+
# otherwise only name
|
48
|
+
|
49
|
+
link_to_blank_unless(condition, name, options = {}, html_options = {})
|
50
|
+
# if condition is true, only name
|
51
|
+
# otherwise create a link tag
|
52
|
+
|
53
|
+
link_to_blank_unless_current(name, options = {}, html_options = {})
|
54
|
+
# create a link tag of the given name unless current page is the same
|
55
|
+
|
43
56
|
### Examples
|
44
57
|
|
45
58
|
link_to_blank "Profile", profile_path(@profile)
|
@@ -18,6 +18,26 @@ module LinkToBlank
|
|
18
18
|
link_to(name, options, html_options)
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
def link_to_blank_unless(condition, name, options = {}, html_options = {}, &block)
|
23
|
+
if condition
|
24
|
+
if block_given?
|
25
|
+
block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
|
26
|
+
else
|
27
|
+
name
|
28
|
+
end
|
29
|
+
else
|
30
|
+
link_to_blank(name, options, html_options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def link_to_blank_if(condition, name, options = {}, html_options = {}, &block)
|
35
|
+
link_to_blank_unless !condition, name, options, html_options, &block
|
36
|
+
end
|
37
|
+
|
38
|
+
def link_to_blank_unless_current(name, options = {}, html_options = {}, &block)
|
39
|
+
link_to_blank_unless current_page?(options), name, options, html_options, &block
|
40
|
+
end
|
21
41
|
end
|
22
42
|
end
|
23
43
|
end
|
@@ -274,6 +274,79 @@ class TestActionViewLinkToBlank < MiniTest::Unit::TestCase
|
|
274
274
|
assert_dom_equal %{<a href="http://www.example.com" target="override">Hello</a>}, link_to_blank("Hello", "http://www.example.com", target: 'override')
|
275
275
|
end
|
276
276
|
|
277
|
+
def test_link_to_unless
|
278
|
+
assert_equal "Showing", link_to_blank_unless(true, "Showing", url_hash)
|
279
|
+
|
280
|
+
assert_dom_equal %{<a href="/" target="_blank">Listing</a>},
|
281
|
+
link_to_blank_unless(false, "Listing", url_hash)
|
282
|
+
|
283
|
+
assert_equal "Showing", link_to_blank_unless(true, "Showing", url_hash)
|
284
|
+
|
285
|
+
assert_equal "<strong>Showing</strong>",
|
286
|
+
link_to_blank_unless(true, "Showing", url_hash) { |name|
|
287
|
+
"<strong>#{name}</strong>".html_safe
|
288
|
+
}
|
289
|
+
|
290
|
+
assert_equal "test",
|
291
|
+
link_to_blank_unless(true, "Showing", url_hash) {
|
292
|
+
"test"
|
293
|
+
}
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_link_to_if
|
297
|
+
assert_equal "Showing", link_to_blank_if(false, "Showing", url_hash)
|
298
|
+
assert_dom_equal %{<a href="/" target="_blank">Listing</a>}, link_to_blank_if(true, "Listing", url_hash)
|
299
|
+
assert_equal "Showing", link_to_blank_if(false, "Showing", url_hash)
|
300
|
+
end
|
301
|
+
|
302
|
+
def request_for_url(url, opts = {})
|
303
|
+
env = Rack::MockRequest.env_for("http://www.example.com#{url}", opts)
|
304
|
+
ActionDispatch::Request.new(env)
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_link_unless_current
|
308
|
+
@request = request_for_url("/")
|
309
|
+
|
310
|
+
assert_equal "Showing",
|
311
|
+
link_to_blank_unless_current("Showing", url_hash)
|
312
|
+
assert_equal "Showing",
|
313
|
+
link_to_blank_unless_current("Showing", "http://www.example.com/")
|
314
|
+
|
315
|
+
@request = request_for_url("/?order=desc")
|
316
|
+
|
317
|
+
assert_equal "Showing",
|
318
|
+
link_to_blank_unless_current("Showing", url_hash)
|
319
|
+
assert_equal "Showing",
|
320
|
+
link_to_blank_unless_current("Showing", "http://www.example.com/")
|
321
|
+
|
322
|
+
@request = request_for_url("/?order=desc&page=1")
|
323
|
+
|
324
|
+
assert_equal "Showing",
|
325
|
+
link_to_blank_unless_current("Showing", hash_for(order: 'desc', page: '1'))
|
326
|
+
assert_equal "Showing",
|
327
|
+
link_to_blank_unless_current("Showing", "http://www.example.com/?order=desc&page=1")
|
328
|
+
|
329
|
+
@request = request_for_url("/?order=desc")
|
330
|
+
|
331
|
+
assert_equal %{<a href="/?order=asc" target="_blank">Showing</a>},
|
332
|
+
link_to_blank_unless_current("Showing", hash_for(order: :asc))
|
333
|
+
assert_equal %{<a href="http://www.example.com/?order=asc" target="_blank">Showing</a>},
|
334
|
+
link_to_blank_unless_current("Showing", "http://www.example.com/?order=asc")
|
335
|
+
|
336
|
+
@request = request_for_url("/?order=desc")
|
337
|
+
assert_equal %{<a href="/?order=desc&page=2\" target="_blank">Showing</a>},
|
338
|
+
link_to_blank_unless_current("Showing", hash_for(order: "desc", page: 2))
|
339
|
+
assert_equal %{<a href="http://www.example.com/?order=desc&page=2" target="_blank">Showing</a>},
|
340
|
+
link_to_blank_unless_current("Showing", "http://www.example.com/?order=desc&page=2")
|
341
|
+
|
342
|
+
@request = request_for_url("/show")
|
343
|
+
|
344
|
+
assert_equal %{<a href="/" target="_blank">Listing</a>},
|
345
|
+
link_to_blank_unless_current("Listing", url_hash)
|
346
|
+
assert_equal %{<a href="http://www.example.com/" target="_blank">Listing</a>},
|
347
|
+
link_to_blank_unless_current("Listing", "http://www.example.com/")
|
348
|
+
end
|
349
|
+
|
277
350
|
private
|
278
351
|
# MiniTest does not have build_message method, so I copy from below:
|
279
352
|
# https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/assertions/dom.rb
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionview-link_to_blank
|
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-04-
|
11
|
+
date: 2013-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|