Class: ForestLiana::ActionsController
Instance Method Summary
collapse
#authenticate_user_from_jwt, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models
#route_not_found
Instance Method Details
#change ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 107
def change
change_request = get_smart_action_hook_request
action = get_action(change_request[:collection_name])
if !action
return render status: 500, json: {error: 'Error in smart action change hook: cannot retrieve action from collection'}
elsif change_request[:fields].nil?
return render status: 500, json: {error: 'Error in smart action change hook: fields params is mandatory'}
elsif !change_request[:fields].is_a?(Array)
return render status: 500, json: {error: 'Error in smart action change hook: fields params must be an array'}
end
context = get_smart_action_change_ctx(change_request[:fields], change_request[:changed_field])
field_changed_hook = context[:field_changed][:hook]
result = action.hooks[:change][field_changed_hook].(context)
handle_result(result, action)
end
|
#get_action(collection_name) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 17
def get_action(collection_name)
collection = get_collection(collection_name)
begin
collection.actions.find {|action| ActiveSupport::Inflector.parameterize(action.name) == params[:action_name]}
rescue => error
FOREST_LOGGER.error "Smart Action get action retrieval error: #{error}"
nil
end
end
|
#get_collection(collection_name) ⇒ Object
13
14
15
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 13
def get_collection(collection_name)
ForestLiana.apimap.find { |collection| collection.name.to_s == collection_name }
end
|
#get_smart_action_change_ctx(fields, field_changed) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 36
def get_smart_action_change_ctx(fields, field_changed)
found_field_changed = fields.find{|field| field[:field] == field_changed}
fields = fields.map do |field|
field = field.permit!.to_h.symbolize_keys
ForestLiana::WidgetsHelper.set_field_widget(field)
field
end
{:field_changed => found_field_changed, :fields => fields, :params => params}
end
|
#get_smart_action_hook_request ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 4
def get_smart_action_hook_request
begin
params[:data][:attributes]
rescue => error
FOREST_LOGGER.error "Smart Action hook request error: #{error}"
{}
end
end
|
#get_smart_action_load_ctx(fields) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 27
def get_smart_action_load_ctx(fields)
fields = fields.map do |field|
ForestLiana::WidgetsHelper.set_field_widget(field)
field[:value] = nil unless field[:value]
field
end
{:fields => fields, :params => params}
end
|
#handle_result(result, action) ⇒ Object
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
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 46
def handle_result(result, action)
if result.nil? || !result.is_a?(Array)
return render status: 500, json: { error: 'Error in smart action load hook: hook must return an array of fields' }
end
begin
change_hooks_name = action.hooks[:change].nil? ? nil : action.hooks[:change].keys
ForestLiana::SmartActionFieldValidator.validate_smart_action_fields(result, action.name, change_hooks_name)
rescue ForestLiana::Errors::SmartActionInvalidFieldError => invalid_field_error
FOREST_LOGGER.warn invalid_field_error.message
rescue ForestLiana::Errors::SmartActionInvalidFieldHookError => invalid_hook_error
FOREST_LOGGER.error invalid_hook_error.message
return render status: 500, json: { error: invalid_hook_error.message }
end
fields = result.map do |field|
updated_field = result.find{|f| f[:field] == field[:field]}
if updated_field[:enums].is_a?(Array)
if updated_field[:type].is_a?(Array) && updated_field[:value].is_a?(Array) && !(updated_field[:value] - updated_field[:enums]).empty?
updated_field[:value] = nil
end
if !updated_field[:type].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
updated_field[:value] = nil
end
end
updated_field.transform_keys { |key| key.to_s.camelize(:lower) }
end
render serializer: nil, json: { fields: fields }, status: :ok
end
|
#load ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'app/controllers/forest_liana/actions_controller.rb', line 89
def load
load_request = get_smart_action_hook_request
action = get_action(load_request[:collection_name])
if !action
render status: 500, json: {error: 'Error in smart action load hook: cannot retrieve action from collection'}
else
context = get_smart_action_load_ctx(action.fields)
result = action.hooks[:load].(context)
handle_result(result, action)
end
end
|