padrino-helpers-cj 0.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.yardopts +1 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +239 -0
- data/Rakefile +5 -0
- data/lib/padrino-helpers.rb +62 -0
- data/lib/padrino-helpers/asset_tag_helpers.rb +390 -0
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +283 -0
- data/lib/padrino-helpers/form_builder/deprecated_builder_methods.rb +92 -0
- data/lib/padrino-helpers/form_builder/standard_form_builder.rb +40 -0
- data/lib/padrino-helpers/form_helpers.rb +633 -0
- data/lib/padrino-helpers/form_helpers/errors.rb +138 -0
- data/lib/padrino-helpers/form_helpers/options.rb +97 -0
- data/lib/padrino-helpers/form_helpers/security.rb +70 -0
- data/lib/padrino-helpers/format_helpers.rb +372 -0
- data/lib/padrino-helpers/locale/cs.yml +103 -0
- data/lib/padrino-helpers/locale/da.yml +91 -0
- data/lib/padrino-helpers/locale/de.yml +81 -0
- data/lib/padrino-helpers/locale/en.yml +103 -0
- data/lib/padrino-helpers/locale/es.yml +103 -0
- data/lib/padrino-helpers/locale/fr.yml +79 -0
- data/lib/padrino-helpers/locale/hu.yml +103 -0
- data/lib/padrino-helpers/locale/it.yml +89 -0
- data/lib/padrino-helpers/locale/ja.yml +103 -0
- data/lib/padrino-helpers/locale/lv.yml +103 -0
- data/lib/padrino-helpers/locale/nl.yml +82 -0
- data/lib/padrino-helpers/locale/no.yml +91 -0
- data/lib/padrino-helpers/locale/pl.yml +95 -0
- data/lib/padrino-helpers/locale/pt_br.yml +103 -0
- data/lib/padrino-helpers/locale/ro.yml +103 -0
- data/lib/padrino-helpers/locale/ru.yml +103 -0
- data/lib/padrino-helpers/locale/sv.yml +103 -0
- data/lib/padrino-helpers/locale/tr.yml +103 -0
- data/lib/padrino-helpers/locale/uk.yml +103 -0
- data/lib/padrino-helpers/locale/zh_cn.yml +103 -0
- data/lib/padrino-helpers/locale/zh_tw.yml +103 -0
- data/lib/padrino-helpers/number_helpers.rb +283 -0
- data/lib/padrino-helpers/output_helpers.rb +226 -0
- data/lib/padrino-helpers/output_helpers/abstract_handler.rb +61 -0
- data/lib/padrino-helpers/output_helpers/erb_handler.rb +27 -0
- data/lib/padrino-helpers/output_helpers/haml_handler.rb +25 -0
- data/lib/padrino-helpers/output_helpers/slim_handler.rb +18 -0
- data/lib/padrino-helpers/render_helpers.rb +63 -0
- data/lib/padrino-helpers/tag_helpers.rb +294 -0
- data/lib/padrino-helpers/translation_helpers.rb +36 -0
- data/lib/padrino/rendering.rb +397 -0
- data/lib/padrino/rendering/erubis_template.rb +66 -0
- data/lib/padrino/rendering/haml_template.rb +26 -0
- data/lib/padrino/rendering/slim_template.rb +20 -0
- data/padrino-helpers.gemspec +29 -0
- data/test/fixtures/apps/.components +6 -0
- data/test/fixtures/apps/.gitignore +7 -0
- data/test/fixtures/apps/render.rb +25 -0
- data/test/fixtures/apps/views/article/comment/show.slim +1 -0
- data/test/fixtures/apps/views/blog/post.erb +1 -0
- data/test/fixtures/apps/views/layouts/specific.erb +1 -0
- data/test/fixtures/apps/views/test/post.erb +1 -0
- data/test/fixtures/layouts/layout.erb +1 -0
- data/test/fixtures/markup_app/app.rb +87 -0
- data/test/fixtures/markup_app/views/button_to.erb +8 -0
- data/test/fixtures/markup_app/views/button_to.haml +5 -0
- data/test/fixtures/markup_app/views/button_to.slim +6 -0
- data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
- data/test/fixtures/markup_app/views/capture_concat.haml +12 -0
- data/test/fixtures/markup_app/views/capture_concat.slim +12 -0
- data/test/fixtures/markup_app/views/content_for.erb +23 -0
- data/test/fixtures/markup_app/views/content_for.haml +19 -0
- data/test/fixtures/markup_app/views/content_for.slim +19 -0
- data/test/fixtures/markup_app/views/content_tag.erb +13 -0
- data/test/fixtures/markup_app/views/content_tag.haml +11 -0
- data/test/fixtures/markup_app/views/content_tag.slim +11 -0
- data/test/fixtures/markup_app/views/current_engine.erb +5 -0
- data/test/fixtures/markup_app/views/current_engine.haml +5 -0
- data/test/fixtures/markup_app/views/current_engine.slim +5 -0
- data/test/fixtures/markup_app/views/fields_for.erb +20 -0
- data/test/fixtures/markup_app/views/fields_for.haml +15 -0
- data/test/fixtures/markup_app/views/fields_for.slim +15 -0
- data/test/fixtures/markup_app/views/form_for.erb +72 -0
- data/test/fixtures/markup_app/views/form_for.haml +59 -0
- data/test/fixtures/markup_app/views/form_for.slim +59 -0
- data/test/fixtures/markup_app/views/form_tag.erb +95 -0
- data/test/fixtures/markup_app/views/form_tag.haml +78 -0
- data/test/fixtures/markup_app/views/form_tag.slim +79 -0
- data/test/fixtures/markup_app/views/link_to.erb +5 -0
- data/test/fixtures/markup_app/views/link_to.haml +4 -0
- data/test/fixtures/markup_app/views/link_to.slim +4 -0
- data/test/fixtures/markup_app/views/mail_to.erb +3 -0
- data/test/fixtures/markup_app/views/mail_to.haml +3 -0
- data/test/fixtures/markup_app/views/mail_to.slim +3 -0
- data/test/fixtures/markup_app/views/meta_tag.erb +3 -0
- data/test/fixtures/markup_app/views/meta_tag.haml +3 -0
- data/test/fixtures/markup_app/views/meta_tag.slim +3 -0
- data/test/fixtures/markup_app/views/partials/_erb.erb +1 -0
- data/test/fixtures/markup_app/views/partials/_haml.haml +1 -0
- data/test/fixtures/markup_app/views/partials/_slim.slim +1 -0
- data/test/fixtures/markup_app/views/simple_partial.erb +1 -0
- data/test/fixtures/markup_app/views/simple_partial.haml +1 -0
- data/test/fixtures/markup_app/views/simple_partial.slim +1 -0
- data/test/fixtures/render_app/app.rb +94 -0
- data/test/fixtures/render_app/views/_deep.erb +3 -0
- data/test/fixtures/render_app/views/_deep.haml +2 -0
- data/test/fixtures/render_app/views/_deep.slim +2 -0
- data/test/fixtures/render_app/views/_partial_block_erb.erb +10 -0
- data/test/fixtures/render_app/views/_partial_block_haml.haml +7 -0
- data/test/fixtures/render_app/views/_partial_block_slim.slim +7 -0
- data/test/fixtures/render_app/views/_unsafe.html.builder +2 -0
- data/test/fixtures/render_app/views/_unsafe_object.html.builder +2 -0
- data/test/fixtures/render_app/views/current_engine.haml +5 -0
- data/test/fixtures/render_app/views/current_engines/_erb.erb +1 -0
- data/test/fixtures/render_app/views/current_engines/_haml.haml +1 -0
- data/test/fixtures/render_app/views/current_engines/_slim.slim +1 -0
- data/test/fixtures/render_app/views/double_capture_erb.erb +3 -0
- data/test/fixtures/render_app/views/double_capture_haml.haml +2 -0
- data/test/fixtures/render_app/views/double_capture_slim.slim +2 -0
- data/test/fixtures/render_app/views/erb/test.erb +1 -0
- data/test/fixtures/render_app/views/explicit_engine.haml +5 -0
- data/test/fixtures/render_app/views/haml/test.haml +1 -0
- data/test/fixtures/render_app/views/render_block_erb.erb +5 -0
- data/test/fixtures/render_app/views/render_block_haml.haml +4 -0
- data/test/fixtures/render_app/views/render_block_slim.slim +4 -0
- data/test/fixtures/render_app/views/ruby_block_capture_erb.erb +1 -0
- data/test/fixtures/render_app/views/ruby_block_capture_haml.haml +1 -0
- data/test/fixtures/render_app/views/ruby_block_capture_slim.slim +1 -0
- data/test/fixtures/render_app/views/template/_user.haml +7 -0
- data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
- data/test/fixtures/render_app/views/template/some_template.haml +2 -0
- data/test/fixtures/render_app/views/wrong_capture_erb.erb +3 -0
- data/test/fixtures/render_app/views/wrong_capture_haml.haml +2 -0
- data/test/fixtures/render_app/views/wrong_capture_slim.slim +2 -0
- data/test/helper.rb +131 -0
- data/test/test_asset_tag_helpers.rb +406 -0
- data/test/test_form_builder.rb +1216 -0
- data/test/test_form_helpers.rb +1056 -0
- data/test/test_format_helpers.rb +251 -0
- data/test/test_helpers.rb +10 -0
- data/test/test_locale.rb +20 -0
- data/test/test_number_helpers.rb +142 -0
- data/test/test_output_helpers.rb +157 -0
- data/test/test_render_helpers.rb +215 -0
- data/test/test_rendering.rb +694 -0
- data/test/test_rendering_extensions.rb +14 -0
- data/test/test_tag_helpers.rb +131 -0
- metadata +241 -0
@@ -0,0 +1 @@
|
|
1
|
+
<%= current_engine %>
|
@@ -0,0 +1 @@
|
|
1
|
+
=current_engine
|
@@ -0,0 +1 @@
|
|
1
|
+
= current_engine
|
@@ -0,0 +1 @@
|
|
1
|
+
<p class="erb"><%= partial 'partials/erb', :engine => "erb" %></p>
|
@@ -0,0 +1 @@
|
|
1
|
+
%p.haml= partial 'partials/haml', :engine => "haml"
|
@@ -0,0 +1 @@
|
|
1
|
+
p.slim= partial 'partials/slim', :engine => "slim"
|
@@ -0,0 +1,94 @@
|
|
1
|
+
PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
|
2
|
+
RACK_ENV = 'test' unless defined? RACK_ENV
|
3
|
+
|
4
|
+
require 'padrino-core'
|
5
|
+
|
6
|
+
class RenderUser
|
7
|
+
attr_accessor :name
|
8
|
+
def initialize(name); @name = name; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class RenderDemo < Padrino::Application
|
12
|
+
register Padrino::Helpers
|
13
|
+
|
14
|
+
configure do
|
15
|
+
set :logging, false
|
16
|
+
set :padrino_logging, false
|
17
|
+
set :environment, :test
|
18
|
+
end
|
19
|
+
|
20
|
+
# get current engines from partials
|
21
|
+
get '/current_engine' do
|
22
|
+
render :current_engine
|
23
|
+
end
|
24
|
+
|
25
|
+
# get current engines from explicit engine partials
|
26
|
+
get '/explicit_engine' do
|
27
|
+
render :explicit_engine
|
28
|
+
end
|
29
|
+
|
30
|
+
get '/double_capture_:ext' do
|
31
|
+
render "double_capture_#{params[:ext]}"
|
32
|
+
end
|
33
|
+
|
34
|
+
get '/wrong_capture_:ext' do
|
35
|
+
render "wrong_capture_#{params[:ext]}"
|
36
|
+
end
|
37
|
+
|
38
|
+
get '/ruby_block_capture_:ext' do
|
39
|
+
render "ruby_block_capture_#{params[:ext]}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# partial with object
|
43
|
+
get '/partial/object' do
|
44
|
+
partial 'template/user', :object => RenderUser.new('John'), :locals => { :extra => "bar" }
|
45
|
+
end
|
46
|
+
|
47
|
+
# partial with collection
|
48
|
+
get '/partial/collection' do
|
49
|
+
partial 'template/user', :collection => [RenderUser.new('John'), RenderUser.new('Billy')], :locals => { :extra => "bar" }
|
50
|
+
end
|
51
|
+
|
52
|
+
# partial with collection and ext
|
53
|
+
get '/partial/collection.ext' do
|
54
|
+
partial 'template/user.haml', :collection => [RenderUser.new('John'), RenderUser.new('Billy')], :locals => { :extra => "bar" }
|
55
|
+
end
|
56
|
+
|
57
|
+
# partial with locals
|
58
|
+
get '/partial/locals' do
|
59
|
+
partial 'template/user', :locals => { :user => RenderUser.new('John'), :extra => "bar" }
|
60
|
+
end
|
61
|
+
|
62
|
+
# partial starting with forward slash
|
63
|
+
get '/partial/foward_slash' do
|
64
|
+
partial '/template/user', :object => RenderUser.new('John'), :locals => { :extra => "bar" }
|
65
|
+
end
|
66
|
+
|
67
|
+
# partial with unsafe engine
|
68
|
+
get '/partial/unsafe' do
|
69
|
+
block = params[:block] ? proc{ params[:block] } : nil
|
70
|
+
partial 'unsafe.html.builder', &block
|
71
|
+
end
|
72
|
+
|
73
|
+
get '/partial/unsafe_one' do
|
74
|
+
block = params[:block] ? proc{ params[:block] } : nil
|
75
|
+
partial 'unsafe_object', :object => 'Mary', &block
|
76
|
+
end
|
77
|
+
|
78
|
+
get '/partial/unsafe_many' do
|
79
|
+
block = params[:block] ? proc{ params[:block] } : nil
|
80
|
+
partial 'unsafe_object', :collection => ['John', 'Mary'], &block
|
81
|
+
end
|
82
|
+
|
83
|
+
get '/render_block_:ext' do
|
84
|
+
render "render_block_#{params[:ext]}" do
|
85
|
+
content_tag :div, 'go block!'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
get '/partial_block_:ext' do
|
90
|
+
partial "partial_block_#{params[:ext]}" do
|
91
|
+
content_tag :div, 'go block!'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<span><%= current_engine %></span>
|
@@ -0,0 +1 @@
|
|
1
|
+
%span=current_engine
|
@@ -0,0 +1 @@
|
|
1
|
+
span=current_engine
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>This is a <%= @template %> template!</h1>
|
@@ -0,0 +1 @@
|
|
1
|
+
%h1 This is a #{@template} template!
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= content_tag(:a) { content_tag :b, 'c' } %>
|
@@ -0,0 +1 @@
|
|
1
|
+
= content_tag(:a) { content_tag :b, 'c' }
|
@@ -0,0 +1 @@
|
|
1
|
+
= content_tag(:a) { content_tag :b, 'c' }
|
@@ -0,0 +1 @@
|
|
1
|
+
%h1 This is a #{@template} template sent from render_template!
|
data/test/helper.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
|
3
|
+
require File.expand_path('../../../load_paths', __FILE__)
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/pride'
|
6
|
+
require 'mocha/setup'
|
7
|
+
require 'rack/test'
|
8
|
+
require 'webrat'
|
9
|
+
require 'padrino-helpers'
|
10
|
+
require 'active_support/time'
|
11
|
+
require 'builder'
|
12
|
+
|
13
|
+
class MiniTest::Spec
|
14
|
+
include Padrino::Helpers::OutputHelpers
|
15
|
+
include Padrino::Helpers::TagHelpers
|
16
|
+
include Padrino::Helpers::AssetTagHelpers
|
17
|
+
include Rack::Test::Methods
|
18
|
+
include Webrat::Methods
|
19
|
+
include Webrat::Matchers
|
20
|
+
|
21
|
+
Webrat.configure do |config|
|
22
|
+
config.mode = :rack
|
23
|
+
end
|
24
|
+
|
25
|
+
def stop_time_for_test
|
26
|
+
time = Time.now
|
27
|
+
Time.stubs(:now).returns(time)
|
28
|
+
return time
|
29
|
+
end
|
30
|
+
|
31
|
+
# assert_has_tag(:h1, :content => "yellow") { "<h1>yellow</h1>" }
|
32
|
+
# In this case, block is the html to evaluate
|
33
|
+
def assert_has_tag(name, attributes = {}, &block)
|
34
|
+
html = block && block.call
|
35
|
+
assert html.html_safe?, 'html_safe? failed'
|
36
|
+
matcher = HaveSelector.new(name, attributes)
|
37
|
+
raise "Please specify a block!" if html.blank?
|
38
|
+
assert matcher.matches?(html), matcher.failure_message
|
39
|
+
end
|
40
|
+
|
41
|
+
# assert_has_no_tag, tag(:h1, :content => "yellow") { "<h1>green</h1>" }
|
42
|
+
# In this case, block is the html to evaluate
|
43
|
+
def assert_has_no_tag(name, attributes = {}, &block)
|
44
|
+
html = block && block.call
|
45
|
+
attributes.merge!(:count => 0)
|
46
|
+
matcher = HaveSelector.new(name, attributes)
|
47
|
+
raise "Please specify a block!" if html.blank?
|
48
|
+
assert matcher.matches?(html), matcher.failure_message
|
49
|
+
end
|
50
|
+
|
51
|
+
# Asserts that a file matches the pattern
|
52
|
+
def assert_match_in_file(pattern, file)
|
53
|
+
assert File.exist?(file), "File '#{file}' does not exist!"
|
54
|
+
assert_match pattern, File.read(file)
|
55
|
+
end
|
56
|
+
|
57
|
+
# mock_model("Business", :new_record? => true) => <Business>
|
58
|
+
def mock_model(klazz, options={})
|
59
|
+
options.reverse_merge!(:class => klazz, :new_record? => false, :id => 20, :errors => {})
|
60
|
+
record = stub(options)
|
61
|
+
record.stubs(:to_ary => [record])
|
62
|
+
record
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_template(name, content, options={})
|
66
|
+
FileUtils.mkdir_p(File.dirname(__FILE__) + "/views")
|
67
|
+
FileUtils.mkdir_p(File.dirname(__FILE__) + "/views/layouts")
|
68
|
+
path = "/views/#{name}"
|
69
|
+
path += ".#{options.delete(:locale)}" if options[:locale].present?
|
70
|
+
path += ".#{options[:format]}" if options[:format].present?
|
71
|
+
path += ".erb" unless options[:format].to_s =~ /erb|slim|haml|rss|atom|builder/
|
72
|
+
path += ".builder" if options[:format].to_s =~ /rss|atom/
|
73
|
+
file = File.dirname(__FILE__) + path
|
74
|
+
File.open(file, 'w') { |io| io.write content }
|
75
|
+
file
|
76
|
+
end
|
77
|
+
alias :create_view :create_template
|
78
|
+
alias :create_layout :create_template
|
79
|
+
|
80
|
+
def remove_views
|
81
|
+
FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
|
82
|
+
end
|
83
|
+
|
84
|
+
def with_template(name, content, options={})
|
85
|
+
# Build a temp layout
|
86
|
+
template = create_template(name, content, options)
|
87
|
+
yield
|
88
|
+
ensure
|
89
|
+
# Remove temp layout
|
90
|
+
File.unlink(template) rescue nil
|
91
|
+
remove_views
|
92
|
+
end
|
93
|
+
alias :with_view :with_template
|
94
|
+
alias :with_layout :with_template
|
95
|
+
|
96
|
+
def mock_app(base=Padrino::Application.dup, &block)
|
97
|
+
base.register Padrino::Rendering
|
98
|
+
@app = Sinatra.new(base, &block)
|
99
|
+
end
|
100
|
+
|
101
|
+
def app
|
102
|
+
Rack::Lint.new(@app)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Asserts that a file matches the pattern
|
106
|
+
def assert_match_in_file(pattern, file)
|
107
|
+
assert File.exist?(file), "File '#{file}' does not exist!"
|
108
|
+
assert_match pattern, File.read(file)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Delegate other missing methods to response.
|
112
|
+
def method_missing(name, *args, &block)
|
113
|
+
if response && response.respond_to?(name)
|
114
|
+
response.send(name, *args, &block)
|
115
|
+
else
|
116
|
+
super(name, *args, &block)
|
117
|
+
end
|
118
|
+
rescue Rack::Test::Error # no response yet
|
119
|
+
super(name, *args, &block)
|
120
|
+
end
|
121
|
+
|
122
|
+
alias :response :last_response
|
123
|
+
end
|
124
|
+
|
125
|
+
module Webrat
|
126
|
+
module Logging
|
127
|
+
def logger # @private
|
128
|
+
@logger = nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,406 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
|
3
|
+
|
4
|
+
describe "AssetTagHelpers" do
|
5
|
+
include Padrino::Helpers::AssetTagHelpers
|
6
|
+
|
7
|
+
def app
|
8
|
+
MarkupDemo
|
9
|
+
end
|
10
|
+
|
11
|
+
def flash
|
12
|
+
@_flash ||= { :notice => "Demo notice" }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'for #flash_tag method' do
|
16
|
+
it 'should display flash with no given attributes' do
|
17
|
+
assert_has_tag('div.notice', :content => "Demo notice") { flash_tag(:notice) }
|
18
|
+
end
|
19
|
+
it 'should display flash with given attributes' do
|
20
|
+
actual_html = flash_tag(:notice, :class => 'notice', :id => 'notice-area')
|
21
|
+
assert_has_tag('div.notice#notice-area', :content => "Demo notice") { actual_html }
|
22
|
+
end
|
23
|
+
it 'should display multiple flash tags with given attributes' do
|
24
|
+
flash[:error] = 'wrong'
|
25
|
+
flash[:success] = 'okey'
|
26
|
+
actual_html = flash_tag(:success, :warning, :error, :id => 'area')
|
27
|
+
assert_has_tag('div.success#area', :content => flash[:success]) { actual_html }
|
28
|
+
assert_has_tag('div.error#area', :content => flash[:error]) { actual_html }
|
29
|
+
assert_has_no_tag('div.notice') { actual_html }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'for #link_to method' do
|
34
|
+
it 'should display link element with no given attributes' do
|
35
|
+
assert_has_tag('a', :content => "Sign up", :href => '/register') { link_to('Sign up', '/register') }
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should display link element with given attributes' do
|
39
|
+
actual_html = link_to('Sign up', '/register', :class => 'first', :id => 'linky')
|
40
|
+
assert_has_tag('a#linky.first', :content => "Sign up", :href => '/register') { actual_html }
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should display link element with anchor attribute' do
|
44
|
+
actual_html = link_to("Anchor", "/anchor", :anchor => :foo)
|
45
|
+
assert_has_tag('a', :content => "Anchor", :href => '/anchor#foo') { actual_html }
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should display link element with void url and options' do
|
49
|
+
actual_link = link_to('Sign up', :class => "test")
|
50
|
+
assert_has_tag('a', :content => "Sign up", :href => '#', :class => 'test') { actual_link }
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should display link element with remote option' do
|
54
|
+
actual_link = link_to('Sign up', '/register', :remote => true)
|
55
|
+
assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-remote' => 'true') { actual_link }
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should display link element with method option' do
|
59
|
+
actual_link = link_to('Sign up', '/register', :method => :delete)
|
60
|
+
assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-method' => 'delete', :rel => 'nofollow') { actual_link }
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should display link element with confirm option' do
|
64
|
+
actual_link = link_to('Sign up', '/register', :confirm => "Are you sure?")
|
65
|
+
assert_has_tag('a', :content => "Sign up", :href => '/register', 'data-confirm' => 'Are you sure?') { actual_link }
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should display link element with ruby block' do
|
69
|
+
actual_link = link_to('/register', :class => 'first', :id => 'binky') { "Sign up" }
|
70
|
+
assert_has_tag('a#binky.first', :content => "Sign up", :href => '/register') { actual_link }
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should escape the link text' do
|
74
|
+
actual_link = link_to('/register', :class => 'first', :id => 'binky') { "<&>" }
|
75
|
+
assert_has_tag('a#binky.first', :href => '/register') { actual_link }
|
76
|
+
assert_match "<&>", actual_link
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should not escape image_tag' do
|
80
|
+
actual_link = link_to(image_tag("/my/fancy/image.png"), :class => 'first', :id => 'binky')
|
81
|
+
assert_has_tag('img', :src => "/my/fancy/image.png") { actual_link }
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should display link block element in haml' do
|
85
|
+
visit '/haml/link_to'
|
86
|
+
assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
|
87
|
+
assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should display link block element in erb' do
|
91
|
+
visit '/erb/link_to'
|
92
|
+
assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
|
93
|
+
assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should display link block element in slim' do
|
97
|
+
visit '/slim/link_to'
|
98
|
+
assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
|
99
|
+
assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should not double-escape' do
|
103
|
+
actual_link = link_to('test escape', '?a=1&b=2')
|
104
|
+
assert_has_tag('a', :href => '?a=1&b=2') { actual_link }
|
105
|
+
assert_match %r{&}, actual_link
|
106
|
+
refute_match %r{&amp;}, actual_link
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should escape scary things' do
|
110
|
+
actual_link = link_to('test escape<adfs>', '?a=1&b=<script>alert(1)</script>')
|
111
|
+
refute_match('<script', actual_link)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'for #mail_to method' do
|
116
|
+
it 'should display link element for mail to no caption' do
|
117
|
+
actual_html = mail_to('test@demo.com')
|
118
|
+
assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'test@demo.com') { actual_html }
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should display link element for mail to with caption' do
|
122
|
+
actual_html = mail_to('test@demo.com', "My Email", :class => 'demo')
|
123
|
+
assert_has_tag(:a, :href => "mailto:test@demo.com", :content => 'My Email', :class => 'demo') { actual_html }
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should display link element for mail to with caption and mail options' do
|
127
|
+
actual_html = mail_to('test@demo.com', "My Email", :subject => 'demo test', :class => 'demo', :cc => 'foo@test.com')
|
128
|
+
assert_has_tag(:a, :class => 'demo') { actual_html }
|
129
|
+
assert_match %r{mailto\:test\@demo.com\?}, actual_html
|
130
|
+
assert_match %r{cc=foo\@test\.com}, actual_html
|
131
|
+
assert_match %r{subject\=demo\%20test}, actual_html
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should escape & with encoded string and & in HTML' do
|
135
|
+
actual_html = mail_to('test@demo.com', "My&Email", :subject => "this&that")
|
136
|
+
assert_match 'this%26that', actual_html
|
137
|
+
assert_match 'My&Email', actual_html
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should not double-escape ampersands in query' do
|
141
|
+
actual_html = mail_to('to@demo.com', "Email", :subject => 'Hi there', :bcc => 'bcc@test.com')
|
142
|
+
assert_has_tag(:a, :href => 'mailto:to@demo.com?bcc=bcc@test.com&subject=Hi%20there', :content => 'Email') { actual_html }
|
143
|
+
assert_match %r{&}, actual_html
|
144
|
+
refute_match %r{&amp;}, actual_html
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should display mail link element in haml' do
|
148
|
+
visit '/haml/mail_to'
|
149
|
+
assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
|
150
|
+
assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should display mail link element in erb' do
|
154
|
+
visit '/erb/mail_to'
|
155
|
+
assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
|
156
|
+
assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should display mail link element in slim' do
|
160
|
+
visit '/slim/mail_to'
|
161
|
+
assert_have_selector 'p.simple a', :href => 'mailto:test@demo.com', :content => 'test@demo.com'
|
162
|
+
assert_have_selector 'p.captioned a', :href => 'mailto:test@demo.com', :content => 'Click my Email'
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe 'for #meta_tag method' do
|
167
|
+
it 'should display meta tag with given content and name' do
|
168
|
+
actual_html = meta_tag("weblog,news", :name => "keywords")
|
169
|
+
assert_has_tag("meta", :name => "keywords", "content" => "weblog,news") { actual_html }
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should display meta tag with given content and http-equiv' do
|
173
|
+
actual_html = meta_tag("text/html; charset=UTF-8", :"http-equiv" => "Content-Type")
|
174
|
+
assert_has_tag("meta", :"http-equiv" => "Content-Type", "content" => "text/html; charset=UTF-8") { actual_html }
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should display meta tag element in haml' do
|
178
|
+
visit '/haml/meta_tag'
|
179
|
+
assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
|
180
|
+
assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'should display meta tag element in erb' do
|
184
|
+
visit '/erb/meta_tag'
|
185
|
+
assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
|
186
|
+
assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'should display meta tag element in slim' do
|
190
|
+
visit '/slim/meta_tag'
|
191
|
+
assert_have_selector 'meta', "content" => "weblog,news", :name => "keywords"
|
192
|
+
assert_have_selector 'meta', "content" => "text/html; charset=UTF-8", :"http-equiv" => "Content-Type"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe 'for #image_tag method' do
|
197
|
+
it 'should display image tag absolute link with no options' do
|
198
|
+
time = stop_time_for_test
|
199
|
+
assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'should display image tag relative link with specified uri root' do
|
203
|
+
time = stop_time_for_test
|
204
|
+
self.class.stubs(:uri_root).returns("/blog")
|
205
|
+
assert_has_tag('img', :src => "/blog/images/relative/pic.gif?#{time.to_i}") { image_tag('relative/pic.gif') }
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'should display image tag relative link with options' do
|
209
|
+
time = stop_time_for_test
|
210
|
+
assert_has_tag('img.photo', :src => "/images/relative/pic.gif?#{time.to_i}") {
|
211
|
+
image_tag('relative/pic.gif', :class => 'photo') }
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'should display image tag uri link with options' do
|
215
|
+
time = stop_time_for_test
|
216
|
+
assert_has_tag('img.photo', :src => "http://demo.org/pic.gif") { image_tag('http://demo.org/pic.gif', :class => 'photo') }
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'should display image tag relative link with incorrect spacing' do
|
220
|
+
time = stop_time_for_test
|
221
|
+
assert_has_tag('img.photo', :src => "/images/%20relative/%20pic.gif%20%20?#{time.to_i}") {
|
222
|
+
image_tag(' relative/ pic.gif ', :class => 'photo')
|
223
|
+
}
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should not use a timestamp if stamp setting is false' do
|
227
|
+
assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'should have xhtml convention tag' do
|
231
|
+
assert_equal image_tag('/absolute/pic.gif'), '<img src="/absolute/pic.gif" />'
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe 'for #stylesheet_link_tag method' do
|
236
|
+
it 'should display stylesheet link item' do
|
237
|
+
time = stop_time_for_test
|
238
|
+
actual_html = stylesheet_link_tag('style')
|
239
|
+
expected_options = { :rel => "stylesheet", :type => "text/css" }
|
240
|
+
assert_has_tag('link', expected_options.merge(:href => "/stylesheets/style.css?#{time.to_i}")) { actual_html }
|
241
|
+
assert actual_html.html_safe?
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'should display stylesheet link item for long relative path' do
|
245
|
+
time = stop_time_for_test
|
246
|
+
expected_options = { :rel => "stylesheet", :type => "text/css" }
|
247
|
+
actual_html = stylesheet_link_tag('example/demo/style')
|
248
|
+
assert_has_tag('link', expected_options.merge(:href => "/stylesheets/example/demo/style.css?#{time.to_i}")) { actual_html }
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'should display stylesheet link item with absolute path' do
|
252
|
+
time = stop_time_for_test
|
253
|
+
expected_options = { :rel => "stylesheet", :type => "text/css" }
|
254
|
+
actual_html = stylesheet_link_tag('/css/style')
|
255
|
+
assert_has_tag('link', expected_options.merge(:href => "/css/style.css")) { actual_html }
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'should display stylesheet link item with uri root' do
|
259
|
+
self.class.stubs(:uri_root).returns("/blog")
|
260
|
+
time = stop_time_for_test
|
261
|
+
expected_options = { :rel => "stylesheet", :type => "text/css" }
|
262
|
+
actual_html = stylesheet_link_tag('style')
|
263
|
+
assert_has_tag('link', expected_options.merge(:href => "/blog/stylesheets/style.css?#{time.to_i}")) { actual_html }
|
264
|
+
end
|
265
|
+
|
266
|
+
it 'should display stylesheet link items' do
|
267
|
+
time = stop_time_for_test
|
268
|
+
actual_html = stylesheet_link_tag('style', 'layout.css', 'http://google.com/style.css')
|
269
|
+
assert_has_tag('link', :rel => "stylesheet", :type => "text/css", :count => 3) { actual_html }
|
270
|
+
assert_has_tag('link', :href => "/stylesheets/style.css?#{time.to_i}") { actual_html }
|
271
|
+
assert_has_tag('link', :href => "/stylesheets/layout.css?#{time.to_i}") { actual_html }
|
272
|
+
assert_has_tag('link', :href => "http://google.com/style.css") { actual_html }
|
273
|
+
assert_equal actual_html, stylesheet_link_tag(['style', 'layout.css', 'http://google.com/style.css'])
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'should not use a timestamp if stamp setting is false' do
|
277
|
+
self.class.expects(:asset_stamp).returns(false)
|
278
|
+
expected_options = { :rel => "stylesheet", :type => "text/css" }
|
279
|
+
assert_has_tag('link', expected_options.merge(:href => "/stylesheets/style.css")) { stylesheet_link_tag('style') }
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'should display stylesheet link used custom options' do
|
283
|
+
assert_has_tag('link', :rel => 'stylesheet', :media => 'screen') { stylesheet_link_tag('style', :media => 'screen') }
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
describe 'for #javascript_include_tag method' do
|
288
|
+
it 'should display javascript item' do
|
289
|
+
time = stop_time_for_test
|
290
|
+
actual_html = javascript_include_tag('application')
|
291
|
+
assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
292
|
+
assert actual_html.html_safe?
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'should respond to js_asset_folder setting' do
|
296
|
+
time = stop_time_for_test
|
297
|
+
self.class.stubs(:js_asset_folder).returns('js')
|
298
|
+
assert_equal 'js', asset_folder_name(:js)
|
299
|
+
actual_html = javascript_include_tag('application')
|
300
|
+
assert_has_tag('script', :src => "/js/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'should display javascript item for long relative path' do
|
304
|
+
time = stop_time_for_test
|
305
|
+
actual_html = javascript_include_tag('example/demo/application')
|
306
|
+
assert_has_tag('script', :src => "/javascripts/example/demo/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'should display javascript item for path containing js' do
|
310
|
+
time = stop_time_for_test
|
311
|
+
actual_html = javascript_include_tag 'test/jquery.json'
|
312
|
+
assert_has_tag('script', :src => "/javascripts/test/jquery.json?#{time.to_i}", :type => "text/javascript") { actual_html }
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'should display javascript item for path containing period' do
|
316
|
+
time = stop_time_for_test
|
317
|
+
actual_html = javascript_include_tag 'test/jquery.min'
|
318
|
+
assert_has_tag('script', :src => "/javascripts/test/jquery.min.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
319
|
+
end
|
320
|
+
|
321
|
+
it 'should display javascript item with absolute path' do
|
322
|
+
time = stop_time_for_test
|
323
|
+
actual_html = javascript_include_tag('/js/application')
|
324
|
+
assert_has_tag('script', :src => "/js/application.js", :type => "text/javascript") { actual_html }
|
325
|
+
end
|
326
|
+
|
327
|
+
it 'should display javascript item with uri root' do
|
328
|
+
self.class.stubs(:uri_root).returns("/blog")
|
329
|
+
time = stop_time_for_test
|
330
|
+
actual_html = javascript_include_tag('application')
|
331
|
+
assert_has_tag('script', :src => "/blog/javascripts/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'should not append extension to absolute paths' do
|
335
|
+
time = stop_time_for_test
|
336
|
+
actual_html = javascript_include_tag('https://maps.googleapis.com/maps/api/js?key=value&sensor=false')
|
337
|
+
assert_has_tag('script', :src => "https://maps.googleapis.com/maps/api/js?key=value&sensor=false") { actual_html }
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'should display javascript items' do
|
341
|
+
time = stop_time_for_test
|
342
|
+
actual_html = javascript_include_tag('application', 'base.js', 'http://google.com/lib.js')
|
343
|
+
assert_has_tag('script', :type => "text/javascript", :count => 3) { actual_html }
|
344
|
+
assert_has_tag('script', :src => "/javascripts/application.js?#{time.to_i}") { actual_html }
|
345
|
+
assert_has_tag('script', :src => "/javascripts/base.js?#{time.to_i}") { actual_html }
|
346
|
+
assert_has_tag('script', :src => "http://google.com/lib.js") { actual_html }
|
347
|
+
assert_equal actual_html, javascript_include_tag(['application', 'base.js', 'http://google.com/lib.js'])
|
348
|
+
end
|
349
|
+
|
350
|
+
it 'should not use a timestamp if stamp setting is false' do
|
351
|
+
self.class.expects(:asset_stamp).returns(false)
|
352
|
+
actual_html = javascript_include_tag('application')
|
353
|
+
assert_has_tag('script', :src => "/javascripts/application.js", :type => "text/javascript") { actual_html }
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
describe "for #favicon_tag method" do
|
358
|
+
it 'should display favicon' do
|
359
|
+
time = stop_time_for_test
|
360
|
+
actual_html = favicon_tag('icons/favicon.png')
|
361
|
+
assert_has_tag('link', :rel => 'icon', :type => 'image/png', :href => "/images/icons/favicon.png?#{time.to_i}") { actual_html }
|
362
|
+
end
|
363
|
+
|
364
|
+
it 'should match type with file ext' do
|
365
|
+
time = stop_time_for_test
|
366
|
+
actual_html = favicon_tag('favicon.ico')
|
367
|
+
assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => "/images/favicon.ico?#{time.to_i}") { actual_html }
|
368
|
+
end
|
369
|
+
|
370
|
+
it 'should allow option overrides' do
|
371
|
+
time = stop_time_for_test
|
372
|
+
actual_html = favicon_tag('favicon.png', :type => 'image/ico')
|
373
|
+
assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => "/images/favicon.png?#{time.to_i}") { actual_html }
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
describe 'for #feed_tag method' do
|
378
|
+
it 'should generate correctly link tag for rss' do
|
379
|
+
assert_has_tag('link', :type => 'application/rss+xml', :rel => 'alternate', :href => "/blog/post.rss", :title => 'rss') { feed_tag :rss, "/blog/post.rss" }
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'should generate correctly link tag for atom' do
|
383
|
+
assert_has_tag('link', :type => 'application/atom+xml', :rel => 'alternate', :href => "/blog/post.atom", :title => 'atom') { feed_tag :atom, "/blog/post.atom" }
|
384
|
+
end
|
385
|
+
|
386
|
+
it 'should override options' do
|
387
|
+
assert_has_tag('link', :type => 'my-type', :rel => 'my-rel', :href => "/blog/post.rss", :title => 'my-title') { feed_tag :rss, "/blog/post.rss", :type => "my-type", :rel => "my-rel", :title => "my-title" }
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
describe 'for #asset_path method' do
|
392
|
+
it 'should generate proper paths for js and css' do
|
393
|
+
assert_match /\/javascripts\/app.js\?\d+/, asset_path(:js, 'app')
|
394
|
+
assert_match /\/stylesheets\/app.css\?\d+/, asset_path(:css, 'app')
|
395
|
+
end
|
396
|
+
|
397
|
+
it 'should generate proper paths for images and other files' do
|
398
|
+
assert_match /\/images\/app.png\?\d+/, asset_path(:images, 'app.png')
|
399
|
+
assert_match /\/documents\/app.pdf\?\d+/, asset_path(:documents, 'app.pdf')
|
400
|
+
end
|
401
|
+
|
402
|
+
it 'should generate proper paths for public folder' do
|
403
|
+
assert_match /\/files\/file.ext\?\d+/, asset_path('files/file.ext')
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|