5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/neewom/repository.rb', line 5
def store!(abstract_form)
form_record = Neewom::CustomForm.find_by key: abstract_form.id
current_crc = calculate_crc32(abstract_form)
return if form_record.present? && form_record.crc32 == current_crc
form_record ||= Neewom::CustomForm.new(key: abstract_form.id)
form_record.transaction do
form_record.assign_attributes(
repository_klass: abstract_form.repository_klass,
template: abstract_form.template,
crc32: current_crc,
persist_submit_controls: (abstract_form.persist_submit_controls || false)
)
existing_fields = form_record.custom_fields.to_a
abstract_form.fields.each_with_index do |field, index|
if field.collection.present?
raise "Form with specified collection could not be stored."
end
field_record = existing_fields.find { |item| item.name.to_s == field.name.to_s }
field_record ||= Neewom::CustomField.new(custom_form: form_record, name: field.name)
field_record.update!(
order: index,
label: field.label,
input: field.input,
virtual: field.virtual,
validations: field.validations.to_json,
collection_klass: field.collection_klass,
collection_method: field.collection_method,
collection_params: field.collection_params.to_json,
label_method: field.label_method,
value_method: field.value_method,
input_html: field.input_html.to_json,
custom_options: field.custom_options.to_json
)
form_record.custom_fields << field_record
end
removable_fields = existing_fields.select { |item| abstract_form.fields.none? { |f| f.name.to_s == item.name.to_s } }
removable_fields.each(&:destroy)
form_record.save!
end
form_record
end
|