Class: Bureaucrat::Formsets::BaseFormSet
- Inherits:
-
Object
- Object
- Bureaucrat::Formsets::BaseFormSet
show all
- Includes:
- Bureaucrat::Fields, Bureaucrat::Forms, Utils
- Defined in:
- lib/bureaucrat/formsets.rb
Constant Summary
Constant Summary
Constants included
from Utils
Utils::ESCAPES
Instance Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Methods included from Utils
#blank_value?, #conditional_escape, #escape, #flatatt, #format_string, #make_bool, #make_float, #mark_safe, #pretty_name
Constructor Details
- (BaseFormSet) initialize(data = nil, options = {})
A new instance of BaseFormSet
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/bureaucrat/formsets.rb', line 30
def initialize(data=nil, options={})
set_defaults
@is_bound = !data.nil?
@prefix = options.fetch(:prefix, self.class.default_prefix)
@auto_id = options.fetch(:auto_id, 'id_%s')
@data = data || {}
@initial = options[:initial]
@error_class = options.fetch(:error_class, ErrorList)
@errors = nil
@non_form_errors = nil
construct_forms
end
|
Instance Attribute Details
- (Object) can_delete
Returns the value of attribute can_delete
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def can_delete
@can_delete
end
|
- (Object) can_order
Returns the value of attribute can_order
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def can_order
@can_order
end
|
Returns the value of attribute extra
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def
@extra
end
|
Returns the value of attribute form
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def form
@form
end
|
Returns the value of attribute forms
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def forms
@forms
end
|
- (Object) max_num
Returns the value of attribute max_num
28
29
30
|
# File 'lib/bureaucrat/formsets.rb', line 28
def max_num
@max_num
end
|
Class Method Details
+ (Object) default_prefix
24
25
26
|
# File 'lib/bureaucrat/formsets.rb', line 24
def self.default_prefix
'form'
end
|
Instance Method Details
- (Object) [](index)
48
49
50
|
# File 'lib/bureaucrat/formsets.rb', line 48
def [](index)
forms[index]
end
|
- (Object) add_fields(form, index)
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/bureaucrat/formsets.rb', line 229
def add_fields(form, index)
if can_order
attrs = {:label => 'Order', :required => false}
attrs[:initial] = index + 1 if index && index < initial_form_count
form.fields[ORDERING_FIELD_NAME] = IntegerField.new(attrs)
end
if can_delete
field = BooleanField.new(:label => 'Delete', :required => false)
form.fields[DELETION_FIELD_NAME] = field
end
end
|
- (Object) add_prefix(index)
241
242
243
|
# File 'lib/bureaucrat/formsets.rb', line 241
def add_prefix(index)
'%s-%s' % [@prefix, index]
end
|
- (Object) clean
226
227
|
# File 'lib/bureaucrat/formsets.rb', line 226
def clean
end
|
- (Object) cleaned_data
Maybe this should just go away?
132
133
134
135
136
137
|
# File 'lib/bureaucrat/formsets.rb', line 132
def cleaned_data
unless valid?
raise NoMethodError.new("'#{self.class.name}' object has no method 'cleaned_data'")
end
@forms.collect(&:cleaned_data)
end
|
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/bureaucrat/formsets.rb', line 109
def construct_form(i, options={})
defaults = {:auto_id => @auto_id, :prefix => add_prefix(i)}
defaults[:initial] = @initial[i] if @initial && @initial[i]
defaults[:empty_permitted] = true if i >= initial_form_count
defaults.merge!(options)
form_data = @is_bound ? @data : nil
form = self.form.new(form_data, defaults)
add_fields(form, i)
form
end
|
105
106
107
|
# File 'lib/bureaucrat/formsets.rb', line 105
def construct_forms
@forms = (0...total_form_count).map { |i| construct_form(i) }
end
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/bureaucrat/formsets.rb', line 139
def deleted_forms
unless valid? && self.can_delete
raise NoMethodError.new("'#{self.class.name}' object has no method 'deleted_forms'")
end
if @deleted_form_indexes.nil?
@deleted_form_indexes = (0...total_form_count).select do |i|
form = @forms[i]
if i >= initial_form_count && !form.changed?
false
else
should_delete_form?(form)
end
end
end
@deleted_form_indexes.map {|i| @forms[i]}
end
|
- (Object) each(&block)
44
45
46
|
# File 'lib/bureaucrat/formsets.rb', line 44
def each(&block)
forms.each(&block)
end
|
- (Object) errors
186
187
188
189
|
# File 'lib/bureaucrat/formsets.rb', line 186
def errors
full_clean if @errors.nil?
@errors
end
|
126
127
128
129
|
# File 'lib/bureaucrat/formsets.rb', line 126
def
n = initial_form_count
@forms[n, @forms.length - n]
end
|
- (Object) full_clean
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/bureaucrat/formsets.rb', line 212
def full_clean
if @is_bound
@errors = @forms.collect(&:errors)
begin
self.clean
rescue ValidationError => e
@non_form_errors = @error_class.new(e.messages)
end
else
@errors = []
end
end
|
95
96
97
98
99
100
101
102
103
|
# File 'lib/bureaucrat/formsets.rb', line 95
def initial_form_count
if @is_bound
management_form.cleaned_data[INITIAL_FORM_COUNT]
else
n = @initial ? @initial.length : 0
(self.max_num > 0 && n > self.max_num) ? self.max_num : n
end
end
|
122
123
124
|
# File 'lib/bureaucrat/formsets.rb', line 122
def initial_forms
@forms[0, initial_form_count]
end
|
- (Object) length
52
53
54
|
# File 'lib/bureaucrat/formsets.rb', line 52
def length
forms.length
end
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/bureaucrat/formsets.rb', line 56
def management_form
if @is_bound
form = ManagementForm.new(@data, :auto_id => @auto_id,
:prefix => @prefix)
unless form.valid?
msg = 'ManagementForm data is missing or has been tampered with'
raise ValidationError.new(msg)
end
else
form = ManagementForm.new(nil, :auto_id => @auto_id,
:prefix => @prefix,
:initial => {
TOTAL_FORM_COUNT => total_form_count,
INITIAL_FORM_COUNT => initial_form_count,
MAX_NUM_FORM_COUNT => self.max_num
})
end
form
end
|
- (Boolean) multipart?
245
246
247
|
# File 'lib/bureaucrat/formsets.rb', line 245
def multipart?
@forms && @forms.first.multipart?
end
|
182
183
184
|
# File 'lib/bureaucrat/formsets.rb', line 182
def non_form_errors
@non_form_errors || @error_class.new
end
|
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/bureaucrat/formsets.rb', line 159
def ordered_forms
unless valid? && self.can_order
raise NoMethodError.new("'#{self.class.name}' object has no method 'ordered_forms'")
end
if @ordering.nil?
@ordering = (0...total_form_count).map do |i|
form = @forms[i]
next if i >= initial_form_count && !form.changed?
next if self.can_delete && should_delete_form?(form)
[i, form.cleaned_data[ORDERING_FIELD_NAME]]
end.compact
@ordering.sort! do |a, b|
if x[1].nil? then 1
elsif y[1].nil? then -1
else x[1] - y[1]
end
end
end
@ordering.map {|i| @forms[i.first]}
end
|
191
192
193
194
195
|
# File 'lib/bureaucrat/formsets.rb', line 191
def should_delete_form?(form)
field = form.fields[DELETION_FIELD_NAME]
raw_value = form.send(:raw_value, DELETION_FIELD_NAME)
field.clean(raw_value)
end
|
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/bureaucrat/formsets.rb', line 76
def total_form_count
if @is_bound
management_form.cleaned_data[TOTAL_FORM_COUNT]
else
initial_forms = initial_form_count
total_forms = initial_form_count + self.
if self.max_num > 0 && initial_forms > self.max_num
initial_forms
elsif self.max_num > 0 && total_forms > self.max_num
max_num
else
total_forms
end
end
end
|
- (Boolean) valid?
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/bureaucrat/formsets.rb', line 197
def valid?
return false unless @is_bound
forms_valid = true
(0...total_form_count).each do |i|
form = @forms[i]
next if self.can_delete && should_delete_form?(form)
forms_valid = false unless errors[i].empty?
end
forms_valid && non_form_errors.empty?
end
|