nice-n-easy 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.
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{nice-n-easy}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Landau"]
12
+ s.date = %q{2009-10-28}
13
+ s.description = %q{A set of Sinatra HTML view helpers to make your life nicer and easier. Helpers for forms, links, and assets.}
14
+ s.email = %q{brian.landau@viget.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sinatra/nice_easy_helpers.rb",
27
+ "nice-n-easy.gemspec",
28
+ "tasks/rdoc.rb",
29
+ "tasks/test.rb",
30
+ "tasks/vendor/sdoc-helpers/markdown.rb",
31
+ "tasks/vendor/sdoc-helpers/pages.rb",
32
+ "test/nice_easy_helpers_test.rb",
33
+ "test/tag_matcher.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/brianjlandau/nice-n-easy}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.5}
40
+ s.summary = %q{Sinatra HTML helpers that are nice-n-easy to use.}
41
+ s.test_files = [
42
+ "test/nice_easy_helpers_test.rb",
43
+ "test/tag_matcher.rb",
44
+ "test/test_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<sinatra>, ["~> 0.9"])
53
+ s.add_runtime_dependency(%q<activesupport>, ["~> 2.3"])
54
+ else
55
+ s.add_dependency(%q<sinatra>, ["~> 0.9"])
56
+ s.add_dependency(%q<activesupport>, ["~> 2.3"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<sinatra>, ["~> 0.9"])
60
+ s.add_dependency(%q<activesupport>, ["~> 2.3"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'rake/rdoctask'
3
+ require 'sdoc'
4
+ require File.join(File.dirname(__FILE__), 'vendor/sdoc-helpers/markdown')
5
+ require File.join(File.dirname(__FILE__), 'vendor/sdoc-helpers/pages')
6
+
7
+ Rake::RDocTask.new do |rdoc|
8
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
9
+
10
+ rdoc.rdoc_dir = 'docs'
11
+ rdoc.title = "Nice-n-Easy #{version} Documentation"
12
+ rdoc.rdoc_files.include('README*')
13
+ rdoc.rdoc_files.include('LICENSE')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ rdoc.main = 'README.md'
16
+ rdoc.template = 'direct'
17
+ rdoc.options << '--fmt' << 'shtml'
18
+ end
19
+ rescue LoadError
20
+ puts "sdoc RDoc tasks not loaded. Please intall sdoc."
21
+ end
@@ -0,0 +1,17 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new(:test) do |test|
3
+ test.libs << 'lib' << 'test'
4
+ test.pattern = 'test/**/*_test.rb'
5
+ test.verbose = true
6
+ end
7
+
8
+ begin
9
+ require 'rcov/rcovtask'
10
+ Rcov::RcovTask.new do |test|
11
+ test.libs << 'test'
12
+ test.pattern = 'test/**/*_test.rb'
13
+ test.verbose = true
14
+ test.rcov_opts = %w[--sort coverage -T --only-uncovered]
15
+ end
16
+ rescue LoadError
17
+ end
@@ -0,0 +1,56 @@
1
+ require 'digest/md5'
2
+
3
+ module SDocHelpers
4
+ module MarkdownFiles
5
+ def description
6
+ return super unless full_name =~ /\.(md|markdown)$/
7
+ # assuming your path is ROOT/html or ROOT/doc
8
+ path = Dir.pwd + '/../' + full_name
9
+ Markdown.new(gfm(File.read(path))).to_html + open_links_in_new_window
10
+ end
11
+
12
+ def open_links_in_new_window
13
+ <<-html
14
+ <script type="text/javascript">$(function() {
15
+ $('a').each(function() { $(this).attr('target', '_blank') })
16
+ })</script>
17
+ html
18
+ end
19
+
20
+ private
21
+
22
+ def gfm(text)
23
+ # Extract pre blocks
24
+ extractions = {}
25
+ text.gsub!(%r{<pre>.*?</pre>}m) do |match|
26
+ md5 = Digest::MD5.hexdigest(match)
27
+ extractions[md5] = match
28
+ "{gfm-extraction-#{md5}}"
29
+ end
30
+
31
+ # prevent foo_bar_baz from ending up with an italic word in the middle
32
+ text.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
33
+ x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
34
+ end
35
+
36
+ # in very clear cases, let newlines become <br /> tags
37
+ text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
38
+ x.gsub(/^(.+)$/, "\\1 ")
39
+ end
40
+
41
+ # Insert pre block extractions
42
+ text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
43
+ extractions[$1]
44
+ end
45
+
46
+ text
47
+ end
48
+ end
49
+ end
50
+
51
+ begin
52
+ require 'rdiscount'
53
+ RDoc::TopLevel.send :include, SDocHelpers::MarkdownFiles
54
+ rescue LoadError
55
+ puts "Markdown support not enabled. Please install RDiscount."
56
+ end
@@ -0,0 +1,28 @@
1
+ namespace :pages do
2
+ task :publish => [ :check_dirty, :rerdoc ] do
3
+ `git checkout gh-pages`
4
+ `ls -1 | grep -v docs | xargs rm -rf; mv docs/* .; rm -rf docs`
5
+ `git add .; git commit -m "update docs"; git push origin gh-pages`
6
+ `git checkout master`
7
+ puts :done
8
+ end
9
+
10
+ desc "Initialize GitHub Pages with documentation"
11
+ task :init => [ :check_dirty, :rerdoc ] do
12
+ `git symbolic-ref HEAD refs/heads/gh-pages`
13
+ `rm .git/index`
14
+ `ls -1 | grep -v docs | xargs rm -rf; mv docs/* .; rm -rf docs`
15
+ `git add .;git commit -m "create docs"; git push origin gh-pages`
16
+ `git checkout master`
17
+ puts :done
18
+ end
19
+
20
+ task :check_dirty do
21
+ if !`git status`.include?('nothing to commit')
22
+ abort "dirty index - not publishing!"
23
+ end
24
+ end
25
+ end
26
+
27
+ desc "Build and publish documentation using GitHub Pages."
28
+ task :pages => "pages:publish"
@@ -0,0 +1,621 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ User = Struct.new(:email, :password, :picture, :bio, :gender, :subscribe, :external_id, :membership_type)
4
+
5
+ class NiceEasyHelpersTest < Test::Unit::TestCase
6
+ include Sinatra::NiceEasyHelpers
7
+
8
+ context 'link helper' do
9
+ should 'create a link tag' do
10
+ html = link("Go look at this", 'http://www.somewhere.com/')
11
+ assert_select_in html, 'a[href="http://www.somewhere.com/"]', "Go look at this"
12
+ end
13
+
14
+ should 'create a link tag with custom HTML attributes' do
15
+ html = link("Go look at this", 'http://www.somewhere.com/', :class => 'button')
16
+ assert_select_in html, 'a.button[href="http://www.somewhere.com/"]', "Go look at this"
17
+ end
18
+ end
19
+
20
+ context 'label helper' do
21
+ should 'create a label tag without a field' do
22
+ html = label(:email)
23
+ assert_select_in html, 'label[for="email"]', 'Email'
24
+ end
25
+
26
+ should 'create a label tag with a field' do
27
+ html = label(:user, :email)
28
+ assert_select_in html, 'label[for="user_email"]', 'Email'
29
+ end
30
+
31
+ should 'create a label tag without a field with custom label' do
32
+ html = label(:email, :text => 'Email Address')
33
+ assert_select_in html, 'label[for="email"]', 'Email Address'
34
+ end
35
+
36
+ should 'create a label tag with a field with custom label' do
37
+ html = label(:user, :email, :text => 'Email Address')
38
+ assert_select_in html, 'label[for="user_email"]', 'Email Address'
39
+ end
40
+ end
41
+
42
+ context 'text field helper' do
43
+ setup do
44
+ @params = {}
45
+ end
46
+
47
+ should 'create a input text tag without a field' do
48
+ html = text_field(:email)
49
+ assert_select_in html, 'input#email[type="text"][name="email"]'
50
+ end
51
+
52
+ should 'create a input text tag with a field' do
53
+ html = text_field(:user, :email)
54
+ assert_select_in html, 'input#user_email[type="text"][name=?]', "user[email]"
55
+ end
56
+
57
+ should 'create a text_field tag without a field with custom class' do
58
+ html = text_field(:email, :class => 'required')
59
+ assert_select_in html, 'input#email.required[type="text"][name="email"]'
60
+ end
61
+
62
+ should 'create a text_field tag with a field with custom class' do
63
+ html = text_field(:user, :email, :class => 'required')
64
+ assert_select_in html, 'input#user_email.required[type="text"][name=?]', 'user[email]'
65
+ end
66
+
67
+ should 'create a text_field tag with value set by params when no field provided' do
68
+ @params = {:email => 'joe@example.com'}.with_indifferent_access
69
+ html = text_field(:email)
70
+ assert_select_in html, 'input#email[type="text"][name="email"][value="joe@example.com"]'
71
+ end
72
+
73
+ should 'create a text_field tag with value set by params' do
74
+ @params = {:user => {:email => 'joe@example.com'}.with_indifferent_access}.with_indifferent_access
75
+ html = text_field(:user, :email)
76
+ assert_select_in html, 'input#user_email[type="text"][name=?][value="joe@example.com"]', 'user[email]'
77
+ end
78
+
79
+ should 'create a text_field tag with value set by object when provided' do
80
+ user = User.new
81
+ user.email = "joe@example.com"
82
+
83
+ html = text_field(user, :email)
84
+ assert_select_in html, 'input#user_email[type="text"][name=?][value="joe@example.com"]', 'user[email]'
85
+ end
86
+
87
+ teardown do
88
+ @param = nil
89
+ end
90
+ end
91
+
92
+ context 'text area helper' do
93
+ setup do
94
+ @params = {}
95
+ end
96
+
97
+ should 'create a text area tag without a field' do
98
+ html = text_area(:bio)
99
+ assert_select_in html, 'textarea#bio[name="bio"]'
100
+ end
101
+
102
+ should 'create a text area tag with a field' do
103
+ html = text_area(:user, :bio)
104
+ assert_select_in html, 'textarea#user_bio[name=?]', 'user[bio]'
105
+ end
106
+
107
+ should 'create a text area tag without a field with custom class' do
108
+ html = text_area(:bio, :class => 'required')
109
+ assert_select_in html, 'textarea#bio.required[name="bio"]'
110
+ end
111
+
112
+ should 'create a text area tag with a field with custom class' do
113
+ html = text_area(:user, :bio, :class => 'required')
114
+ assert_select_in html, 'textarea#user_bio.required[name=?]', 'user[bio]'
115
+ end
116
+
117
+ should 'create a text area tag with value set by params when no field provided' do
118
+ @params = {:bio => %q(I'm from Canada)}.with_indifferent_access
119
+ html = text_area(:bio)
120
+ assert_select_in html, 'textarea#bio[name="bio"]', %q(I'm from Canada)
121
+ end
122
+
123
+ should 'create a text area tag with value set by params' do
124
+ @params = {:user => {:bio => %q(I'm from Canada)}.with_indifferent_access}.with_indifferent_access
125
+ html = text_area(:user, :bio)
126
+ assert_select_in html, 'textarea#user_bio', %q(I'm from Canada)
127
+ end
128
+
129
+ should 'create a text area tag with value set by object when provided' do
130
+ user = User.new
131
+ user.bio = %q(I'm from Canada)
132
+
133
+ html = text_area(user, :bio)
134
+ assert_select_in html, 'textarea#user_bio', %q(I'm from Canada)
135
+ end
136
+
137
+ teardown do
138
+ @params = nil
139
+ end
140
+ end
141
+
142
+ context 'password field helper' do
143
+ setup do
144
+ @params = {}
145
+ end
146
+
147
+ should 'create a input password tag without a field' do
148
+ html = password_field(:password)
149
+ assert_select_in html, 'input#password[type="password"][name="password"]'
150
+ end
151
+
152
+ should 'create a input password tag with a field' do
153
+ html = password_field(:user, :password)
154
+ assert_select_in html, 'input#user_password[type="password"][name=?]', 'user[password]'
155
+ end
156
+
157
+ should 'create a password field tag without a field with custom class' do
158
+ html = password_field(:password, :class => 'required')
159
+ assert_select_in html, 'input#password.required[type="password"][name="password"]'
160
+ end
161
+
162
+ should 'create a password field tag with a field with custom class' do
163
+ html = password_field(:user, :password, :class => 'required')
164
+ assert_select_in html, 'input#user_password.required[type="password"][name=?]', 'user[password]'
165
+ end
166
+
167
+ should 'create a password field tag with value set by params when no field provided' do
168
+ @params = {:password => 'password'}.with_indifferent_access
169
+ html = password_field(:password)
170
+ assert_select_in html, 'input#password[type="password"][name="password"][value="password"]'
171
+ end
172
+
173
+ should 'create a password field tag with value set by params' do
174
+ @params = {:user => {:password => 'password'}.with_indifferent_access}.with_indifferent_access
175
+ html = password_field(:user, :password)
176
+ assert_select_in html, 'input#user_password[type="password"][name=?][value="password"]', 'user[password]'
177
+ end
178
+
179
+ should 'create a password_field tag with value set by object when provided' do
180
+ user = User.new
181
+ user.password = "password"
182
+
183
+ html = password_field(user, :password)
184
+ assert_select_in html, 'input#user_password[type="password"][name=?][value="password"]', 'user[password]'
185
+ end
186
+
187
+ teardown do
188
+ @param = nil
189
+ end
190
+ end
191
+
192
+ context 'file field helper' do
193
+ setup do
194
+ @params = {}
195
+ end
196
+
197
+ should 'create a input text tag without a field' do
198
+ html = file_field(:picture)
199
+ assert_select_in html, 'input#picture[type="file"][name="picture"]'
200
+ end
201
+
202
+ should 'create a input text tag with a field' do
203
+ html = file_field(:user, :picture)
204
+ assert_select_in html, 'input#user_picture[type="file"][name=?]', 'user[picture]'
205
+ end
206
+
207
+ should 'create a file_field tag without a field with custom class' do
208
+ html = file_field(:picture, :class => 'required')
209
+ assert_select_in html, 'input#picture.required[type="file"][name="picture"]'
210
+ end
211
+
212
+ should 'create a file_field tag with a field with custom class' do
213
+ html = file_field(:user, :picture, :class => 'required')
214
+ assert_select_in html, 'input#user_picture.required[type="file"][name=?]', 'user[picture]'
215
+ end
216
+
217
+ teardown do
218
+ @param = nil
219
+ end
220
+ end
221
+
222
+ context 'button helper' do
223
+ should 'create a button element of submit type' do
224
+ html = button('save', 'Save and continue')
225
+ assert_select_in html, 'button#save[type="submit"][name="save"]', 'Save and continue'
226
+ end
227
+
228
+ should 'create a button element of submit type with a custom class' do
229
+ html = button('save', 'Save and continue', :class => 'light')
230
+ assert_select_in html, 'button#save.light[type="submit"][name="save"]', 'Save and continue'
231
+ end
232
+
233
+
234
+ should 'create a button element of submit type with a value' do
235
+ html = button('save', 'Save and continue', :value => 'continue')
236
+ assert_select_in html, 'button#save[type="submit"][name="save"][value="continue"]', 'Save and continue'
237
+ end
238
+
239
+ should 'create a button element of button type' do
240
+ html = button('action', 'Do something', 'button')
241
+ assert_select_in html, 'button#action[type="button"][name="action"]', 'Do something'
242
+ end
243
+
244
+ should 'create a button element of button type with custom class' do
245
+ html = button('action', 'Do something', 'button', :class => 'light')
246
+ assert_select_in html, 'button#action.light[type="button"][name="action"]', 'Do something'
247
+ end
248
+ end
249
+
250
+ context 'image submit button helper' do
251
+ should 'create an image input tag' do
252
+ html = image_input('save.png')
253
+ assert_select_in html, 'input[type="image"][src="/images/save.png"]'
254
+ end
255
+
256
+ should 'create an image input tag with custom class' do
257
+ html = image_input('save.png', :class => 'large')
258
+ assert_select_in html, 'input.large[type="image"][src="/images/save.png"]'
259
+ end
260
+ end
261
+
262
+ context 'submit helper' do
263
+ should 'create a submit button' do
264
+ html = submit
265
+ assert_select_in html, 'input[type="submit"][value="Save"]'
266
+ end
267
+
268
+ should 'create a sbumit button with custom text' do
269
+ html = submit('Continue')
270
+ assert_select_in html, 'input[type="submit"][value="Continue"]'
271
+ end
272
+
273
+ should 'create a sbumit button with custom text and custom class' do
274
+ html = submit('Continue', :class => 'large')
275
+ assert_select_in html, 'input.large[type="submit"][value="Continue"]'
276
+ end
277
+ end
278
+
279
+ context 'hidden field helper' do
280
+ setup do
281
+ @params = {}
282
+ end
283
+
284
+ should 'create a input hidden tag without a field' do
285
+ html = hidden_field(:external_id, :value => 25)
286
+ assert_select_in html, 'input#external_id[type="hidden"][name="external_id"][value="25"]'
287
+ end
288
+
289
+ should 'create a input hidden tag with a field' do
290
+ html = hidden_field(:user, :external_id, :value => 25)
291
+ assert_select_in html, 'input#user_external_id[type="hidden"][name=?][value="25"]', 'user[external_id]'
292
+ end
293
+
294
+ should 'create a hidden field tag without a field with custom class' do
295
+ html = hidden_field(:external_id, :class => 'required', :value => 25)
296
+ assert_select_in html, 'input#external_id.required[type="hidden"][name="external_id"][value="25"]'
297
+ end
298
+
299
+ should 'create a hidden field tag with a field with custom class' do
300
+ html = hidden_field(:user, :external_id, :class => 'required', :value => 25)
301
+ assert_select_in html, 'input#user_external_id.required[type="hidden"][name=?][value="25"]', 'user[external_id]'
302
+ end
303
+
304
+ should 'create a hidden field tag with value set by params when no field provided' do
305
+ @params = {:external_id => '3987'}.with_indifferent_access
306
+ html = hidden_field(:external_id)
307
+ assert_select_in html, 'input#external_id[type="hidden"][name="external_id"][value="3987"]'
308
+ end
309
+
310
+ should 'create a hidden field tag with value set by params' do
311
+ @params = {:user => {:external_id => '3987'}.with_indifferent_access}.with_indifferent_access
312
+ html = hidden_field(:user, :external_id)
313
+ assert_select_in html, 'input#user_external_id[type="hidden"][name=?][value="3987"]', 'user[external_id]'
314
+ end
315
+
316
+ should 'create a hidden field tag with value set by object when provided' do
317
+ user = User.new
318
+ user.external_id = "3987"
319
+
320
+ html = hidden_field(user, :external_id)
321
+ assert_select_in html, 'input#user_external_id[type="hidden"][name=?][value="3987"]', 'user[external_id]'
322
+ end
323
+
324
+ teardown do
325
+ @param = nil
326
+ end
327
+ end
328
+
329
+ context 'checkbox field helper' do
330
+ setup do
331
+ @params = {}
332
+ end
333
+
334
+ should 'create a input checkbox tag without a field' do
335
+ html = checkbox_field(:subscribe, :value => 1)
336
+ assert_select_in html, 'input#subscribe[type="checkbox"][name="subscribe"][value="1"]'
337
+ end
338
+
339
+ should 'require a value option' do
340
+ assert_raise ArgumentError do
341
+ checkbox_field(:subscribe)
342
+ end
343
+ end
344
+
345
+ should 'create a input checkbox tag with a field' do
346
+ html = checkbox_field(:user, :subscribe, :value => 1)
347
+ assert_select_in html, 'input#user_subscribe[type="checkbox"][name=?][value="1"]', 'user[subscribe]'
348
+ end
349
+
350
+ should 'create a checkbox field tag without a field with custom class' do
351
+ html = checkbox_field(:subscribe, :value => 1, :class => 'required')
352
+ assert_select_in html, 'input#subscribe.required[type="checkbox"][name="subscribe"][value="1"]'
353
+ end
354
+
355
+ should 'create a checkbox field tag with a field with custom class' do
356
+ html = checkbox_field(:user, :subscribe, :value => 1, :class => 'required')
357
+ assert_select_in html, 'input#user_subscribe.required[type="checkbox"][name=?][value="1"]', 'user[subscribe]'
358
+ end
359
+
360
+ should 'create a checkbox field tag with checked set by params when no field provided' do
361
+ @params = {:subscribe => 1}.with_indifferent_access
362
+ html = checkbox_field(:subscribe, :value => 1)
363
+ assert_select_in html, 'input#subscribe[type="checkbox"][name="subscribe"][value="1"][checked="checked"]'
364
+ end
365
+
366
+ should 'create a checkbox field tag with checked not set when value does not match' do
367
+ @params = {:subscribe => 2}.with_indifferent_access
368
+ html = checkbox_field(:subscribe, :value => 1)
369
+ assert_select_in html, 'input#subscribe[type="checkbox"][name="subscribe"][value="1"]:not([checked])'
370
+ end
371
+
372
+ should 'create a checkbox field tag with checked set by params' do
373
+ @params = {:user => {:subscribe => 1}.with_indifferent_access}.with_indifferent_access
374
+ html = checkbox_field(:user, :subscribe, :value => 1)
375
+ assert_select_in html, 'input#user_subscribe[type="checkbox"][name=?][value="1"][checked="checked"]', 'user[subscribe]'
376
+ end
377
+
378
+ should 'create a checkbox field tag with checked set by object when provided' do
379
+ user = User.new
380
+ user.subscribe = 1
381
+
382
+ html = checkbox_field(user, :subscribe, :value => 1)
383
+ assert_select_in html, 'input#user_subscribe[type="checkbox"][name=?][value="1"][checked="checked"]', 'user[subscribe]'
384
+ end
385
+
386
+ context 'multipe values possilbe' do
387
+ setup do
388
+ @params = {:user => {:subscribe => ['newsletter']}.with_indifferent_access}.with_indifferent_access
389
+ end
390
+
391
+ should 'craete a checkbox field tag with checked set if value is in param values' do
392
+ html = checkbox_field(:user, :subscribe, :value => 'newsletter')
393
+ assert_select_in html, 'input#user_subscribe[type="checkbox"][name=?][value="newsletter"][checked="checked"]', 'user[subscribe]'
394
+ end
395
+
396
+ should 'craete a checkbox field tag with checked not set if value is not in param values' do
397
+ html = checkbox_field(:user, :subscribe, :value => 'updates')
398
+ assert_select_in html, 'input#user_subscribe[type="checkbox"][name=?][value="updates"]:not([checked])', 'user[subscribe]'
399
+ end
400
+ end
401
+
402
+ teardown do
403
+ @param = nil
404
+ end
405
+ end
406
+
407
+
408
+ context 'radio button helper' do
409
+ setup do
410
+ @params = {}
411
+ end
412
+
413
+ should 'create a input radio tag without a field' do
414
+ html = radio_button(:gender, :value => 'male')
415
+ assert_select_in html, 'input#gender[type="radio"][name="gender"][value="male"]'
416
+ end
417
+
418
+ should 'require a value option' do
419
+ assert_raise ArgumentError do
420
+ radio_button(:gender)
421
+ end
422
+ end
423
+
424
+ should 'create a input radio tag with a field' do
425
+ html = radio_button(:user, :gender, :value => 'male')
426
+ assert_select_in html, 'input#user_gender[type="radio"][name=?][value="male"]', 'user[gender]'
427
+ end
428
+
429
+ should 'create a radio field tag without a field with custom class' do
430
+ html = radio_button(:gender, :value => "male", :class => 'required')
431
+ assert_select_in html, 'input#gender.required[type="radio"][name="gender"][value="male"]'
432
+ end
433
+
434
+ should 'create a radio field tag with a field with custom class' do
435
+ html = radio_button(:user, :gender, :value => "male", :class => 'required')
436
+ assert_select_in html, 'input#user_gender.required[type="radio"][name=?][value="male"]', 'user[gender]'
437
+ end
438
+
439
+ should 'create a radio field tag with checked set by params when no field provided' do
440
+ @params = {:gender => "male"}.with_indifferent_access
441
+ html = radio_button(:gender, :value => "male")
442
+ assert_select_in html, 'input#gender[type="radio"][name="gender"][value="male"][checked="checked"]'
443
+ end
444
+
445
+ should 'create a radio field tag with checked not set when value does not match' do
446
+ @params = {:gender => 'female'}.with_indifferent_access
447
+ html = radio_button(:gender, :value => "male")
448
+ assert_select_in html, 'input#gender[type="radio"][name="gender"][value="male"]:not([checked])'
449
+ end
450
+
451
+ should 'create a radio field tag with checked set by params' do
452
+ @params = {:user => {:gender => "male"}.with_indifferent_access}.with_indifferent_access
453
+ html = radio_button(:user, :gender, :value => "male")
454
+ assert_select_in html, 'input#user_gender[type="radio"][name=?][value="male"][checked="checked"]', 'user[gender]'
455
+ end
456
+
457
+ should 'create a radio field tag with checked set by object when provided' do
458
+ user = User.new
459
+ user.gender = "male"
460
+
461
+ html = radio_button(user, :gender, :value => "male")
462
+ assert_select_in html, 'input#user_gender[type="radio"][name=?][value="male"][checked="checked"]', 'user[gender]'
463
+ end
464
+
465
+ teardown do
466
+ @param = nil
467
+ end
468
+ end
469
+
470
+ context 'select field helper' do
471
+ setup do
472
+ @params = {}
473
+ end
474
+
475
+ should 'require an items argument' do
476
+ assert_raise ArgumentError do
477
+ select_field(:membership_type)
478
+ end
479
+ end
480
+
481
+ should 'create a select tag with options' do
482
+ html = select_field(:membership_type, ['Lifetime', '1 Month', '1 Year'])
483
+
484
+ assert_select_in html, 'select#membership_type[name="membership_type"]'
485
+ assert_select_in html, 'select#membership_type > option', 'Lifetime'
486
+ assert_select_in html, 'select#membership_type > option', '1 Month'
487
+ assert_select_in html, 'select#membership_type > option', '1 Year'
488
+ end
489
+
490
+ should 'create a select tag with options from an array of arrays' do
491
+ html = select_field(:membership_type, [['Lifetime', 1], ['1 Month', 2], ['1 Year', 3]])
492
+
493
+ assert_select_in html, 'select#membership_type[name="membership_type"]'
494
+ assert_select_in html, 'select#membership_type > option[value="1"]', 'Lifetime'
495
+ assert_select_in html, 'select#membership_type > option[value="2"]', '1 Month'
496
+ assert_select_in html, 'select#membership_type > option[value="3"]', '1 Year'
497
+ end
498
+
499
+ should 'create a select tag with options from a hash' do
500
+ html = select_field(:membership_type, {'Lifetime' => 1, '1 Month' => 2, '1 Year' => 3})
501
+
502
+ assert_select_in html, 'select#membership_type[name="membership_type"]'
503
+ assert_select_in html, 'select#membership_type > option[value="1"]', 'Lifetime'
504
+ assert_select_in html, 'select#membership_type > option[value="2"]', '1 Month'
505
+ assert_select_in html, 'select#membership_type > option[value="3"]', '1 Year'
506
+ end
507
+
508
+ should 'create a select tag with object and field' do
509
+ html = select_field(:user, :membership_type, ['Lifetime', '1 Month', '1 Year'])
510
+ assert_select_in html, 'select#user_membership_type[name=?]', 'user[membership_type]'
511
+ end
512
+
513
+ should 'create a select tag with custom class' do
514
+ html = select_field(:membership_type, ['Lifetime', '1 Month', '1 Year'], :class => 'required')
515
+ assert_select_in html, 'select#membership_type.required[name="membership_type"]'
516
+ end
517
+
518
+ should 'create a select tag with object and field with custom class' do
519
+ html = select_field(:user, :membership_type, ['Lifetime', '1 Month', '1 Year'], :class => 'required')
520
+ assert_select_in html, 'select#user_membership_type.required[name=?]', 'user[membership_type]'
521
+ end
522
+
523
+ should 'create a select tag with value from params' do
524
+ @params = {:membership_type => '1 Year'}.with_indifferent_access
525
+ html = select_field(:membership_type, ['Lifetime', '1 Month', '1 Year'])
526
+ assert_select_in html, 'select#membership_type[name="membership_type"] > option[selected="selected"]', '1 Year'
527
+ end
528
+
529
+ should 'create a select tag with object and field with value from params' do
530
+ @params = {:user => {:membership_type => '1 Year'}.with_indifferent_access}.with_indifferent_access
531
+ html = select_field(:user, :membership_type, ['Lifetime', '1 Month', '1 Year'])
532
+ assert_select_in html, 'select#user_membership_type > option[selected="selected"]', '1 Year'
533
+ end
534
+
535
+ should 'create a select tag with object and field with value from object' do
536
+ user = User.new
537
+ user.membership_type = '1 Year'
538
+
539
+ html = select_field(user, :membership_type, ['Lifetime', '1 Month', '1 Year'])
540
+ assert_select_in html, 'select#user_membership_type > option[selected="selected"]', '1 Year'
541
+ end
542
+
543
+ teardown do
544
+ @param = nil
545
+ end
546
+ end
547
+
548
+ context 'image tag helper' do
549
+ should 'create an image tag for an image in the standard location' do
550
+ html = image_tag('button.jpg')
551
+ assert_select_in html, 'img[src="/images/button.jpg"]'
552
+ end
553
+
554
+ should 'create an image tag for an image in the standard location with options' do
555
+ html = image_tag('button.jpg', :alt => 'Action', :class => 'rounded')
556
+ assert_select_in html, 'img.rounded[src="/images/button.jpg"][alt="Action"]'
557
+ end
558
+
559
+ should 'create an image tag for an image in a custom location' do
560
+ html = image_tag('/icons/button.jpg')
561
+ assert_select_in html, 'img[src="/icons/button.jpg"]'
562
+ end
563
+
564
+ should 'create an image tag for an image on another server' do
565
+ html = image_tag('http://www.example.com/buttons/close.jpg')
566
+ assert_select_in html, 'img[src="http://www.example.com/buttons/close.jpg"]'
567
+ end
568
+ end
569
+
570
+ context 'javascript include tag helper' do
571
+ should 'create a script tag for a javascript file in the standard location' do
572
+ html = javascript_include_tag('jquery')
573
+ assert_select_in html, 'script[src="/javascripts/jquery.js"]'
574
+ end
575
+
576
+ should 'create multiple script tags for javascript files in multiple locations' do
577
+ html = javascript_include_tag('jquery', 'facebox')
578
+ assert_select_in html, 'script[src="/javascripts/jquery.js"]'
579
+ assert_select_in html, 'script[src="/javascripts/facebox.js"]'
580
+ end
581
+
582
+ should 'create a script tag for a javascript file in a custom location' do
583
+ html = javascript_include_tag('/scripts/custom.js')
584
+ assert_select_in html, 'script[src="/scripts/custom.js"]'
585
+ end
586
+
587
+ should 'create a script tag for a javascript file on another server' do
588
+ html = javascript_include_tag('http://www.example.com/js/prototype.js')
589
+ assert_select_in html, 'script[src="http://www.example.com/js/prototype.js"]'
590
+ end
591
+ end
592
+
593
+ context 'stylesheet link tag helper' do
594
+ should 'create a link tag for a stylesheet file in the standard location' do
595
+ html = stylesheet_link_tag('global')
596
+ assert_select_in html, 'link[href="/stylesheets/global.css"][rel="stylesheet"][type="text/css"][media="screen"]'
597
+ end
598
+
599
+ should 'create a link tag for a stylesheet file in the standard location with media attribute' do
600
+ html = stylesheet_link_tag('print', :media => 'print')
601
+ assert_select_in html, 'link[href="/stylesheets/print.css"][rel="stylesheet"][type="text/css"][media="print"]'
602
+ end
603
+
604
+ should 'create multiple link tags for stylesheet files in multiple locations' do
605
+ html = stylesheet_link_tag('global', 'print')
606
+ assert_select_in html, 'link[href="/stylesheets/global.css"][rel="stylesheet"][type="text/css"]'
607
+ assert_select_in html, 'link[href="/stylesheets/print.css"][rel="stylesheet"][type="text/css"]'
608
+ end
609
+
610
+ should 'create a link tag for a stylesheet file in a custom location' do
611
+ html = stylesheet_link_tag('/styles/custom.css')
612
+ assert_select_in html, 'link[href="/styles/custom.css"][rel="stylesheet"][type="text/css"][media="screen"]'
613
+ end
614
+
615
+ should 'create a link tag for a stylesheet file on another server' do
616
+ html = stylesheet_link_tag('http://www.example.com/css/sample.css')
617
+ assert_select_in html, 'link[href="http://www.example.com/css/sample.css"][rel="stylesheet"][type="text/css"][media="screen"]'
618
+ end
619
+ end
620
+
621
+ end