lazy_form 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +110 -0
- data/Rakefile +9 -0
- data/lazy_form.gemspec +31 -0
- data/lib/lazy_form/sinatra.rb +9 -0
- data/lib/lazy_form.rb +166 -0
- data/test/builder_test.rb +138 -0
- data/test/cuba_test.rb +27 -0
- data/test/fixtures/_form.erb +4 -0
- data/test/helper.rb +10 -0
- data/test/hobbit_test.rb +29 -0
- data/test/sinatra_test.rb +26 -0
- data/test/tag_test.rb +53 -0
- metadata +206 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46cb809f06ff9f3f265019d51f973614b47e4ea2
|
4
|
+
data.tar.gz: 2c1bcc21bce9ba0ffde4dee860d05034f55d76e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39e24a9f2be2169e232b29fa7a5747a318679a3028bb9fa0e5e79cb4a5da9af299f65cd85eb5fe9e792fda4dbe1c6a971cc0320d61a9e7bf3b90a859cb6380f1
|
7
|
+
data.tar.gz: 4f96c72df723fef854157511359cf5394062c8e82de0ea162cdc407d1e901d95833652a153d142a7a839c3a611924ef4cc6aed170e27fcaec9f1e343dabbb4cf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Patricio Mac Adden
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# LazyForm
|
2
|
+
|
3
|
+
Forms for the rest of us, the lazy.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'lazy_form'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ gem install lazy_form
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
It's quite simple. See an example:
|
28
|
+
|
29
|
+
```html
|
30
|
+
<%# views/_form.erb %>
|
31
|
+
<% form_for User.new, '/users' do |f| %>
|
32
|
+
<p>
|
33
|
+
<%= f.label :username, 'Username' %>
|
34
|
+
<%= f.text :username %>
|
35
|
+
</p>
|
36
|
+
<p>
|
37
|
+
<%= f.label :password, 'Password' %>
|
38
|
+
<%= f.password :password %>
|
39
|
+
</p>
|
40
|
+
<p>
|
41
|
+
<%= f.label :admin, 'Admin?' %>
|
42
|
+
<%= f.checkbox :admin %>
|
43
|
+
</p>
|
44
|
+
<% end %>
|
45
|
+
```
|
46
|
+
|
47
|
+
### Example using [Cuba](https://github.com/soveran/cuba)
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'cuba'
|
51
|
+
require 'cuba/render'
|
52
|
+
require 'lazy_form'
|
53
|
+
|
54
|
+
Cuba.plugin Cuba::Render
|
55
|
+
Cuba.plugin LazyForm::Helper
|
56
|
+
|
57
|
+
Cuba.define do
|
58
|
+
on root do
|
59
|
+
partial '_form'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
### Example using [Hobbit](https://github.com/patriciomacadden/hobbit)
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require 'hobbit'
|
68
|
+
require 'hobbit/render'
|
69
|
+
require 'lazy_form'
|
70
|
+
|
71
|
+
class App < Hobbit::Base
|
72
|
+
include LazyForm::Helper
|
73
|
+
|
74
|
+
get '/' do
|
75
|
+
partial 'form'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
### Example using [Sinatra](https://github.com/sinatra/sinatra)
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'sinatra/base'
|
84
|
+
require 'lazy_form/sinatra'
|
85
|
+
|
86
|
+
class App < Sinatra::Base
|
87
|
+
include LazyForm::Helper
|
88
|
+
|
89
|
+
get '/' do
|
90
|
+
partial :'form', layout: false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
## What else can I do with this gem?
|
96
|
+
|
97
|
+
Please see the `LazyForm::Builder` class in order to see available methods. I'm
|
98
|
+
too lazy to write the docs!
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
1. Fork it
|
103
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
104
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
105
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
106
|
+
5. Create a new Pull Request
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
See the [LICENSE](https://github.com/patriciomacadden/lazy_form/blob/master/LICENSE).
|
data/Rakefile
ADDED
data/lazy_form.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'lazy_form'
|
7
|
+
spec.version = '0.0.2'
|
8
|
+
spec.authors = ['Patricio Mac Adden']
|
9
|
+
spec.email = ['patriciomacadden@gmail.com']
|
10
|
+
spec.summary = %q{Forms for the rest of us, the lazy.}
|
11
|
+
spec.description = %q{Forms for the rest of us, the lazy.}
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'cuba', '~> 3.3.0'
|
22
|
+
spec.add_development_dependency 'hobbit', '~> 0.6.0'
|
23
|
+
spec.add_development_dependency 'hobbit-contrib', '~> 0.7.1'
|
24
|
+
spec.add_development_dependency 'oktobertest'
|
25
|
+
spec.add_development_dependency 'oktobertest-contrib'
|
26
|
+
spec.add_development_dependency 'sinatra', '~> 1.4.5'
|
27
|
+
spec.add_development_dependency 'rack-test'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'inflecto'
|
31
|
+
end
|
data/lib/lazy_form.rb
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'inflecto'
|
2
|
+
|
3
|
+
module LazyForm
|
4
|
+
module Helper
|
5
|
+
def outvar
|
6
|
+
@_output
|
7
|
+
end
|
8
|
+
|
9
|
+
def form_for(object, url, attributes = {}, &block)
|
10
|
+
attributes[:action] = url
|
11
|
+
attributes[:method] ||= 'POST'
|
12
|
+
|
13
|
+
attrs = attributes.collect { |k, v| "#{k}=\"#{v}\"" }
|
14
|
+
attrs.unshift 'form'
|
15
|
+
|
16
|
+
outvar << "<#{attrs.reject(&:empty?).join ' '}>\n"
|
17
|
+
form_builder = Builder.new object
|
18
|
+
block.call form_builder
|
19
|
+
outvar << "</form>\n"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Tag
|
24
|
+
attr_reader :name, :attributes, :block
|
25
|
+
|
26
|
+
def initialize(name, attributes = {}, &block)
|
27
|
+
@name = name
|
28
|
+
@attributes = attributes
|
29
|
+
@block = block
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
attrs = build_attributes attributes.reject { |k, v| v.nil? }
|
34
|
+
attrs.unshift name
|
35
|
+
|
36
|
+
if block.nil?
|
37
|
+
"<#{attrs.reject(&:empty?).join ' '}/>"
|
38
|
+
else
|
39
|
+
"<#{attrs.reject(&:empty?).join ' '}>#{block.call}</#{name}>"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def build_attributes(attributes = {})
|
46
|
+
attributes.collect do |k, v|
|
47
|
+
if v.is_a? Hash
|
48
|
+
build_attributes Hash[v.collect { |ik, iv| [:"#{k}-#{ik}", iv] }]
|
49
|
+
else
|
50
|
+
"#{k}=\"#{v}\""
|
51
|
+
end
|
52
|
+
end.flatten
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Builder
|
57
|
+
BUTTONS = %w(button image reset submit)
|
58
|
+
|
59
|
+
INPUT_TYPES = [
|
60
|
+
'checkbox',
|
61
|
+
'color',
|
62
|
+
'date',
|
63
|
+
'datetime',
|
64
|
+
'email',
|
65
|
+
'file',
|
66
|
+
'hidden',
|
67
|
+
'month',
|
68
|
+
'number',
|
69
|
+
'password',
|
70
|
+
'radio',
|
71
|
+
'range',
|
72
|
+
'search',
|
73
|
+
'tel',
|
74
|
+
'text',
|
75
|
+
'time',
|
76
|
+
'url',
|
77
|
+
'week'
|
78
|
+
]
|
79
|
+
|
80
|
+
attr_reader :object
|
81
|
+
|
82
|
+
def initialize(object)
|
83
|
+
@object = object
|
84
|
+
end
|
85
|
+
|
86
|
+
BUTTONS.each do |type|
|
87
|
+
define_method type do |attributes = {}|
|
88
|
+
attributes[:type] = type
|
89
|
+
|
90
|
+
Tag.new 'input', attributes
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def datetime_local(object_attribute, attributes = {})
|
95
|
+
attributes[:id] ||= as_id object_attribute
|
96
|
+
attributes[:name] ||= as_name object_attribute
|
97
|
+
attributes[:type] = 'datetime-local'
|
98
|
+
begin
|
99
|
+
attributes[:value] ||= object.send object_attribute
|
100
|
+
rescue NoMethodError
|
101
|
+
end
|
102
|
+
|
103
|
+
Tag.new 'input', attributes
|
104
|
+
end
|
105
|
+
|
106
|
+
INPUT_TYPES.each do |type|
|
107
|
+
define_method type do |object_attribute, attributes = {}|
|
108
|
+
attributes[:id] ||= as_id object_attribute
|
109
|
+
attributes[:name] ||= as_name object_attribute
|
110
|
+
attributes[:type] = type
|
111
|
+
begin
|
112
|
+
attributes[:value] ||= object.send object_attribute
|
113
|
+
rescue NoMethodError
|
114
|
+
end
|
115
|
+
|
116
|
+
Tag.new 'input', attributes
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def label(object_attribute, content = nil, attributes = {})
|
121
|
+
attributes[:for] ||= as_id object_attribute
|
122
|
+
|
123
|
+
Tag.new('label', attributes) { content }
|
124
|
+
end
|
125
|
+
|
126
|
+
def select(object_attribute, options = {}, attributes = {})
|
127
|
+
attributes[:id] ||= as_id object_attribute
|
128
|
+
attributes[:name] ||= as_name object_attribute
|
129
|
+
|
130
|
+
Tag.new('select', attributes) { build_options object_attribute, options }
|
131
|
+
end
|
132
|
+
|
133
|
+
def textarea(object_attribute, content = nil, attributes = {})
|
134
|
+
attributes[:id] ||= as_id object_attribute
|
135
|
+
attributes[:name] ||= as_name object_attribute
|
136
|
+
content ||= object.send object_attribute
|
137
|
+
|
138
|
+
Tag.new('textarea', attributes) { content }
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
def as_id(attribute)
|
144
|
+
Inflecto.underscore "#{object.class.name}_#{attribute}"
|
145
|
+
end
|
146
|
+
|
147
|
+
def as_name(attribute)
|
148
|
+
"#{Inflecto.underscore object.class.name}[#{attribute}]"
|
149
|
+
end
|
150
|
+
|
151
|
+
def build_options(object_attribute, options = {})
|
152
|
+
options.collect do |k, v|
|
153
|
+
if v.is_a? Hash
|
154
|
+
Tag.new('optgroup', label: k) { build_options object_attribute, v }
|
155
|
+
else
|
156
|
+
opts = { value: k }
|
157
|
+
begin
|
158
|
+
opts[:selected] = :selected if k == object.send(object_attribute)
|
159
|
+
rescue NoMethodError
|
160
|
+
end
|
161
|
+
Tag.new('option', opts) { v }
|
162
|
+
end
|
163
|
+
end.join
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
scope LazyForm::Builder do
|
4
|
+
setup do
|
5
|
+
@person = Person.new
|
6
|
+
@builder = LazyForm::Builder.new @person
|
7
|
+
end
|
8
|
+
|
9
|
+
scope '#initialize' do
|
10
|
+
test 'returns a form builder' do
|
11
|
+
assert_instance_of LazyForm::Builder, @builder
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
LazyForm::Builder::BUTTONS.each do |type|
|
16
|
+
scope type do
|
17
|
+
test "returns a #{type} input" do
|
18
|
+
tag = @builder.send type
|
19
|
+
assert_equal "<input type=\"#{type}\"/>", tag.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
test "returns a #{type} input with attributes" do
|
23
|
+
tag = @builder.send type, style: 'margin-top: 5px', data: { something: 'something' }
|
24
|
+
assert_equal "<input style=\"margin-top: 5px\" data-something=\"something\" type=\"#{type}\"/>", tag.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
scope '#datetime_local' do
|
30
|
+
test "returns a datetime-local input" do
|
31
|
+
tag = @builder.datetime_local :birth_date
|
32
|
+
assert_equal '<input id="person_birth_date" name="person[birth_date]" type="datetime-local"/>', tag.to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
test "returns a datetime-local input with attributes" do
|
36
|
+
tag = @builder.datetime_local :birth_date, style: 'margin-top: 5px', data: { something: 'something' }
|
37
|
+
assert_equal '<input style="margin-top: 5px" data-something="something" id="person_birth_date" name="person[birth_date]" type="datetime-local"/>', tag.to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
LazyForm::Builder::INPUT_TYPES.each do |type|
|
42
|
+
scope type do
|
43
|
+
test "returns a #{type} input" do
|
44
|
+
tag = @builder.send type, :first_name
|
45
|
+
assert_equal "<input id=\"person_first_name\" name=\"person[first_name]\" type=\"#{type}\"/>", tag.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
test "returns a #{type} input with attributes" do
|
49
|
+
tag = @builder.send type, :first_name, style: 'margin-top: 5px', data: { something: 'something' }
|
50
|
+
assert_equal "<input style=\"margin-top: 5px\" data-something=\"something\" id=\"person_first_name\" name=\"person[first_name]\" type=\"#{type}\"/>", tag.to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
scope '#label' do
|
56
|
+
test 'returns a label' do
|
57
|
+
assert_equal '<label for="person_first_name">First name</label>', @builder.label(:first_name, 'First name').to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
test 'returns a label with attributes' do
|
61
|
+
assert_equal '<label style="color: blue" for="person_first_name">First name</label>', @builder.label(:first_name, 'First name', style: 'color: blue').to_s
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
scope '#select' do
|
66
|
+
test 'returns a select' do
|
67
|
+
assert_equal '<select id="person_gender" name="person[gender]"></select>', @builder.select(:gender).to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'returns a select with attributes' do
|
71
|
+
assert_equal '<select style="color: blue" id="person_gender" name="person[gender]"></select>', @builder.select(:gender, {}, style: 'color: blue').to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'returns a select with options' do
|
75
|
+
assert_equal '<select id="person_gender" name="person[gender]"><option value="m">Male</option><option value="f">Female</option></select>', @builder.select(:gender, { m: 'Male', f: 'Female' }).to_s
|
76
|
+
end
|
77
|
+
|
78
|
+
test 'returns a select with a selected option' do
|
79
|
+
@person.gender = :m
|
80
|
+
assert_equal '<select id="person_gender" name="person[gender]"><option value="m" selected="selected">Male</option><option value="f">Female</option></select>', @builder.select(:gender, { m: 'Male', f: 'Female' }).to_s
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
scope '#textarea' do
|
85
|
+
test 'returns a textarea' do
|
86
|
+
assert_equal '<textarea id="person_first_name" name="person[first_name]"></textarea>', @builder.textarea(:first_name).to_s
|
87
|
+
end
|
88
|
+
|
89
|
+
test "returns a textarea with the object's value" do
|
90
|
+
@person.first_name = 'Patricio'
|
91
|
+
assert_equal '<textarea id="person_first_name" name="person[first_name]">Patricio</textarea>', @builder.textarea(:first_name).to_s
|
92
|
+
end
|
93
|
+
|
94
|
+
test 'returns a textarea with a default value' do
|
95
|
+
assert_equal '<textarea id="person_first_name" name="person[first_name]">First name</textarea>', @builder.textarea(:first_name, 'First name').to_s
|
96
|
+
end
|
97
|
+
|
98
|
+
test 'returns a textarea with attributes' do
|
99
|
+
assert_equal '<textarea style="color: blue" id="person_first_name" name="person[first_name]"></textarea>', @builder.textarea(:first_name, nil, style: 'color: blue').to_s
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
scope '#as_id' do
|
104
|
+
test "returns an attribute's html id" do
|
105
|
+
assert_equal 'person_first_name', @builder.send(:as_id, :first_name)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
scope '#as_name' do
|
110
|
+
test "returns an attribute's html name" do
|
111
|
+
assert_equal 'person[first_name]', @builder.send(:as_name, :first_name)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
scope '#build_options' do
|
116
|
+
test 'builds options for a select' do
|
117
|
+
assert_equal '<option value="m">Male</option><option value="f">Female</option>', @builder.send(:build_options, :gender, { m: 'Male', f: 'Female' })
|
118
|
+
end
|
119
|
+
|
120
|
+
test 'builds options for a select with a selected option' do
|
121
|
+
@person.gender = :m
|
122
|
+
assert_equal '<option value="m" selected="selected">Male</option><option value="f">Female</option>', @builder.send(:build_options, :gender, { m: 'Male', f: 'Female' })
|
123
|
+
end
|
124
|
+
|
125
|
+
test 'supports optgroups' do
|
126
|
+
assert_equal '<optgroup label="1"><option value="a">A</option></optgroup><optgroup label="2"><option value="b">B</option></optgroup>', @builder.send(:build_options, :gender, { 1 => { a: 'A' }, 2 => { b: 'B' } })
|
127
|
+
end
|
128
|
+
|
129
|
+
test 'supports optgroups with a selected option' do
|
130
|
+
@person.gender = :a
|
131
|
+
assert_equal '<optgroup label="1"><option value="a" selected="selected">A</option></optgroup><optgroup label="2"><option value="b">B</option></optgroup>', @builder.send(:build_options, :gender, { 1 => { a: 'A' }, 2 => { b: 'B' } })
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
test 'supports fields without a corresponding attribute in the receiver object' do
|
136
|
+
assert_equal '<input name="_method" value="PUT" id="person__method" type="hidden"/>', @builder.hidden('_method', name: '_method', value: 'PUT').to_s
|
137
|
+
end
|
138
|
+
end
|
data/test/cuba_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
require 'cuba'
|
4
|
+
require 'cuba/render'
|
5
|
+
|
6
|
+
Cuba.plugin Cuba::Render
|
7
|
+
Cuba.plugin LazyForm::Helper
|
8
|
+
|
9
|
+
Cuba.settings[:render][:views] = 'test/fixtures'
|
10
|
+
|
11
|
+
Cuba.define do
|
12
|
+
on default do
|
13
|
+
res.write partial '_form'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
scope 'test Cuba' do
|
18
|
+
def app
|
19
|
+
Cuba
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'displays a form' do
|
23
|
+
get '/'
|
24
|
+
assert_status 200
|
25
|
+
assert last_response.body.include? "<form action=\"/person\" method=\"POST\">\n <label for=\"person_first_name\">First name</label>\n <input id=\"person_first_name\" name=\"person[first_name]\" type=\"text\"/>\n</form>"
|
26
|
+
end
|
27
|
+
end
|
data/test/helper.rb
ADDED
data/test/hobbit_test.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
require 'hobbit'
|
4
|
+
require 'hobbit/render'
|
5
|
+
|
6
|
+
class HobbitApp < Hobbit::Base
|
7
|
+
include Hobbit::Render
|
8
|
+
include LazyForm::Helper
|
9
|
+
|
10
|
+
def views_path
|
11
|
+
'test/fixtures'
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/' do
|
15
|
+
partial 'form'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
scope 'test Hobbit' do
|
20
|
+
def app
|
21
|
+
HobbitApp
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'displays a form' do
|
25
|
+
get '/'
|
26
|
+
assert_status 200
|
27
|
+
assert last_response.body.include? "<form action=\"/person\" method=\"POST\">\n <label for=\"person_first_name\">First name</label>\n <input id=\"person_first_name\" name=\"person[first_name]\" type=\"text\"/>\n</form>"
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
require 'lazy_form/sinatra'
|
5
|
+
|
6
|
+
class SinatraApp < Sinatra::Base
|
7
|
+
include LazyForm::Helper
|
8
|
+
|
9
|
+
set :views, 'test/fixtures'
|
10
|
+
|
11
|
+
get '/' do
|
12
|
+
erb :'_form', layout: false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
scope 'test Sinatra' do
|
17
|
+
def app
|
18
|
+
SinatraApp
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'displays a form' do
|
22
|
+
get '/'
|
23
|
+
assert_status 200
|
24
|
+
assert last_response.body.include? "<form action=\"/person\" method=\"POST\">\n <label for=\"person_first_name\">First name</label>\n <input id=\"person_first_name\" name=\"person[first_name]\" type=\"text\"/>\n</form>"
|
25
|
+
end
|
26
|
+
end
|
data/test/tag_test.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
scope LazyForm::Tag do
|
4
|
+
scope '#initialize' do
|
5
|
+
test 'creates an instance of Tag' do
|
6
|
+
tag = LazyForm::Tag.new 'input'
|
7
|
+
assert_instance_of LazyForm::Tag, tag
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
scope '#to_s' do
|
12
|
+
test 'returns an html string' do
|
13
|
+
tag = LazyForm::Tag.new 'br'
|
14
|
+
assert_equal '<br/>', tag.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'returns an html string with attributes' do
|
18
|
+
tag = LazyForm::Tag.new 'hr', style: 'margin-top: 5px'
|
19
|
+
assert_equal '<hr style="margin-top: 5px"/>', tag.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'returns an html string with content' do
|
23
|
+
tag = LazyForm::Tag.new('p') { 'Hello world' }
|
24
|
+
assert_equal '<p>Hello world</p>', tag.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'returns an html string with attributes and content' do
|
28
|
+
tag = LazyForm::Tag.new('p', style: 'color: blue') { 'Hello world' }
|
29
|
+
assert_equal '<p style="color: blue">Hello world</p>', tag.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
scope '#build_attributes' do
|
34
|
+
setup do
|
35
|
+
@tag = LazyForm::Tag.new 'div'
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'returns an array of attributes (k=v)' do
|
39
|
+
attributes = @tag.send :build_attributes, key: 'value'
|
40
|
+
assert_equal ['key="value"'], attributes
|
41
|
+
end
|
42
|
+
|
43
|
+
test 'returns an array of attributes with nested keys (for data-* attributes)' do
|
44
|
+
attributes = @tag.send :build_attributes, data: { toggle: 'toggle' }
|
45
|
+
assert_equal ['data-toggle="toggle"'], attributes
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'returns an array of attributes with regular keys and nested keys' do
|
49
|
+
attributes = @tag.send :build_attributes, key: 'value', data: { toggle: 'toggle' }
|
50
|
+
assert_equal ['key="value"', 'data-toggle="toggle"'], attributes
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patricio Mac Adden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cuba
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.3.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.3.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hobbit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.6.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.6.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hobbit-contrib
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.7.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.7.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: oktobertest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: oktobertest-contrib
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sinatra
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.4.5
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.4.5
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rack-test
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rake
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '10.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '10.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: inflecto
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Forms for the rest of us, the lazy.
|
154
|
+
email:
|
155
|
+
- patriciomacadden@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- Gemfile
|
162
|
+
- LICENSE
|
163
|
+
- README.md
|
164
|
+
- Rakefile
|
165
|
+
- lazy_form.gemspec
|
166
|
+
- lib/lazy_form.rb
|
167
|
+
- lib/lazy_form/sinatra.rb
|
168
|
+
- test/builder_test.rb
|
169
|
+
- test/cuba_test.rb
|
170
|
+
- test/fixtures/_form.erb
|
171
|
+
- test/helper.rb
|
172
|
+
- test/hobbit_test.rb
|
173
|
+
- test/sinatra_test.rb
|
174
|
+
- test/tag_test.rb
|
175
|
+
homepage: ''
|
176
|
+
licenses:
|
177
|
+
- MIT
|
178
|
+
metadata: {}
|
179
|
+
post_install_message:
|
180
|
+
rdoc_options: []
|
181
|
+
require_paths:
|
182
|
+
- lib
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
requirements: []
|
194
|
+
rubyforge_project:
|
195
|
+
rubygems_version: 2.2.2
|
196
|
+
signing_key:
|
197
|
+
specification_version: 4
|
198
|
+
summary: Forms for the rest of us, the lazy.
|
199
|
+
test_files:
|
200
|
+
- test/builder_test.rb
|
201
|
+
- test/cuba_test.rb
|
202
|
+
- test/fixtures/_form.erb
|
203
|
+
- test/helper.rb
|
204
|
+
- test/hobbit_test.rb
|
205
|
+
- test/sinatra_test.rb
|
206
|
+
- test/tag_test.rb
|