radiant-forms-extension 2.0.1 → 3.1.1

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.
@@ -1,23 +0,0 @@
1
- module Forms
2
- module AdminUI
3
-
4
- def self.included(base)
5
- base.class_eval do
6
- attr_accessor :form
7
- alias_method :forms, :form
8
- protected
9
- def load_default_form_regions
10
- returning OpenStruct.new do |form|
11
- form.edit = Radiant::AdminUI::RegionSet.new do |edit|
12
- edit.main.concat %w{edit_header edit_form}
13
- edit.form.concat %w{edit_title edit_content}
14
- edit.form_bottom.concat %w{edit_buttons edit_timestamp}
15
- end
16
- form.new = form.edit
17
- end
18
- end
19
- end
20
- end
21
-
22
- end
23
- end
@@ -1,20 +0,0 @@
1
- module Forms
2
- module ApplicationControllerExtensions
3
- def self.included(base)
4
- base.class_eval {
5
- helper_method :current_response
6
-
7
- def current_response
8
- if request.session[:form_response]
9
- @response = Response.find(request.session[:form_response])
10
- else
11
- @response = Response.create
12
- request.session = @response.id
13
- end
14
-
15
- @response
16
- end
17
- }
18
- end
19
- end
20
- end
@@ -1,6 +0,0 @@
1
- module Forms
2
- module PageExtensions
3
- attr_accessor :last_form
4
- attr_accessor :data
5
- end
6
- end
@@ -1,19 +0,0 @@
1
- module Forms
2
- module SiteControllerExtensions
3
- def self.included(base)
4
- base.class_eval {
5
- before_filter :find_response
6
-
7
- def find_response
8
- if request.session[:form_response]
9
- @response = Response.find(request.session[:form_response])
10
- else
11
- @response = nil
12
- end
13
-
14
- @response
15
- end
16
- }
17
- end
18
- end
19
- end
data/lib/forms/tags.rb DELETED
@@ -1,277 +0,0 @@
1
- module Forms
2
- module Tags
3
- include Radiant::Taggable
4
-
5
- class TagError < StandardError; end
6
-
7
- desc %{
8
- Render a form and its contents
9
- <pre><code><r:form name="object[key]" [method, url] /></code></pre>
10
-
11
- * *name* the name of the form
12
- * *method* url submission method [ post, get, delete, put ]
13
- * *url* address to submit the form to
14
- }
15
- tag 'form' do |tag|
16
-
17
- unless tag.attr['name'].blank?
18
- if tag.locals.form = Form.find_by_title(tag.attr['name'])
19
- tag.attr['id'] ||= 'form_' + tag.locals.form.title
20
- tag.attr['method'] ||= 'post'
21
- tag.attr['action'] ||= tag.locals.form.action.blank? ? "/forms/#{tag.locals.form.id}" : tag.locals.form.action
22
- return render_form(tag.locals.form, tag)
23
- else
24
- raise TagError, "Could not find '#{tag.attr['name']}' form"
25
- end
26
- else
27
- tag.expand
28
- end
29
-
30
- end
31
-
32
- desc %{
33
- Render a @<label />@ tag to be used inside a form
34
- <pre><code><r:form:label for="object[key]" /></code></pre>
35
-
36
- **Required**
37
- * *for* the name of the field you are relating the tag to
38
-
39
- _A label is a piece of text which activates its related input when clicked_
40
- }
41
- tag 'form:label' do |tag|
42
- raise_error_if_missing 'form:label', tag.attr, 'for'
43
-
44
- tag.attr['for'] = tag.attr['for'].gsub('[','_').gsub(']','') unless tag.attr['for'].nil?
45
-
46
- %(<label #{form_attrs(tag)}>#{tag.expand}</label>)
47
- end
48
-
49
- %w(text password reset checkbox radio hidden file button).each do |type|
50
- desc %{
51
- Render a @<#{type}>...</#{type}>@ tag to be used in a form
52
- <pre><code><r:form:#{type} name="object[key]" /></code></pre>
53
-
54
- **Required**
55
- * *name* the name of the data to be sent
56
-
57
- **Optional**
58
- * *class* css class names
59
- * *placeholder* default text, which is cleared when clicked
60
- * *maxlength* the maximum amount of characters
61
- }
62
- tag "form:#{type}" do |tag|
63
- raise_error_if_missing "form:#{type}", tag.attr, 'name'
64
-
65
- value = tag.attr['value']
66
- tag.attr['type'] = type
67
-
68
- %(<input type="#{type}" value="#{value}" #{form_attrs(tag)} />)
69
- end
70
- end
71
-
72
- desc %{
73
- Renders a @<select>...</select>@ tag to be used in a form
74
-
75
- <pre><code><r:form:select name="object[key]"><r:option value="text" /></r:form:select></code></pre>
76
-
77
- **Required**
78
- * *name* the name of the data to be sent
79
-
80
- **Optional**
81
- * *class* css class names
82
-
83
- _@<r:option />@ tags may be nested inside the tag to automatically generate options_
84
- }
85
- tag 'form:select' do |tag|
86
- raise_error_if_missing 'form:select', tag.attr, 'name'
87
-
88
- tag.locals.parent_tag_name = tag.attr['name']
89
- tag.locals.parent_tag_type = 'select'
90
-
91
- %(<select #{form_attrs(tag)}>#{tag.expand}</select>)
92
- end
93
-
94
- desc %{
95
- Renders a series of @<input type="radio"/>@ tags to be used in a form
96
-
97
- <pre><code><r:form:radios name="object[value]" value="text"><r:option value="text" /></r:form:radios></code></pre>
98
-
99
- **Required**
100
- * *name* the name of the data to be sent
101
-
102
- **Optional**
103
- * *class* css class names
104
-
105
- _@<r:option />@ tags may be nested inside the tag to automatically generate options_
106
- }
107
- tag 'form:radios' do |tag|
108
- raise_error_if_missing 'form:radios', tag.attr, 'name'
109
-
110
- tag.locals.parent_tag_name = tag.attr['name']
111
- tag.locals.parent_tag_type = 'radios'
112
- tag.expand
113
- end
114
-
115
- desc %{
116
- Renders an @<option/>@ tag if the parent is a
117
- @<r:form:select>...</r:form:select>@ or @<r:form:radios>...</r:form:radios>@
118
-
119
- **Required**
120
- * *value* the value you want to be sent if selected
121
- }
122
- tag 'form:option' do |tag|
123
- value = tag.attr['value'] || tag.expand
124
-
125
- if tag.locals.parent_tag_type == 'select'
126
- text = tag.expand.length == 0 ? value : tag.expand
127
- %(<option #{form_attrs(tag, { 'value' => value})}>#{text}</option>)
128
- elsif tag.locals.parent_tag_type == 'radios'
129
- name = tag.locals.parent_tag_type == 'radios' ? tag.locals.parent_tag_name : tag.attr['name']
130
- id = name.gsub('[','_').gsub(']','') + '_' + value
131
- %(<input #{form_attrs(tag, { 'id' => id, 'value' => value, 'name' => name, 'type' => 'radio' })} />)
132
- end
133
- end
134
-
135
- desc %{
136
- Renders an @<input type="submit" />@ tag for a form.
137
-
138
- **Optional**
139
- * *value* the text on the submit
140
- }
141
- tag 'form:submit' do |tag|
142
- value = tag.attr['value'] || 'submit'
143
-
144
- %(<input #{form_attrs(tag, { 'value' => value, 'type' => 'submit', 'class' => 'submit', 'name' => 'submit' })} />)
145
- end
146
-
147
- desc %{
148
- Allows access to the response of a submitted form
149
- @<r:form:response>...<r:get /><r:clear />...</r:form:response>@
150
-
151
- _Will not expand if a form has not been submitted or has been cleared_
152
- }
153
- tag 'form:response' do |tag|
154
- tag.locals.form_response = find_form_response(tag)
155
-
156
- tag.expand unless tag.locals.form_response.nil? or tag.locals.form_response.response.nil?
157
- end
158
-
159
- desc %{
160
- Access the attributes of a submitted form.
161
-
162
- @<r:form:get name=object[value]
163
- }
164
- tag 'form:response:clear' do |tag|
165
- clear_form_response(tag)
166
-
167
- return nil
168
- end
169
-
170
- desc %{
171
- Access the attributes of a submitted form.
172
-
173
- @<r:form:get name="object[value]" />@ an object which was submitted with the form
174
- @<r:form:get name="mail_ext[value]" />@ a response to an extension which hooked the submission
175
-
176
- _Refer to the readme on extensions to find out what they return_
177
- }
178
- tag 'form:response:get' do |tag|
179
- raise_error_if_missing 'form:response:get', tag.attr, 'name'
180
-
181
- results = hash_retrieve(tag.locals.form_response.result, tag.attr['name'])
182
- end
183
-
184
- # accessing data when processing a form
185
- desc %{
186
- Query the data from a submitted form
187
-
188
- @<r:form:read name="object[value]" />@ access the object which was submitted
189
-
190
- _This can only be used in the content section of a form, use @<r:form:get />@ in pages_
191
- }
192
- tag 'form:read' do |tag|
193
- raise_error_if_missing 'form:read', tag.attr, 'name'
194
-
195
- data = hash_retrieve(tag.locals.page.data, tag.attr['name']).to_s
196
- end
197
-
198
- protected
199
-
200
- def form_attrs(tag, extras={})
201
- @id = tag.attr['name'] || extras['name']
202
- @id = @id || tag.attr['for'] + '_label' if tag.attr['for']
203
- @id = @id.gsub('[','_').gsub(']','') unless @id.nil?
204
-
205
- attrs = {
206
- 'id' => @id,
207
- 'class' => tag.attr['type'],
208
- 'name' => nil,
209
- 'for' => nil,
210
- 'method' => nil,
211
- 'action' => nil,
212
- 'placeholder' => nil,
213
- 'value' => nil,
214
- 'maxlength' => nil
215
- }.merge(extras)
216
-
217
- result = attrs.collect do |k,v|
218
- v = (tag.attr[k] || v)
219
- next if v.blank?
220
- %(#{k}="#{v}")
221
- end.reject{|e| e.blank?}
222
-
223
- result.join(' ')
224
- end
225
-
226
- def prior_value(tag, tag_name=tag.attr['name'])
227
- #TODO make work with hash_retrieve
228
- end
229
-
230
- def render_form(form, tag)
231
- string = %(<form enctype="multipart/form-data" #{form_attrs(tag)}>\n)
232
- string << %(<input type="hidden" name="page_id" value="#{tag.locals.page.id}" />\n)
233
- string << %(<input type="hidden" name="form_id" value="#{tag.locals.form.id.to_s}" />\n)
234
- string << "<r:form>"
235
- string << form.body
236
- string << "</r:form>"
237
- string << %(</form>)
238
-
239
- text = parse(string)
240
- end
241
-
242
- def raise_error_if_missing(tag_name, tag_attr, name)
243
- raise "'#{tag_name}' tag requires a '#{name}' attribute" if tag_attr[name].blank?
244
- end
245
-
246
- def find_form_response(tag)
247
- if tag.locals.form_response
248
- tag.locals.form_response
249
- elsif request.session[:form_response]
250
- Response.find(request.session[:form_response])
251
- else
252
- nil
253
- end
254
- end
255
-
256
- def clear_form_response(tag)
257
- tag.locals.form_response.update_attribute('result', nil)
258
- end
259
-
260
- # takes object[key] or value
261
- # Accesses the hash as hash[object][value]
262
- # Accesses the value as hash[value]
263
- def hash_retrieve(hash, array)
264
- result = nil
265
-
266
- unless hash.nil? or hash.empty?
267
- data = array.gsub("[","|").gsub("]","").split("|") rescue nil
268
-
269
- result = hash.fetch(data[0]) unless data.nil?
270
- result = result.fetch(data[1]) unless data.nil? or data[1].nil?
271
- end
272
-
273
- result
274
- end
275
-
276
- end
277
- end
@@ -1,41 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- describe Forms::AddonMethods do
4
- dataset :forms, :pages
5
-
6
- before :each do
7
- @page = pages(:home)
8
- @form = forms(:test)
9
- end
10
-
11
- context 'initializer' do
12
-
13
- it 'should expect a form and page object' do
14
- lambda { FormsTestController.new(@form, @page) }.should_not raise_error
15
- end
16
-
17
- end
18
-
19
- context 'instance variables' do
20
-
21
- before :each do
22
- @forms_test = FormsTestController.new(@form, @page)
23
- end
24
-
25
- it 'should have a forms attribute' do
26
- @forms_test.form.should be_an_instance_of(Form)
27
- @forms_test.form.should == @form
28
- end
29
-
30
- it 'should have a page attribute' do
31
- @forms_test.page.should be_an_instance_of(Page)
32
- @forms_test.page.should == @page
33
- end
34
-
35
- it 'should have a data attribute' do
36
- @forms_test.data.should == @page.data
37
- end
38
-
39
- end
40
-
41
- end