sinatra-formhelpers-huii 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a61ee9d516ea9200d8e977cf6c13e7dea468db16
4
+ data.tar.gz: f770b7739bdded37177de9783bd3f4281403c9e2
5
+ SHA512:
6
+ metadata.gz: 59bbde3ec8d1d62815c25081148a98294efc52deb8aa9b7684d8ea3d7213dd28d9c53ce80ea6f7e9138cc6bffa2cbbcf695ffde1fdfe1378189a5e5748e7049e
7
+ data.tar.gz: 9eda4ae2240719dabad399327cb92a5eff5ce7a84ba5bab8eaa1f9ffe817d0e287905ef51ac6b5d98912f79f5c55ad10519ed15e08e76348e0414f42a6fa7c2b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.0
5
+ - 2.0.0
6
+
7
+ branches:
8
+ only:
9
+ - master
10
+
11
+ install: "bundle --jobs 4"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bitly_quickly.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sinatra-formhelpers-huii (1.9.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ docile (1.1.2)
11
+ multi_json (1.8.4)
12
+ rack (1.5.2)
13
+ rack-protection (1.5.2)
14
+ rack
15
+ rack-test (0.6.2)
16
+ rack (>= 1.0)
17
+ rake (10.1.1)
18
+ rspec (2.14.1)
19
+ rspec-core (~> 2.14.0)
20
+ rspec-expectations (~> 2.14.0)
21
+ rspec-mocks (~> 2.14.0)
22
+ rspec-core (2.14.7)
23
+ rspec-expectations (2.14.4)
24
+ diff-lcs (>= 1.1.3, < 2.0)
25
+ rspec-mocks (2.14.4)
26
+ simplecov (0.8.2)
27
+ docile (~> 1.1.0)
28
+ multi_json
29
+ simplecov-html (~> 0.8.0)
30
+ simplecov-html (0.8.0)
31
+ sinatra (1.4.4)
32
+ rack (~> 1.4)
33
+ rack-protection (~> 1.4)
34
+ tilt (~> 1.3, >= 1.3.4)
35
+ tilt (1.4.1)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.5)
42
+ rack-test (~> 0.6)
43
+ rake (~> 10.1)
44
+ rspec (~> 2.14)
45
+ simplecov (~> 0.8)
46
+ sinatra (~> 1.4)
47
+ sinatra-formhelpers-huii!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ sinatra-formhelpers - Suite of form helpers for sinatra
2
+ Copyright 2009 Tom Wilson
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Sinatra::FormHelpers - Lightweight form helpers for Sinatra
2
+
3
+ This plugin adds lightweight (3-5 lines each) form helpers to Sinatra that aid with
4
+ common form and HTML tags.
5
+
6
+ link "google", "http://www.google.com" # <a href="http://www.google.com">google</a>
7
+ label :person, :first_name # <label for="person_first_name">First Name</label>
8
+ input :person, :first_name # <input name="person[first_name]" id="person_first_name" type="text" />
9
+
10
+ There are also helpers for: form, textarea, submit, image, radio, checkbox, and select
11
+
12
+
13
+ ## Why Bother?
14
+
15
+ After all, you can just write Haml or write your own helpers or hand-code raw HTML or whatever. Well, here's some considerations:
16
+
17
+ 1. Helpers maintain correct state across form submissions (eg, on errors, form stays filled in)
18
+ 2. Generate automatic labels, valid CSS ID's, and `nested[names]` to make ORMs happy
19
+ 3. No Rails ultra-magic(tm) here. Just fast, simple code.
20
+
21
+
22
+ ## Usage
23
+
24
+ With Bundler/Isolate:
25
+
26
+ gem 'sinatra-formhelpers-huii'
27
+
28
+ Then, include it in a Sinatra application:
29
+
30
+ require 'sinatra/form_helpers'
31
+
32
+ If you're subclassing `Sinatra::Base`, you also need to call `helpers` manually:
33
+
34
+ class MyApp < Sinatra::Base
35
+ helpers Sinatra::FormHelpers
36
+ # ...
37
+ end
38
+
39
+
40
+ ## Views
41
+
42
+ In your views, use these helpers to dynamically create form HTML elements. Here's an example in ERB:
43
+
44
+ <p>
45
+ Fill out the below form to sign up.
46
+ For more information, visit our <%= link 'FAQ', '/faq' %>
47
+ </p>
48
+
49
+ <%= form('/users', :post) %>
50
+
51
+ <%= input(:user, :first_name) %>
52
+ <%= input(:user, :last_name) %>
53
+
54
+ <%= input(:user, :email, :size => 40) %>
55
+
56
+ <%= password(:user, :password) %>
57
+ <%= password(:user, :confirm_password) %>
58
+
59
+ <%= radio(:user, :gender, ['M', 'F']) %>
60
+
61
+ <%= submit %>
62
+
63
+ Unlike the super-magic Rails `form_for` method, the `form()` helper just takes a URL and method. (Note that `form()` will accept `:create`, `:update`, and `:delete` and include the special `_method` hidden param for you.)
64
+
65
+ To reduce repetition, use `fieldset()` to prefix fields with a namespace:
66
+
67
+ <%= form('/users', :create) %>
68
+
69
+ <% fieldset(:user) do |f| %>
70
+ <%= f.input(:first_name) %>
71
+ <%= f.input(:last_name) %>
72
+
73
+ <%= f.input(:email, :size => 40) %>
74
+
75
+ <%= f.password(:password) %>
76
+ <%= f.password(:confirm_password) %>
77
+
78
+ <%= f.radio(:gender, ['M', 'F']) %>
79
+ <% end %>
80
+
81
+ </form> <-- You need to close it manually if you are not passing a block
82
+
83
+ <%= submit 'Create account' %>
84
+ <%= submit 'Cancel', :onclick => 'window.location=http://mydomain.com;return false' %>
85
+
86
+ or
87
+
88
+ <%= form(:user, :post, action: '/users') do |f| %> <-- This will create a fieldset automatically
89
+ <%= f.input(:first_name) %>
90
+ <%= f.input(:last_name) %>
91
+
92
+ <%= f.input(:email, :size => 40) %>
93
+
94
+ <%= f.password(:password) %>
95
+ <%= f.password(:confirm_password) %>
96
+
97
+ <%= f.radio(:gender, ['M', 'F']) %>
98
+ <% end %>
99
+
100
+ <%= submit 'Create account' %>
101
+ <%= submit 'Cancel', :onclick => 'window.location=http://mydomain.com;return false' %>
102
+
103
+ This will create fields named `user[first_name]`, `user[last_name]`, and so forth.
104
+
105
+ ## Known Bugs
106
+
107
+ * `fieldset` must be optional in `form`
108
+
109
+
110
+ ## Fixed Bugs
111
+
112
+ * Currently `fieldset` does not return a <fieldset> tag properly.
113
+ * The state of select tags was not persisted across form submissions.
114
+
115
+
116
+ ## Running tests
117
+
118
+ Just run `bundle exec rake` or `bundle exec rspec` or `rspec`.
119
+
120
+
121
+ ## Authors
122
+
123
+ * [Initial efforts](https://github.com/twilson63/sinatra-formhelpers) (c) 2009 [Tom Wilson](https://github.com/twilson63).
124
+ * [Additional efforts](https://github.com/nateware/sinatra-formhelpers) (c) 2011 [Nate Wiger](http://nateware.com).
125
+ * [Further efforts](https://github.com/cymen/sinatra-formhelpers-ng) (c) 2013 [Cymen Vig](http://blog.cymen.org/).
126
+ * [Other efforts](https://github.com/ollie/sinatra-formhelpers-huii) (c) 2014 [Oldrich Vetesnik](https://github.com/ollie).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new 'spec'
5
+
6
+ # If you want to make this the default task
7
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module FormHelpers
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,229 @@
1
+ module Sinatra
2
+ module FormHelpers
3
+ # FormHelpers are a suite of helper methods
4
+ # built to make building forms in Sinatra
5
+ # a breeze.
6
+ #
7
+ # link "jackhq", "http://www.jackhq.com"
8
+ #
9
+ # label :person, :first_name
10
+ # input :person, :first_name
11
+ # textarea :person, :notes
12
+ #
13
+ # etc.
14
+ def form(action, method = :get, options = {}, &block)
15
+ method_input = ''
16
+ # the docs suggest using ':create', ':update', or ':delete'
17
+ # but you can use any symbol for the method value
18
+ # allows for more than 3 forms on a single page
19
+ if method.is_a? Symbol
20
+ method_input = %Q(<input type="hidden" name="_method" value="#{method}" />)
21
+ method = :post
22
+ end
23
+ action = "/#{action}" if action.is_a? Symbol
24
+
25
+ out = tag(:form, nil, { action: action, method: method.to_s }.merge(options)) + method_input
26
+ out << fieldset(action, &block) + '</form>' if block_given?
27
+ out
28
+ end
29
+
30
+ def fieldset(obj, legend = nil, &block)
31
+ raise ArgumentError, "Missing block to fieldset()" unless block_given?
32
+ out = yield Fieldset.new(self, obj)
33
+ '<fieldset>' + (legend.nil? ? '' : "<legend>#{fast_escape_html(legend)}</legend>") + out.to_s + '</fieldset>'
34
+ end
35
+
36
+ # Link to a URL
37
+ def link(content, href = content, options = {})
38
+ tag :a, content, options.merge(href: href)
39
+ end
40
+
41
+ # Link to an image
42
+ def image(src, options = {})
43
+ single_tag :img, options.merge(src: src)
44
+ end
45
+
46
+ # Form field label
47
+ def label(obj, field, display = "", options = {})
48
+ tag :label, (display.nil? || display == '') ? titleize(field.to_s) : display, options.merge(for: css_id(obj, field))
49
+ end
50
+
51
+ # Form text input. Specify the value as value: 'foo'
52
+ def input(obj, field = nil, options = {})
53
+ value = param_or_default(obj, field, options[:value])
54
+ single_tag :input, options.merge(
55
+ type: options[:type] || "text",
56
+ id: css_id(obj, field),
57
+ name: field.nil? ? obj : "#{obj}[#{field}]",
58
+ value: value
59
+ )
60
+ end
61
+
62
+ # Form password input. Specify the value as value: 'foo'
63
+ def password(obj, field = nil, options = {})
64
+ input(obj, field, options.merge(type: 'password'))
65
+ end
66
+
67
+ # Form textarea box.
68
+ def textarea(obj, field = nil, content = '', options = {})
69
+ content = param_or_default(obj, field, content)
70
+ tag :textarea, content, options.merge(
71
+ id: css_id(obj, field),
72
+ name: field.nil? ? obj : "#{obj}[#{field}]"
73
+ )
74
+ end
75
+
76
+ # Form submit tag.
77
+ def submit(value = 'Submit', options = {})
78
+ single_tag :input, {
79
+ name: "submit",
80
+ type: "submit",
81
+ value: value,
82
+ id: css_id('button', value)
83
+ }.merge(options)
84
+ end
85
+
86
+ # Form reset tag. Does anyone use these anymore?
87
+ def reset(value = 'Reset', options = {})
88
+ single_tag :input, {
89
+ name: "reset",
90
+ type: "reset",
91
+ value: value,
92
+ id: css_id('button', value)
93
+ }.merge(options)
94
+ end
95
+
96
+ # General purpose button, usually these need JavaScript hooks.
97
+ def button(value, options = {})
98
+ single_tag :input, {
99
+ name: "button",
100
+ type: "button",
101
+ value: value,
102
+ id: css_id('button', value)
103
+ }.merge(options)
104
+ end
105
+
106
+ # Form checkbox. Specify an array of values to get a checkbox group.
107
+ def checkbox(obj, field, values, options = {})
108
+ join = options.delete(:join) || ' '
109
+ labs = options.delete(:label)
110
+ vals = param_or_default(obj, field, [])
111
+ ary = values.is_a?(Array) && values.length > 1 ? '[]' : ''
112
+ Array(values).collect do |val|
113
+ id, text = id_and_text_from_value(val)
114
+ single_tag(:input, options.merge(
115
+ type: "checkbox",
116
+ id: css_id(obj, field, id),
117
+ name: "#{obj}[#{field}]#{ary}",
118
+ value: id,
119
+ checked: vals.include?(id) ? 'checked' : nil
120
+ )) +
121
+ (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
122
+ end.join(join)
123
+ end
124
+
125
+ # Form radio input. Specify an array of values to get a radio group.
126
+ def radio(obj, field, values, options = {})
127
+ #content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""
128
+ # , checked: content
129
+ join = options.delete(:join) || ' '
130
+ labs = options.delete(:label)
131
+ vals = param_or_default(obj, field, [])
132
+ Array(values).collect do |val|
133
+ id, text = id_and_text_from_value(val)
134
+ single_tag(:input, options.merge(
135
+ type: "radio",
136
+ id: css_id(obj, field, id),
137
+ name: "#{obj}[#{field}]",
138
+ value: id,
139
+ checked: vals.include?(id) ? 'checked' : nil
140
+ )) +
141
+ (labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
142
+ end.join(join)
143
+ end
144
+
145
+ # Form select dropdown. Currently only single-select (not multi-select) is supported.
146
+ def select(obj, field, values, options = {})
147
+ value = param_or_default(obj, field, options[:value])
148
+ content = ""
149
+ Array(values).each do |val|
150
+ id, text = id_and_text_from_value(val)
151
+ tag_options = { value: id }
152
+ tag_options[:selected] = 'selected' if id == value
153
+ content << tag(:option, text, tag_options)
154
+ end
155
+ tag :select, content, options.merge(id: css_id(obj, field), name: "#{obj}[#{field}]")
156
+ end
157
+
158
+ # Form hidden input. Specify value as value: 'foo'
159
+ def hidden(obj, field = nil, options = {})
160
+ input(obj, field, options.merge(type: 'hidden'))
161
+ end
162
+
163
+ # Standard open and close tags
164
+ # EX : tag :h1, "shizam", title: "shizam"
165
+ # => <h1 title="shizam">shizam</h1>
166
+ def tag(name, content, options = {})
167
+ "<#{name.to_s}" +
168
+ (options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') +
169
+ (content.nil? ? '>' : ">#{content}</#{name}>")
170
+ end
171
+
172
+ # Standard single closing tags
173
+ # single_tag :img, src: "images/google.jpg"
174
+ # => <img src="images/google.jpg" />
175
+ def single_tag(name, options = {})
176
+ "<#{name.to_s} #{hash_to_html_attrs(options)} />"
177
+ end
178
+
179
+ def fast_escape_html(text)
180
+ text.to_s.gsub(/\&/,'&amp;').gsub(/\"/,'&quot;').gsub(/>/,'&gt;').gsub(/</,'&lt;')
181
+ end
182
+
183
+ def titleize(text)
184
+ text.to_s.gsub(/_+/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
185
+ end
186
+
187
+ def param_or_default(obj, field, default)
188
+ if field
189
+ params[obj] ? params[obj][field.to_s] || default : default
190
+ else
191
+ params[obj] || default
192
+ end
193
+ end
194
+
195
+ def hash_to_html_attrs(options = {})
196
+ html_attrs = ""
197
+ options.keys.sort.each do |key|
198
+ next if options[key].nil? # do not include empty attributes
199
+ html_attrs << %Q(#{key}="#{fast_escape_html(options[key])}" )
200
+ end
201
+ html_attrs.chop
202
+ end
203
+
204
+ def id_and_text_from_value(val)
205
+ if val.is_a? Array
206
+ [val.first, val.last]
207
+ else
208
+ [val, val]
209
+ end
210
+ end
211
+
212
+ def css_id(*things)
213
+ things.compact.map{|t| t.to_s}.join('_').downcase.gsub(/\W/,'_')
214
+ end
215
+
216
+ class Fieldset
217
+ def initialize(parent, name)
218
+ @parent = parent
219
+ @name = name.to_s.gsub(/\W+/,'')
220
+ end
221
+
222
+ def method_missing(meth, *args)
223
+ @parent.send(meth, @name, *args)
224
+ end
225
+ end
226
+ end
227
+
228
+ helpers FormHelpers
229
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/form_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sinatra-formhelpers-huii'
8
+ spec.version = Sinatra::FormHelpers::VERSION
9
+ spec.authors = ['twilson63', 'Nate Wiger', 'Cymen Vig', 'Oldrich Vetesnik']
10
+ spec.email = ['oldrich.vetesnik@gmail.com']
11
+ spec.summary = %q{Form helpers for Sinatra}
12
+ spec.description = %q{Simple, lightweight form helpers for Sinatra.}
13
+ spec.homepage = 'https://github.com/ollie/sinatra-formhelpers-huii'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake', '~> 10.1'
23
+ spec.add_development_dependency 'rack-test', '~> 0.6'
24
+ spec.add_development_dependency 'rspec', '~> 2.14'
25
+ spec.add_development_dependency 'simplecov', '~> 0.8'
26
+ spec.add_development_dependency 'sinatra', '~> 1.4'
27
+ # spec.add_development_dependency 'pry', '~> 0.9'
28
+ end
@@ -0,0 +1,250 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Sinatra::FormHelpers in app" do
4
+ it 'renders an anchor tag' do
5
+ app.get '/link' do
6
+ erb "<%= link 'google', 'http://www.google.com', :title => 'Google' %>"
7
+ end
8
+
9
+ get '/link'
10
+ expect( last_response.body ).to eq( %q(<a href="http://www.google.com" title="Google">google</a>) )
11
+ end
12
+
13
+ it 'renders a label tag' do
14
+ app.get '/label' do
15
+ erb "<%= label :person, :first_name %>"
16
+ end
17
+
18
+ get '/label'
19
+ expect( last_response.body ).to eq( %q(<label for="person_first_name">First Name</label>) )
20
+ end
21
+
22
+ it 'renders a label tag with display input' do
23
+ app.get '/hello' do
24
+ erb "<%= label :person, :first_name, 'Hello World'%>"
25
+ end
26
+
27
+ get '/hello'
28
+ expect( last_response.body ).to eq( %q(<label for="person_first_name">Hello World</label>) )
29
+ end
30
+
31
+ it 'renders an input tag type text without @params' do
32
+ app.get '/text' do
33
+ erb "<%= input :person, :first_name %>"
34
+ end
35
+
36
+ get '/text'
37
+ expect( last_response.body ).to match( /name="person\[first_name\]"/ )
38
+ expect( last_response.body ).to eq( %q(<input id="person_first_name" name="person[first_name]" type="text" />) )
39
+ end
40
+
41
+ it 'accepts an input tag with custom type option' do
42
+ app.get '/input-with-custom-type' do
43
+ erb "<%= input :person, :password, type: 'password' %>"
44
+ end
45
+
46
+ get '/input-with-custom-type'
47
+ expect( last_response.body ).to eq( '<input id="person_password" name="person[password]" type="password" />' )
48
+ end
49
+
50
+ it 'renders an input tag type text with single arg' do
51
+ app.get '/q' do
52
+ erb "<%= input :q %>"
53
+ end
54
+
55
+ get '/q'
56
+ expect( last_response.body ).to eq( %q(<input id="q" name="q" type="text" />) )
57
+ end
58
+
59
+ it 'renders an input tag type text with @params' do
60
+ app.get '/tom' do
61
+ @params = { :person => {"first_name" => "Tom"}}
62
+ erb "<%= input :person, :first_name %>"
63
+ end
64
+
65
+ get '/tom'
66
+ expect( last_response.body ).to eq( %q(<input id="person_first_name" name="person[first_name]" type="text" value="Tom" />) )
67
+ end
68
+
69
+ it 'renders a password tag type password' do
70
+ app.get '/password' do
71
+ erb "<%= password :person, :password %>"
72
+ end
73
+
74
+ get '/password'
75
+ expect( last_response.body ).to eq( '<input id="person_password" name="person[password]" type="password" />' )
76
+ end
77
+
78
+ it 'renders a button tag type button' do
79
+ app.get '/button' do
80
+ erb "<%= button :new %>"
81
+ end
82
+
83
+ get '/button'
84
+ expect( last_response.body ).to eq( '<input id="button_new" name="button" type="button" value="new" />' )
85
+ end
86
+
87
+ it 'renders an textarea tag type text without @params' do
88
+ app.get '/notes' do
89
+ erb "<%= textarea :person, :notes %>"
90
+ end
91
+
92
+ get '/notes?person[notes]=Yeppers'
93
+ expect( last_response.body ).to eq( %q(<textarea id="person_notes" name="person[notes]">Yeppers</textarea>) )
94
+ end
95
+
96
+ it 'renders a textarea tag with @params' do
97
+ app.get '/notes2' do
98
+ @params = { :person => {"notes" => "This is a note"}}
99
+ erb "<%= textarea :person, :notes %>"
100
+ end
101
+
102
+ get '/notes2'
103
+ expect( last_response.body ).to eq( %q(<textarea id="person_notes" name="person[notes]">This is a note</textarea>) )
104
+ end
105
+
106
+ it 'renders a textarea tag with @params' do
107
+ app.get '/img' do
108
+ erb "<%= image '/images/hello.png', :alt => 'Lolcatz' %>"
109
+ end
110
+
111
+ get '/img'
112
+ expect( last_response.body ).to eq( '<img alt="Lolcatz" src="/images/hello.png" />' )
113
+ end
114
+
115
+ it 'renders an input tag with a submit type' do
116
+ app.get '/sub' do
117
+ erb "<%= submit 'Create' %>"
118
+ end
119
+
120
+ get '/sub'
121
+ expect( last_response.body ).to eq( '<input id="button_create" name="submit" type="submit" value="Create" />' )
122
+ end
123
+
124
+ it 'renders an input tag with a submit type with zero args' do
125
+ app.get '/create' do
126
+ erb "<%= submit %>"
127
+ end
128
+
129
+ get '/create'
130
+ expect( last_response.body ).to eq( '<input id="button_submit" name="submit" type="submit" value="Submit" />' )
131
+ end
132
+
133
+ it 'renders an input tag with a checkbox type' do
134
+ app.get '/check' do
135
+ erb "<%= checkbox :person, :active, 'Yes' %>"
136
+ end
137
+
138
+ get '/check'
139
+ expect( last_response.body ).to eq(
140
+ '<input id="person_active_yes" name="person[active]" type="checkbox" value="Yes" /><label for="person_active_yes">Yes</label>'
141
+ )
142
+ end
143
+
144
+ it 'renders an input tag with a multiple checkbox type' do
145
+ app.get '/check2' do
146
+ erb "<%= checkbox :person, :eyes, [1,2,3] %>"
147
+ end
148
+
149
+ get '/check2'
150
+ expect( last_response.body ).to eq(
151
+ '<input id="person_eyes_1" name="person[eyes][]" type="checkbox" value="1" /><label for="person_eyes_1">1</label> ' +
152
+ '<input id="person_eyes_2" name="person[eyes][]" type="checkbox" value="2" /><label for="person_eyes_2">2</label> ' +
153
+ '<input id="person_eyes_3" name="person[eyes][]" type="checkbox" value="3" /><label for="person_eyes_3">3</label>'
154
+ )
155
+ end
156
+
157
+ it 'renders an input tag with a radio type' do
158
+ app.get '/radio' do
159
+ erb "<%= radio :person, :gender, [['M','Male'],['F','Female'],'Other'] %>"
160
+ end
161
+
162
+ get '/radio'
163
+ expect( last_response.body ).to eq(
164
+ %(<input id="person_gender_m" name="person[gender]" type="radio" value="M" /><label for="person_gender_m">Male</label> ) +
165
+ %(<input id="person_gender_f" name="person[gender]" type="radio" value="F" /><label for="person_gender_f">Female</label> ) +
166
+ %(<input id="person_gender_other" name="person[gender]" type="radio" value="Other" /><label for="person_gender_other">Other</label>)
167
+ )
168
+ end
169
+
170
+ it 'renders a select tag' do
171
+ app.get '/select' do
172
+ erb "<%= select :person, :relationship, ['Friend','CoWorker','Lead'] %>"
173
+ end
174
+
175
+ get '/select'
176
+ expect( last_response.body ).to eq( '<select id="person_relationship" name="person[relationship]">' +
177
+ '<option value="Friend">Friend</option><option value="CoWorker">CoWorker</option>' +
178
+ '<option value="Lead">Lead</option></select>' )
179
+ end
180
+
181
+ it 'renders a select tag with selected option' do
182
+ app.get '/select2' do
183
+ @params = { :person => {"relationship" => "CoWorker"}}
184
+ erb "<%= select :person, :relationship, ['Friend','CoWorker','Lead'] %>"
185
+ end
186
+
187
+ get '/select2'
188
+ expect( last_response.body ).to eq( '<select id="person_relationship" name="person[relationship]">' +
189
+ '<option value="Friend">Friend</option><option selected="selected" value="CoWorker">CoWorker</option>' +
190
+ '<option value="Lead">Lead</option></select>' )
191
+ end
192
+
193
+ it 'renders a hidden tag with single arg' do
194
+ app.get '/hidden' do
195
+ erb "<%= hidden :q %>"
196
+ end
197
+
198
+ get '/hidden'
199
+ expect( last_response.body ).to eq( %q(<input id="q" name="q" type="hidden" />) )
200
+ end
201
+
202
+ it 'renders a hidden tag with value' do
203
+ app.get '/hidden2' do
204
+ erb '<%= hidden :person, :id, :value => 1 %>'
205
+ end
206
+
207
+ get '/hidden2'
208
+ expect( last_response.body ).to eq( '<input id="person_id" name="person[id]" type="hidden" value="1" />' )
209
+ end
210
+
211
+ it 'renders a form tag' do
212
+ app.get '/form' do
213
+ erb "<%= form :person, :create %>"
214
+ end
215
+
216
+ get '/form'
217
+ expect( last_response.body ).to eq( %q(<form action="/person" method="post"><input type="hidden" name="_method" value="create" />) )
218
+ end
219
+
220
+ it 'renders a fieldset group' do
221
+ app.get '/fieldset' do
222
+ erb <<-EndTemplate
223
+ <% fieldset(:user) do |f| %>
224
+ <%= f.input(:first_name) %>
225
+ <%= f.input(:last_name) %>
226
+
227
+ <%= f.input(:email, :size => 40) %>
228
+
229
+ <%= f.password(:password) %>
230
+ <%= f.password(:confirm_password) %>
231
+
232
+ <%= f.radio(:gender, ['M', 'F']) %>
233
+ <% end %>
234
+ <%= submit %>
235
+ EndTemplate
236
+ end
237
+
238
+ get '/fieldset'
239
+ expect( last_response.body ).to eq(
240
+ %( <input id="user_first_name" name="user[first_name]" type="text" />\n) +
241
+ %( <input id="user_last_name" name="user[last_name]" type="text" />\n\n) +
242
+ %( <input id="user_email" name="user[email]" size="40" type="text" />\n\n) +
243
+ %( <input id="user_password" name="user[password]" type="password" />\n) +
244
+ %( <input id="user_confirm_password" name="user[confirm_password]" type="password" />\n\n) +
245
+ %( <input id="user_gender_m" name="user[gender]" type="radio" value="M" /><label for="user_gender_m">M</label>) +
246
+ %( <input id="user_gender_f" name="user[gender]" type="radio" value="F" /><label for="user_gender_f">F</label>\n) +
247
+ %(<input id="button_submit" name="submit" type="submit" value="Submit" />)
248
+ )
249
+ end
250
+ end
@@ -0,0 +1,39 @@
1
+ require 'rack/test'
2
+ require 'simplecov'
3
+ require 'sinatra/base'
4
+
5
+ # Coverage tool, needs to be started as soon as possible
6
+ SimpleCov.start do
7
+ add_filter '/spec/' # Ignore spec directory
8
+ end
9
+
10
+ require 'sinatra/form_helpers'
11
+
12
+ class TestFormHelpers
13
+ include Sinatra::FormHelpers
14
+ attr_accessor :params
15
+ def initialize
16
+ @params = {}
17
+ end
18
+ end
19
+ def fh
20
+ @fh ||= TestFormHelpers.new
21
+ end
22
+
23
+ class Application < Sinatra::Base
24
+ helpers Sinatra::FormHelpers
25
+ set :raise_errors, false
26
+ set :show_exceptions, false
27
+ end
28
+
29
+ module RSpecMixin
30
+ include Rack::Test::Methods
31
+
32
+ def app
33
+ Application
34
+ end
35
+ end
36
+
37
+ RSpec.configure do |config|
38
+ config.include RSpecMixin
39
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Sinatra::FormHelpers methods" do
4
+ it 'renders an anchor tag' do
5
+ expect( fh.form(:person, :create) ).to eq( '<form action="/person" method="post"><input type="hidden" name="_method" value="create" />' )
6
+ end
7
+
8
+ it 'renders a form tag' do
9
+ expect( fh.form(:person, :update, :action => "/people/14") ).to eq(
10
+ '<form action="/people/14" method="post"><input type="hidden" name="_method" value="update" />'
11
+ )
12
+ end
13
+
14
+ it 'renders a form tag (2)' do
15
+ expect( fh.form("/people/7", :delete) ).to eq(
16
+ '<form action="/people/7" method="post"><input type="hidden" name="_method" value="delete" />'
17
+ )
18
+ end
19
+
20
+ it 'renders a form tag (3)' do
21
+ expect( fh.form("/people", :create) ).to eq(
22
+ '<form action="/people" method="post"><input type="hidden" name="_method" value="create" />'
23
+ )
24
+ end
25
+
26
+ it 'renders a nested form tag' do
27
+ expect( fh.form(:person, :create) do |f|
28
+ # f.input(:first_name)
29
+ f.input(:last_name)
30
+ end ).to eq( '<form action="/person" method="post"><input type="hidden" name="_method" value="create" /><fieldset>' +
31
+ # '<input id="person_first_name" name="person[first_name]" type="text" />' +
32
+ '<input id="person_last_name" name="person[last_name]" type="text" />' +
33
+ '</fieldset></form>'
34
+ )
35
+ end
36
+
37
+ it 'renders a nested form tag with different action and namespace' do
38
+ expect( fh.form(:person, :create, :action => "/people") do |f|
39
+ f.input(:name)
40
+ end ).to eq( '<form action="/people" method="post"><input type="hidden" name="_method" value="create" /><fieldset>' +
41
+ '<input id="person_name" name="person[name]" type="text" />' +
42
+ '</fieldset></form>'
43
+ )
44
+ end
45
+
46
+ it 'nests form elements arbitrarily' do
47
+ expect( fh.fieldset(:person) do |n|
48
+ # n.radio(:partners, 1..6)
49
+ n.textarea(:comments, "yo yo yo!")
50
+ end ).to eq( '<fieldset>' +
51
+ # '<input id="person_partners_1" name="person[partners]" type="radio" value="1" /> ' +
52
+ # '<input id="person_partners_2" name="person[partners]" type="radio" value="2" /> ' +
53
+ # '<input id="person_partners_3" name="person[partners]" type="radio" value="3" /> ' +
54
+ # '<input id="person_partners_4" name="person[partners]" type="radio" value="4" /> ' +
55
+ # '<input id="person_partners_5" name="person[partners]" type="radio" value="5" /> ' +
56
+ # '<input id="person_partners_6" name="person[partners]" type="radio" value="6" />' +
57
+ '<textarea id="person_comments" name="person[comments]">yo yo yo!</textarea></fieldset>'
58
+ )
59
+ end
60
+
61
+ it 'renders a fieldset tag with an input' do
62
+ expect( fh.fieldset(:user) do |f|
63
+ f.input(:last_name)
64
+ end ).to eq( '<fieldset><input id="user_last_name" name="user[last_name]" type="text" /></fieldset>' )
65
+ end
66
+
67
+ it 'renders an empty fieldset tag' do
68
+ expect( fh.fieldset(:user) do |f|
69
+ end ).to eq( '<fieldset></fieldset>' )
70
+ end
71
+
72
+ it 'renders a fieldset tag with legend' do
73
+ expect( fh.fieldset(:user, 'Oh boy!') do |f|
74
+ end ).to eq( '<fieldset><legend>Oh boy!</legend></fieldset>' )
75
+ end
76
+
77
+ it 'renders a link tag' do
78
+ expect( fh.link('http://google.com') ).to eq( '<a href="http://google.com">http://google.com</a>' )
79
+ end
80
+
81
+ it 'renders a form label tag' do
82
+ expect( fh.label(:person, :first_name) ).to eq( '<label for="person_first_name">First Name</label>' )
83
+ end
84
+
85
+ it 'renders a form checkbox tag' do
86
+ expect( fh.checkbox(:person, :gender, %w[M F], :join => '<br />') ).to eq(
87
+ '<input id="person_gender_m" name="person[gender][]" type="checkbox" value="M" /><label for="person_gender_m">M</label><br />' +
88
+ '<input id="person_gender_f" name="person[gender][]" type="checkbox" value="F" /><label for="person_gender_f">F</label>'
89
+ )
90
+ end
91
+
92
+ it 'renders a minimal text tag' do
93
+ expect( fh.input(:q) ).to eq( %q(<input id="q" name="q" type="text" />) )
94
+ end
95
+
96
+ it 'renders a minimal textarea tag' do
97
+ expect( fh.textarea(:r) ).to eq( %q(<textarea id="r" name="r"></textarea>) )
98
+ end
99
+
100
+ it 'renders a submit tag' do
101
+ expect( fh.submit ).to eq( %q(<input id="button_submit" name="submit" type="submit" value="Submit" />) )
102
+ expect( fh.submit("Send!") ).to eq( %q(<input id="button_send_" name="submit" type="submit" value="Send!" />) )
103
+ end
104
+
105
+ it 'renders a reset tag' do
106
+ expect( fh.reset ).to eq( %q(<input id="button_reset" name="reset" type="reset" value="Reset" />) )
107
+ expect( fh.reset("Blark") ).to eq( %q(<input id="button_blark" name="reset" type="reset" value="Blark" />) )
108
+ end
109
+
110
+ it 'supports multiple values for checkboxes' do
111
+ fh.params = {:user => {'devices' => ['iPhone', 'iPad'] }}
112
+ expect( fh.checkbox(:user, :devices, ['iPhone', 'iPad', 'iPod', 'iPoop']) ).to eq(
113
+ "<input checked=\"checked\" id=\"user_devices_iphone\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPhone\" /><label for=\"user_devices_iphone\">iPhone</label> <input checked=\"checked\" id=\"user_devices_ipad\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPad\" /><label for=\"user_devices_ipad\">iPad</label> <input id=\"user_devices_ipod\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPod\" /><label for=\"user_devices_ipod\">iPod</label> <input id=\"user_devices_ipoop\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPoop\" /><label for=\"user_devices_ipoop\">iPoop</label>"
114
+ )
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-formhelpers-huii
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - twilson63
8
+ - Nate Wiger
9
+ - Cymen Vig
10
+ - Oldrich Vetesnik
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2014-02-12 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.5'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: '10.1'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '10.1'
44
+ - !ruby/object:Gem::Dependency
45
+ name: rack-test
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ~>
49
+ - !ruby/object:Gem::Version
50
+ version: '0.6'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: '0.6'
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ version: '2.14'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: '2.14'
72
+ - !ruby/object:Gem::Dependency
73
+ name: simplecov
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '0.8'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '0.8'
86
+ - !ruby/object:Gem::Dependency
87
+ name: sinatra
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ version: '1.4'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: '1.4'
100
+ description: Simple, lightweight form helpers for Sinatra.
101
+ email:
102
+ - oldrich.vetesnik@gmail.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - .gitignore
108
+ - .rspec
109
+ - .travis.yml
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE
113
+ - README.md
114
+ - Rakefile
115
+ - lib/sinatra/form_helpers.rb
116
+ - lib/sinatra/form_helpers/version.rb
117
+ - sinatra-formhelpers-huii.gemspec
118
+ - spec/integration/form_helpers_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/units/form_helpers_spec.rb
121
+ homepage: https://github.com/ollie/sinatra-formhelpers-huii
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Form helpers for Sinatra
145
+ test_files:
146
+ - spec/integration/form_helpers_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/units/form_helpers_spec.rb
149
+ has_rdoc: