forked from m-Peter/activeform
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathform_collection.rb
180 lines (145 loc) · 3.79 KB
/
form_collection.rb
1
2
3
4
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module ActionForm
class FormCollection
include ActiveModel::Validations
attr_reader :association_name, :records, :parent, :proc, :forms
def initialize(assoc_name, parent, proc, options)
@association_name = assoc_name
@parent = parent
@proc = proc
@records = options[:records] || 1
@forms = []
assign_forms
end
def update_models
@forms = []
fetch_models
end
def submit(params)
params.each do |key, value|
value = value.to_h if value.is_a?(ActionController::Parameters) && rails_5?
if parent.persisted?
create_or_update_record(value)
else
create_or_assign_record(key, value)
end
end
end
def get_model(assoc_name)
Form.new(association_name, parent, proc)
end
def valid?
aggregate_form_errors
errors.empty?
end
def represents?(assoc_name)
association_name.to_s == assoc_name.to_s
end
def models
forms
end
def each(&block)
forms.each do |form|
block.call(form)
end
end
private
REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
UNASSIGNABLE_KEYS = %w( id _destroy )
def call_reject_if(attributes)
REJECT_ALL_BLANK_PROC.call(attributes)
end
def assign_to_or_mark_for_destruction(form, attributes)
form.submit(attributes.except(*UNASSIGNABLE_KEYS))
if has_destroy_flag?(attributes)
form.delete
remove_form(form)
end
end
def existing_record?(attributes)
attributes[:id] != nil
end
def update_record(attributes)
id = attributes[:id]
form = find_form_by_model_id(id)
assign_to_or_mark_for_destruction(form, attributes)
end
def create_record(attributes)
new_form = create_form
new_form.submit(attributes)
end
def create_or_update_record(attributes)
if existing_record?(attributes)
update_record(attributes)
else
create_record(attributes)
end
end
def create_or_assign_record(key, attributes)
i = key.to_i
if dynamic_key?(i)
create_record(attributes)
else
if call_reject_if(attributes)
forms[i].delete
end
forms[i].submit(attributes)
end
end
def has_destroy_flag?(attributes)
attributes['_destroy'] == "1"
end
def assign_forms
if parent.persisted?
fetch_models
else
initialize_models
end
end
def dynamic_key?(i)
i >= forms.size
end
def aggregate_form_errors
forms.each do |form|
form.valid?
collect_errors_from(form)
end
end
def fetch_models
associated_records = parent.send(association_name)
associated_records.each do |model|
form = Form.new(association_name, parent, proc, model)
forms << form
end
end
def initialize_models
records.times do
form = Form.new(association_name, parent, proc)
forms << form
end
end
def collect_errors_from(model)
model.errors.each do |attribute, error|
errors.add(attribute, error)
end
end
def check_record_limit!(limit, attributes_collection)
if attributes_collection.size > limit
raise TooManyRecords, "Maximum #{limit} records are allowed. Got #{attributes_collection.size} records instead."
end
end
def find_form_by_model_id(id)
forms.select { |form| form.id == id.to_i }.first
end
def remove_form(form)
forms.delete(form)
end
def create_form
new_form = Form.new(association_name, parent, proc)
forms << new_form
new_form
end
def rails_5?
Rails.version[0] == '5'
end
end
end