helperful 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +71 -0
- data/LICENSE.rdoc +24 -0
- data/Manifest +29 -0
- data/README.rdoc +211 -0
- data/Rakefile +47 -0
- data/helperful.gemspec +40 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/helperful.rb +46 -0
- data/lib/helperful/affiliations_helper.rb +44 -0
- data/lib/helperful/asset_tag_helper.rb +105 -0
- data/lib/helperful/content_helper.rb +105 -0
- data/lib/helperful/deprecations.rb +26 -0
- data/lib/helperful/javascript_helper.rb +84 -0
- data/lib/helperful/title_helper.rb +79 -0
- data/lib/helperful/version.rb +32 -0
- data/rails/init.rb +5 -0
- data/tasks/helperful_tasks.rake +4 -0
- data/test/affiliations_helper_test.rb +12 -0
- data/test/asset_tag_helper_test.rb +77 -0
- data/test/content_helper_test.rb +52 -0
- data/test/fixtures/content/has_content.html.erb +4 -0
- data/test/fixtures/content/has_content_is_called_alone.html.erb +1 -0
- data/test/fixtures/layouts/has_content.html.erb +3 -0
- data/test/helperful_test.rb +55 -0
- data/test/javascript_helper_test.rb +91 -0
- data/test/test_helper.rb +47 -0
- data/test/title_helper_test.rb +69 -0
- data/uninstall.rb +1 -0
- metadata +134 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# = Helperful
|
3
|
+
#
|
4
|
+
# A collection of useful Rails helpers.
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# Category:: Rails
|
8
|
+
# Package:: Helperful
|
9
|
+
# Author:: Simone Carletti <weppos@weppos.net>
|
10
|
+
# Copyright:: 2008-2009 The Authors
|
11
|
+
# License:: MIT License
|
12
|
+
#
|
13
|
+
#--
|
14
|
+
#
|
15
|
+
#++
|
16
|
+
|
17
|
+
|
18
|
+
module Helperful
|
19
|
+
|
20
|
+
module Version
|
21
|
+
MAJOR = 0
|
22
|
+
MINOR = 5
|
23
|
+
TINY = 2
|
24
|
+
|
25
|
+
STRING = [MAJOR, MINOR, TINY].join('.')
|
26
|
+
end
|
27
|
+
|
28
|
+
VERSION = Version::STRING
|
29
|
+
STATUS = 'alpha'
|
30
|
+
BUILD = nil
|
31
|
+
|
32
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AffiliationsHelperTest < ActionView::TestCase
|
4
|
+
tests Helperful::AffiliationsHelper
|
5
|
+
|
6
|
+
def test_tradedoubler_verification_tag
|
7
|
+
assert_equal('<!-- TradeDoubler site verification 112233 -->', tradedoubler_verification_tag('112233'))
|
8
|
+
assert_equal('<!-- TradeDoubler site verification 112233 -->', tradedoubler_verification_tag(112233))
|
9
|
+
assert_equal('<!-- TradeDoubler site verification -->', tradedoubler_verification_tag(nil))
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AssetTagHelperTest < ActionView::TestCase
|
4
|
+
tests Helperful::AssetTagHelper
|
5
|
+
|
6
|
+
def setup
|
7
|
+
ActionController::Base.perform_caching = false
|
8
|
+
ActionController::Base.asset_host = nil
|
9
|
+
ENV.delete('RAILS_ASSET_ID')
|
10
|
+
|
11
|
+
@controller = Class.new do
|
12
|
+
attr_accessor :request
|
13
|
+
def url_for(*args)
|
14
|
+
case arg = args.first
|
15
|
+
when String
|
16
|
+
arg
|
17
|
+
when Hash
|
18
|
+
"http://www.example.com?#{arg.to_param}"
|
19
|
+
else
|
20
|
+
"http://www.example.com"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end.new
|
24
|
+
|
25
|
+
@request = Class.new do
|
26
|
+
def protocol() 'http://' end
|
27
|
+
def ssl?() false end
|
28
|
+
def host_with_port() 'localhost' end
|
29
|
+
end.new
|
30
|
+
|
31
|
+
@controller.request = @request
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
CanonicalToTag = {
|
36
|
+
%(canonical_link_tag) => %(<link href="http://www.example.com?only_path=false" rel="canonical" />),
|
37
|
+
%(canonical_link_tag(:action => "show")) => %(<link href="http://www.example.com?action=show&only_path=false" rel="canonical" />),
|
38
|
+
%(canonical_link_tag("http://localhost/show")) => %(<link href="http://localhost/show" rel="canonical" />),
|
39
|
+
}
|
40
|
+
|
41
|
+
test "canonical_link_tag" do
|
42
|
+
CanonicalToTag.each do |method, tag|
|
43
|
+
assert_dom_equal(tag, eval(method))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
JavascriptToTag = {
|
49
|
+
%(javascript) => %(javascript_include_tag),
|
50
|
+
%(javascript(["foo", "bar"])) => %(javascript_include_tag(["foo", "bar"])),
|
51
|
+
%(javascript("foo", "bar")) => %(javascript_include_tag("foo", "bar")),
|
52
|
+
%(javascript("foo", "bar", :cache => true)) => %(javascript_include_tag("foo", "bar", :cache => true)),
|
53
|
+
}
|
54
|
+
|
55
|
+
test "javascript" do
|
56
|
+
JavascriptToTag.each do |method, content|
|
57
|
+
expects(:content_for).with(:head, eval(content))
|
58
|
+
eval(method)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
StylesheetToTag = {
|
64
|
+
%(stylesheet) => %(stylesheet_link_tag),
|
65
|
+
%(stylesheet(["foo", "bar"])) => %(stylesheet_link_tag(["foo", "bar"])),
|
66
|
+
%(stylesheet("foo", "bar")) => %(stylesheet_link_tag("foo", "bar")),
|
67
|
+
%(stylesheet("foo", "bar", :cache => true)) => %(stylesheet_link_tag("foo", "bar", :cache => true)),
|
68
|
+
}
|
69
|
+
|
70
|
+
test "stylesheet" do
|
71
|
+
StylesheetToTag.each do |method, content|
|
72
|
+
expects(:content_for).with(:head, eval(content))
|
73
|
+
eval(method)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContentController < ActionController::Base
|
4
|
+
def self.controller_name; "content"; end
|
5
|
+
def self.controller_path; "content"; end
|
6
|
+
|
7
|
+
helperful "content"
|
8
|
+
|
9
|
+
def has_content
|
10
|
+
render :layout => "has_content"
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_content_is_called_alone
|
14
|
+
render :layout => false
|
15
|
+
end
|
16
|
+
|
17
|
+
def rescue_action(e) raise end
|
18
|
+
end
|
19
|
+
|
20
|
+
ContentController.view_paths = [ File.dirname(__FILE__) + "/fixtures/" ]
|
21
|
+
|
22
|
+
|
23
|
+
class ContentTest < ActionController::TestCase
|
24
|
+
|
25
|
+
# Compatibility workaround for Rails ~> 2.2.0
|
26
|
+
tests ContentController
|
27
|
+
|
28
|
+
def setup
|
29
|
+
@controller = ContentController.new
|
30
|
+
@request = ActionController::TestRequest.new
|
31
|
+
@response = ActionController::TestResponse.new
|
32
|
+
|
33
|
+
@request.host = "test.host"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_has_content
|
37
|
+
get :has_content
|
38
|
+
assert_equal %Q{
|
39
|
+
`one' has content: this is :one for :one\n
|
40
|
+
`two' has content: this is 'two' for 'two'\n
|
41
|
+
\n
|
42
|
+
`four' has content: this is 'four' for :four\n
|
43
|
+
`five' has content: this is 'five' for :five\n}, @response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_has_content_is_called_alone
|
47
|
+
get :has_content_is_called_alone
|
48
|
+
assert_response :success
|
49
|
+
assert_equal('bar', @response.body)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<% if has_content? :foo %>foo<% end %><% if !has_content? :bar %>bar<% end %>
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HelperfulController < ActionController::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
%w(First Second Third).each do |name|
|
7
|
+
class_eval <<-RUBY
|
8
|
+
module Helperful::#{name}Helper
|
9
|
+
end
|
10
|
+
RUBY
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class HelperfulTest < Test::Unit::TestCase
|
15
|
+
|
16
|
+
def setup
|
17
|
+
@controller = HelperfulController
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_helperful_should_delegate_module
|
21
|
+
@controller.expects(:helper).with(Helperful::FirstHelper)
|
22
|
+
@controller.helperful(Helperful::FirstHelper)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_helperful_should_delegate_modules
|
26
|
+
@controller.expects(:helper).with(Helperful::FirstHelper)
|
27
|
+
@controller.expects(:helper).with(Helperful::SecondHelper)
|
28
|
+
@controller.helperful(Helperful::FirstHelper, Helperful::SecondHelper)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_helperful_should_prepend_namespace_and_delegate_string_symbol
|
32
|
+
@controller.expects(:helper).with('helperful/first')
|
33
|
+
@controller.helperful('first')
|
34
|
+
|
35
|
+
@controller.expects(:helper).with('helperful/first')
|
36
|
+
@controller.helperful(:first)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_helperful_should_prepend_namespace_and_delegate_strings_symbols
|
40
|
+
@controller.expects(:helper).with('helperful/first')
|
41
|
+
@controller.helperful('first')
|
42
|
+
|
43
|
+
@controller.expects(:helper).with('helperful/first')
|
44
|
+
@controller.helperful(:first)
|
45
|
+
|
46
|
+
@controller.expects(:helper).with('helperful/first')
|
47
|
+
@controller.expects(:helper).with('helperful/second')
|
48
|
+
@controller.helperful('first', 'second')
|
49
|
+
|
50
|
+
@controller.expects(:helper).with('helperful/first')
|
51
|
+
@controller.expects(:helper).with('helperful/second')
|
52
|
+
@controller.helperful(:first, :second)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class JavascriptHelperTest < ActionView::TestCase
|
4
|
+
tests Helperful::JavascriptHelper
|
5
|
+
|
6
|
+
attr_accessor :output_buffer
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@content_for_name = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_javascript_content_for
|
13
|
+
javascript_content_for(:name, "alert('hello')")
|
14
|
+
|
15
|
+
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
16
|
+
@content_for_name
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_javascript_content_for_with_block_in_erb
|
20
|
+
__in_erb_template = ''
|
21
|
+
javascript_content_for(:name) { concat "alert('hello')" }
|
22
|
+
|
23
|
+
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
24
|
+
@content_for_name
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_javascript_content_for_with_block_out_of_erb
|
28
|
+
javascript_content_for(:name) { "alert('hello')" }
|
29
|
+
|
30
|
+
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
31
|
+
@content_for_name
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_javascript_content_for_with_options
|
35
|
+
javascript_content_for(:name, "alert('hello')", :id => "the_js_tag")
|
36
|
+
|
37
|
+
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
38
|
+
@content_for_name
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_javascript_content_for_with_options_and_block_in_erb
|
42
|
+
__in_erb_template = ''
|
43
|
+
javascript_content_for(:name, :id => "the_js_tag") { concat "alert('hello')" }
|
44
|
+
|
45
|
+
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
46
|
+
@content_for_name
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_javascript_content_for_with_options_and_block_out_of_erb
|
50
|
+
javascript_content_for(:name, :id => "the_js_tag") { "alert('hello')" }
|
51
|
+
|
52
|
+
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
53
|
+
@content_for_name
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_javascript_content_for_with_subsequent_calls
|
57
|
+
javascript_content_for(:name, "alert('hello')")
|
58
|
+
javascript_content_for(:name, "alert('world')")
|
59
|
+
|
60
|
+
expected = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>"
|
61
|
+
expected += "<script type=\"text/javascript\">\n//<![CDATA[\nalert('world')\n//]]>\n</script>"
|
62
|
+
|
63
|
+
assert_dom_equal expected,
|
64
|
+
@content_for_name
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_javascript_content_for_with_subsequent_calls_and_block_in_erb
|
68
|
+
__in_erb_template = ''
|
69
|
+
|
70
|
+
javascript_content_for(:name) { concat "alert('hello')" }
|
71
|
+
javascript_content_for(:name) { concat "alert('world')" }
|
72
|
+
|
73
|
+
expected = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>"
|
74
|
+
expected += "<script type=\"text/javascript\">\n//<![CDATA[\nalert('world')\n//]]>\n</script>"
|
75
|
+
|
76
|
+
assert_dom_equal expected,
|
77
|
+
@content_for_name
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_javascript_content_for_with_subsequent_calls_and_block_out_of_erb
|
81
|
+
javascript_content_for(:name) { "alert('hello')" }
|
82
|
+
javascript_content_for(:name) { "alert('world')" }
|
83
|
+
|
84
|
+
expected = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>"
|
85
|
+
expected += "<script type=\"text/javascript\">\n//<![CDATA[\nalert('world')\n//]]>\n</script>"
|
86
|
+
|
87
|
+
assert_dom_equal expected,
|
88
|
+
@content_for_name
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# = Helperful
|
3
|
+
#
|
4
|
+
# A collection of useful Rails helpers.
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# Category:: Rails
|
8
|
+
# Package:: Helperful
|
9
|
+
# Author:: Simone Carletti <weppos@weppos.net>
|
10
|
+
# Copyright:: 2008-2009 The Authors
|
11
|
+
# License:: MIT License
|
12
|
+
#
|
13
|
+
#--
|
14
|
+
#
|
15
|
+
#++
|
16
|
+
|
17
|
+
|
18
|
+
require 'rubygems'
|
19
|
+
gem 'rails', '~>2.3.0'
|
20
|
+
gem 'mocha', '0.9.7'
|
21
|
+
|
22
|
+
# Remember! Due to some Mocha internal changes,
|
23
|
+
# Rails 2.2.x requires Mocha 0.9.5 and
|
24
|
+
# Rails 2.3.x requires Mocha 0.9.7
|
25
|
+
# gem 'rails', '2.2.2'
|
26
|
+
# gem 'mocha', '0.9.5'
|
27
|
+
|
28
|
+
require 'mocha'
|
29
|
+
require 'test/unit'
|
30
|
+
require 'active_support'
|
31
|
+
require 'action_controller'
|
32
|
+
require 'action_controller/cgi_ext'
|
33
|
+
require 'action_controller/test_process'
|
34
|
+
require 'action_view/test_case'
|
35
|
+
|
36
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
37
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
38
|
+
|
39
|
+
RAILS_ROOT = '.' unless defined? RAILS_ROOT
|
40
|
+
RAILS_ENV = 'test' unless defined? RAILS_ENV
|
41
|
+
|
42
|
+
ActionController::Base.logger = nil
|
43
|
+
ActionController::Routing::Routes.reload rescue nil
|
44
|
+
|
45
|
+
|
46
|
+
# Unit tests for Helpers are based on unit tests created and developed by Rails core team.
|
47
|
+
# See action_pack/test/abstract_unit for more details.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HelperfulTitleHelperTest < ActionView::TestCase
|
4
|
+
tests Helperful::TitleHelper
|
5
|
+
|
6
|
+
def setup
|
7
|
+
reset!
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_title_should_accepts_zero_or_more_params
|
11
|
+
assert_nothing_raised { title }
|
12
|
+
assert_nothing_raised { title('One') }
|
13
|
+
assert_nothing_raised { title('One', 'Two', 'Three') }
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_title_should_return_content
|
17
|
+
assert_equal('', title)
|
18
|
+
assert_equal('One', title('One'))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_title_should_return_empty_string_if_empty
|
22
|
+
assert_equal('', title)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_title_should_store_content
|
26
|
+
assert_equal('', title)
|
27
|
+
assert_equal('One', title('One'))
|
28
|
+
assert_equal('One', title)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_title_should_join_content
|
32
|
+
assert_equal('', title)
|
33
|
+
assert_equal('One', title('One'))
|
34
|
+
assert_equal('One - Two', title('Two'))
|
35
|
+
assert_equal('One - Two - Three - Four', title('Three', 'Four'))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_title_should_join_content_with_separator
|
39
|
+
assert_equal('One - Two', title('One', 'Two'))
|
40
|
+
assert_equal('One | Two', title(:separator => ' | '))
|
41
|
+
assert_equal('One x Two x Three', title('Three', :separator => ' x '))
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_title_should_append_headline_to_content
|
45
|
+
assert_equal('One - Two', title('One', 'Two'))
|
46
|
+
assert_equal('One - Two - Cool!', title(:headline => 'Cool!'))
|
47
|
+
assert_equal('One - Two - Three - Yeah!', title('Three', :headline => 'Yeah!'))
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_title_should_append_site_to_content
|
51
|
+
assert_equal('One - Two', title('One', 'Two'))
|
52
|
+
assert_equal('One - Two - Cool!', title(:site => 'Cool!'))
|
53
|
+
assert_equal('One - Two - Three - Yeah!', title('Three', :site => 'Yeah!'))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_title_should_append_site_then_headline
|
57
|
+
assert_equal('One - Two', title('One', 'Two'))
|
58
|
+
assert_equal('One - Two - Cool!', title(:site => 'Cool!'))
|
59
|
+
assert_equal('One - Two - Cool! - Yeah!', title(:headline => 'Yeah!'))
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def reset!
|
66
|
+
title(nil)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|