-
Notifications
You must be signed in to change notification settings - Fork 8
/
construct_controller.rb
executable file
·53 lines (50 loc) · 1.8 KB
/
construct_controller.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
class ConstructController < BasicInstrumentController
def create
begin
@object = collection.create(safe_params)
if @object
render :show, status: :created
else
render json: @object.errors, status: :unprocessable_entity
end
rescue => e
response = {error: e}
render json: response, status: :internal_server_error
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def safe_params
obj_name = self.class.model_class.name.underscore.to_sym
params = wrap_parent_param(self.params, obj_name)
params[obj_name] = shim_construct_type(params[obj_name])
params.require( obj_name )
.permit( [:label, :parent_id, :parent_type, :position, :branch, :instrument_id] + self.class.params_list )
end
private
def shim_construct_type(p)
parental_classes = %w(CcCondition CcLoop CcQuestion CcSequence CcStatement)
unless p[:parent_type].nil? || parental_classes.include?(p[:parent_type])
p[:parent_type] = case p[:parent_type].downcase
when 'condition' then
'CcCondition'
when 'loop' then
'CcLoop'
when 'question' then
'CcQuestion'
when 'sequence' then
'CcSequence'
when 'statement' then
'CcStatement'
end
end
p
end
def wrap_parent_param(p, obj_name)
parent_object = (p.has_key?(obj_name)) ? p[obj_name].delete(:parent) : p.delete(:parent)
if parent_object
p[obj_name][:parent_id] = parent_object[:id]
p[obj_name][:parent_type] = parent_object[:type]
end
p
end
end