Class: Bureaucrat::Forms::Form
- Inherits:
-
Object
- Object
- Bureaucrat::Forms::Form
show all
- Includes:
- Utils, Validation
- Defined in:
- lib/bureaucrat/forms.rb
Constant Summary
Constants included
from Utils
Utils::ESCAPES
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utils
conditional_escape, escape, flatatt, format_string, make_bool, make_float, mark_safe, pretty_name, security_hash
Constructor Details
#initialize(data = nil, options = {}) ⇒ Form
Returns a new instance of Form.
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/bureaucrat/forms.rb', line 117
def initialize(data=nil, options={})
@is_bound = !data.nil?
@data = {}
data.each {|k, v| @data[k.to_sym] = @data[k] = v} if data
@files = options.fetch(:files, {})
@auto_id = options.fetch(:auto_id, 'id_%s')
@prefix = options[:prefix]
@initial = options.fetch(:initial, {})
@error_class = options.fetch(:error_class, Fields::ErrorList)
@label_suffix = options.fetch(:label_suffix, ':')
@empty_permitted = options.fetch(:empty_permitted, false)
@errors = nil
@changed_data = nil
@fields = self.class.base_fields.dup
@fields.each { |key, value| @fields[key] = value.dup }
end
|
Class Attribute Details
.base_fields ⇒ Object
98
99
100
|
# File 'lib/bureaucrat/forms.rb', line 98
def base_fields
@base_fields ||= Utils::OrderedHash.new
end
|
Instance Attribute Details
#auto_id ⇒ Object
Returns the value of attribute auto_id.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def auto_id
@auto_id
end
|
#cleaned_data ⇒ Object
Returns the value of attribute cleaned_data.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def cleaned_data
@cleaned_data
end
|
#data ⇒ Object
Returns the value of attribute data.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def data
@data
end
|
#error_class ⇒ Object
Returns the value of attribute error_class.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def error_class
@error_class
end
|
#files ⇒ Object
Returns the value of attribute files.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def files
@files
end
|
#initial ⇒ Object
Returns the value of attribute initial.
113
114
115
|
# File 'lib/bureaucrat/forms.rb', line 113
def initial
@initial
end
|
Class Method Details
.field(name, field_obj) ⇒ Object
102
103
104
|
# File 'lib/bureaucrat/forms.rb', line 102
def field(name, field_obj)
base_fields[name] = field_obj
end
|
.inherited(c) ⇒ Object
Copy data to the child class
107
108
109
110
|
# File 'lib/bureaucrat/forms.rb', line 107
def inherited(c)
super(c)
c.base_fields = base_fields.dup
end
|
Instance Method Details
#[](name) ⇒ Object
139
140
141
142
|
# File 'lib/bureaucrat/forms.rb', line 139
def [](name)
field = @fields[name] or return nil
BoundField.new(self, field, name)
end
|
#add_initial_prefix(field_name) ⇒ Object
157
158
159
|
# File 'lib/bureaucrat/forms.rb', line 157
def add_initial_prefix(field_name)
"initial-#{add_prefix(field_name)}"
end
|
#add_prefix(field_name) ⇒ Object
153
154
155
|
# File 'lib/bureaucrat/forms.rb', line 153
def add_prefix(field_name)
@prefix ? :"#{@prefix}-#{field_name}" : field_name
end
|
#as_p ⇒ Object
176
177
178
179
|
# File 'lib/bureaucrat/forms.rb', line 176
def as_p
html_output('<p>%(label)s %(field)s%(help_text)s</p>',
'%s', '</p>', ' %s', true)
end
|
#as_table ⇒ Object
165
166
167
168
169
|
# File 'lib/bureaucrat/forms.rb', line 165
def as_table
html_output('<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
'<tr><td colspan="2">%s</td></tr>', '</td></tr>',
'<br />%s', false)
end
|
#as_ul ⇒ Object
171
172
173
174
|
# File 'lib/bureaucrat/forms.rb', line 171
def as_ul
html_output('<li>%(errors)s%(label)s %(field)s%(help_text)s</li>',
'<li>%s</li>', '</li>', ' %s', false)
end
|
#bound? ⇒ Boolean
115
|
# File 'lib/bureaucrat/forms.rb', line 115
def bound? ; @is_bound; end
|
#changed? ⇒ Boolean
226
227
228
|
# File 'lib/bureaucrat/forms.rb', line 226
def changed?
changed_data && !changed_data.empty?
end
|
#changed_data ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/bureaucrat/forms.rb', line 230
def changed_data
if @changed_data.nil?
@changed_data = []
@fields.each do |name, field|
prefixed_name = add_prefix(name)
data_value = field.widget.value_from_datahash(@data, @files,
prefixed_name)
if !field.show_hidden_initial
initial_value = @initial.fetch(name, field.initial)
else
initial_prefixed_name = add_initial_prefix(name)
hidden_widget = field.hidden_widget.new
initial_value = hidden_widget.value_from_datahash(@data, @files,
initial_prefixed_name)
end
@changed_data << name if
field.widget.has_changed?(initial_value, data_value)
end
end
@changed_data
end
|
#clean ⇒ Object
222
223
224
|
# File 'lib/bureaucrat/forms.rb', line 222
def clean
@cleaned_data
end
|
#empty_permitted? ⇒ Boolean
161
162
163
|
# File 'lib/bureaucrat/forms.rb', line 161
def empty_permitted?
@empty_permitted
end
|
#errors ⇒ Object
144
145
146
147
|
# File 'lib/bureaucrat/forms.rb', line 144
def errors
full_clean if @errors.nil?
@errors
end
|
#full_clean ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/bureaucrat/forms.rb', line 185
def full_clean
@errors = Fields::ErrorHash.new
return unless bound?
@cleaned_data = {}
return if empty_permitted? && !changed?
@fields.each do |name, field|
value = field.widget.value_from_datahash(@data, @files,
add_prefix(name))
begin
if field.is_a?(Fields::FileField)
initial = @initial.fetch(name, field.initial)
@cleaned_data[name] = field.clean(value, initial)
else
@cleaned_data[name] = field.clean(value)
end
clean_method = 'clean_%s' % name
@cleaned_data[name] = send(clean_method) if respond_to?(clean_method)
rescue Fields::FieldValidationError => e
@errors[name] = e.messages
@cleaned_data.clear
end
end
begin
@cleaned_data = clean
rescue Fields::FieldValidationError => e
@errors[:__NON_FIELD_ERRORS] = e.messages
end
@cleaned_data = nil if @errors && !@errors.empty?
end
|
#hidden_fields ⇒ Object
265
266
267
|
# File 'lib/bureaucrat/forms.rb', line 265
def hidden_fields
@fields.select {|f| f.hidden?}
end
|
255
256
257
258
259
|
# File 'lib/bureaucrat/forms.rb', line 255
def media
@fields.values.inject(Widgets::Media.new) do |media, field|
media + field.widget.media
end
end
|
#multipart? ⇒ Boolean
261
262
263
|
# File 'lib/bureaucrat/forms.rb', line 261
def multipart?
@fields.any? {|f| f.widgetneeds_multipart_form?}
end
|
#non_field_errors ⇒ Object
181
182
183
|
# File 'lib/bureaucrat/forms.rb', line 181
def non_field_errors
errors.fetch(:__NON_FIELD_ERRORS, @error_class.new)
end
|
#to_s ⇒ Object
135
136
137
|
# File 'lib/bureaucrat/forms.rb', line 135
def to_s
as_table
end
|
#valid? ⇒ Boolean
149
150
151
|
# File 'lib/bureaucrat/forms.rb', line 149
def valid?
@is_bound && (errors.nil? || errors.empty?)
end
|
#visible_fields ⇒ Object
269
270
271
|
# File 'lib/bureaucrat/forms.rb', line 269
def visible_fields
@fields.select {|f| !f.hidden?}
end
|