visionmedia-dm-forms 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +0,0 @@
1
-
2
- module DataMapper
3
- module Form
4
- module ModelElements
5
-
6
- include Elements
7
-
8
- ##
9
- # Generates a form.
10
-
11
- def form_for model, options = {}, &block
12
- id = model.class.to_s.downcase
13
- method = model.new_record? ? :post : :put
14
- options = { :model => model, :method => method }.merge options
15
- form id, options, &block
16
- end
17
-
18
- ##
19
- # Return markup for errors on +model+.
20
-
21
- def errors_for model
22
- if not model.all_valid?
23
- s = %(<ul class="messages error">\n)
24
- s << model.errors.collect { |error| "<li>#{error.first}</li>" }.join("\n")
25
- s << "\n</ul>\n"
26
- else
27
- ''
28
- end
29
- end
30
-
31
- end
32
- end
33
- end
@@ -1,56 +0,0 @@
1
-
2
-
3
- describe "core ext" do
4
-
5
- #--
6
- # Humanize
7
- #++
8
-
9
- describe "String#humanize" do
10
-
11
- it "should humanize strings" do
12
- 'im_a_Lame_String.Yeah.with.7.words'.humanize.should == 'im a Lame String Yeah with 7 words'
13
- end
14
-
15
- it "should humanize symbols" do
16
- :some_Cool_symbol.humanize.should == 'some Cool symbol'
17
- :login.humanize.should == 'login'
18
- end
19
-
20
- it "should not mess with already human readable strings" do
21
- 'i am already VERY readable'.humanize.should == 'i am already VERY readable'
22
- end
23
-
24
- end
25
-
26
- #--
27
- # Indent
28
- #++
29
-
30
- describe "String#indent" do
31
-
32
- it "should indent 2 spaces by default" do
33
- 'meow'.indent.should == ' meow'
34
- end
35
-
36
- it "should indent variable tab lengths" do
37
- 'meow'.indent(2).should == ' meow'
38
- end
39
-
40
- it "should indent multi-line strings" do
41
- string = <<-EOF.deindent
42
- foo
43
- bar
44
- EOF
45
- string.indent.should == <<-EOF.deindent
46
- foo
47
- bar
48
- EOF
49
- string.indent(2).should == <<-EOF.deindent
50
- foo
51
- bar
52
- EOF
53
- end
54
-
55
- end
56
- end
@@ -1,209 +0,0 @@
1
-
2
- include DataMapper::Form::Elements
3
-
4
- describe DataMapper::Form::Elements do
5
-
6
- #--
7
- # Element aspecs
8
- #++
9
-
10
- describe "element aspecs" do
11
-
12
- it "should allow descriptions" do
13
- s = textarea :comments, :description => 'Please enter your comments.'
14
- s.should == <<-HTML.deindent(8)
15
- <textarea class="form-textarea form-comments" name="comments"></textarea>
16
- <p class="description">Please enter your comments.</p>
17
- HTML
18
- end
19
-
20
- it "should allow prefixing of arbitrary markup" do
21
- s = textarea :comments, :before => "<h1>Comments</h1>\n"
22
- s.should == <<-HTML.deindent(8)
23
- <h1>Comments</h1>
24
- <textarea class="form-textarea form-comments" name="comments"></textarea>
25
- HTML
26
- end
27
-
28
- it "should allow arbitrary markup after" do
29
- s = textarea :comments, :after => "\n<p>Custom markup</p>"
30
- s.should == <<-HTML.deindent(8)
31
- <textarea class="form-textarea form-comments" name="comments"></textarea>
32
- <p>Custom markup</p>
33
- HTML
34
- end
35
-
36
- end
37
-
38
- #--
39
- # Elements
40
- #++
41
-
42
- describe "elements" do
43
-
44
- it "should create labels" do
45
- s = label 'Email', :for => :email
46
- s.should == %(<label for="email">Email:</label>\n)
47
- end
48
-
49
- it "should create required labels" do
50
- s = label 'Email', :for => :email, :required => true
51
- s.should == %(<label for="email">Email:<em>*</em></label>\n)
52
- end
53
-
54
- it "should create textfields" do
55
- s = textfield :phone, :value => 'Enter phone number'
56
- s.should == %(<input type="textfield" class="form-textfield form-phone" value="Enter phone number" name="phone" />\n)
57
- end
58
-
59
- it "should create submit buttons" do
60
- s = submit :op, :value => 'Submit'
61
- s.should == %(<input type="submit" class="form-submit form-op" value="Submit" name="op" />\n)
62
- end
63
-
64
- it "should create buttons" do
65
- s = button :op, :value => 'Edit'
66
- s.should == %(<input type="button" class="form-button form-op" value="Edit" name="op" />\n)
67
- end
68
-
69
- it "should create image buttons" do
70
- s = button :op, :value => 'Edit', :src => 'path/to/image.png'
71
- s.should == %(<input type="image" class="form-image form-op" value="Edit" name="op" src="path/to/image.png" />\n)
72
- end
73
-
74
- it "should create a checkbox" do
75
- s = checkbox :agree_to_terms, :value => 'Agree to terms', :checked => true
76
- s.should == %(<input type="checkbox" class="form-checkbox form-agree_to_terms" value="Agree to terms" name="agree_to_terms" checked="checked" />\n)
77
- end
78
-
79
- it "should create a file field" do
80
- s = file :image
81
- s.should == %(<input type="file" class="form-file form-image" name="image" />\n)
82
- end
83
-
84
- it "should create hidden fields" do
85
- s = hidden :_method, :value => 'put'
86
- s.should == %(<input type="hidden" value="put" name="_method" />\n)
87
- end
88
-
89
- it "should create a radio button" do
90
- s = radio :sex, :value => 'Male'
91
- s.should == %(<input type="radio" class="form-radio form-sex" value="Male" name="sex" />\n)
92
- end
93
-
94
- it "should create textareas" do
95
- s = textarea :comments, :value => 'Enter your comments here', :label => 'Comments'
96
- s.should == <<-HTML.deindent(8)
97
- <label for="comments">Comments:</label>
98
- <textarea class="form-textarea form-comments" name="comments">Enter your comments here</textarea>
99
- HTML
100
- end
101
-
102
- it "should create fieldsets" do
103
- s = fieldset :details, :legend => 'Details', :value => 'WAHOO!'
104
- s.should == <<-HTML.deindent(8)
105
- <fieldset class="fieldset-details">
106
- <legend>Details</legend>WAHOO!</fieldset>
107
- HTML
108
- end
109
-
110
- it "should create fieldsets with element capturing block" do
111
- s = fieldset :details, :id => 'details' do |f|
112
- f.button :one
113
- f.button :two
114
- end
115
- s.should == <<-HTML.deindent(8)
116
- <fieldset class="fieldset-details" id="details">
117
- <legend>Details</legend><input type="button" class="form-button form-one" name="one" />
118
- <input type="button" class="form-button form-two" name="two" />
119
- </fieldset>
120
- HTML
121
- end
122
-
123
- it "should create fieldsets without legends, auto-generating them from the fieldset name" do
124
- s = fieldset :some_legend, :value => 'WAHOO!'
125
- s.should == <<-HTML.deindent(8)
126
- <fieldset class="fieldset-some_legend">
127
- <legend>Some legend</legend>WAHOO!</fieldset>
128
- HTML
129
- end
130
-
131
- it "should create select fields" do
132
- s = select :color, :options => { :red => 'Red', :green => 'Green', :blue => 'Blue' }
133
- s.should == <<-HTML.deindent(8)
134
- <select class="form-select form-color" name="color">
135
- <option value="blue">Blue</option>
136
- <option value="red">Red</option>
137
- <option value="green">Green</option>
138
- </select>
139
- HTML
140
- end
141
-
142
- it "should create select fields with block" do
143
- s = select :color do |s|
144
- s.option :red, 'Red'
145
- s.option :green, 'Green'
146
- s.option :blue, 'Blue'
147
- end
148
- s.should == <<-HTML.deindent(8)
149
- <select class="form-select form-color" name="color">
150
- <option value="red">Red</option>
151
- <option value="green">Green</option>
152
- <option value="blue">Blue</option>
153
- </select>
154
- HTML
155
- end
156
-
157
- end
158
-
159
- #--
160
- # Form specifics
161
- #++
162
-
163
- describe "forms" do
164
-
165
- it "should default method to post" do
166
- s = form :register
167
- s.should == %(<form method="post" id="form-register"></form>\n)
168
- end
169
-
170
- it "should allow custom methods" do
171
- s = form :register, :method => :get
172
- s.should == %(<form method="get" id="form-register"></form>\n)
173
- end
174
-
175
- it "should allow arbitrary inner html" do
176
- s = form :register, :method => :get, :value => 'COOKIE!'
177
- s.should == %(<form method="get" id="form-register">COOKIE!</form>\n)
178
- end
179
-
180
- it "should store arbitrary methods in _method" do
181
- s = form :login, :method => :put do |f|
182
- f.submit :op, :value => 'Submit'
183
- end
184
- s.should == <<-HTML.deindent(8)
185
- <form method="post" id="form-login"><input type="hidden" value="put" name="_method" />
186
- <input type="submit" class="form-submit form-op" value="Submit" name="op" />
187
- </form>
188
- HTML
189
- end
190
-
191
- it "should create with a block for inner html" do
192
- s = form :login do |f|
193
- f.textfield :name, :label => 'Username'
194
- f.textfield :pass, :label => 'Password'
195
- f.submit :op, :value => 'Login'
196
- end
197
- s.should == <<-HTML.deindent(8)
198
- <form method="post" id="form-login"><label for="name">Username:</label>
199
- <input type="textfield" class="form-textfield form-name" name="name" />
200
- <label for="pass">Password:</label>
201
- <input type="textfield" class="form-textfield form-pass" name="pass" />
202
- <input type="submit" class="form-submit form-op" value="Login" name="op" />
203
- </form>
204
- HTML
205
- end
206
-
207
- end
208
-
209
- end
@@ -1,29 +0,0 @@
1
-
2
- require 'rubygems'
3
- require 'haml'
4
-
5
- include DataMapper::Form::Elements
6
-
7
- describe "Haml + dm-forms" do
8
-
9
- it "should create pretty forms :D" do
10
- engine = Haml::Engine.new <<-HAML.deindent
11
- .comments
12
- %h1 Comments
13
- = textfield :name
14
- = textfield :email
15
- = textarea :comments
16
- = submit :op, :value => 'Submit'
17
- HAML
18
- engine.render.should == <<-HTML.deindent
19
- <div class='comments'>
20
- <h1>Comments</h1>
21
- <input type="textfield" class="form-textfield form-name" name="name" />
22
- <input type="textfield" class="form-textfield form-email" name="email" />
23
- <textarea class="form-textarea form-comments" name="comments"></textarea>
24
- <input type="submit" class="form-submit form-op" value="Submit" name="op" />
25
- </div>
26
- HTML
27
- end
28
-
29
- end