Skip to content

Commit a1ddac1

Browse files
fix(smart-actions): reset value when not present in enums in hook response (#397)
1 parent 673b364 commit a1ddac1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

app/controllers/forest_liana/actions_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ def handle_result(result, formatted_fields, action)
5353
end
5454

5555
# Apply result on fields (transform the object back to an array), preserve order.
56-
fields = action.fields.map { |field| result[field[:field]] }
56+
fields = action.fields.map do |field|
57+
updated_field = result[field[:field]]
58+
# Reset `value` when not present in `enums` (which means `enums` has changed).
59+
if updated_field[:enums].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
60+
updated_field[:value] = nil
61+
end
62+
updated_field
63+
end
5764

5865
render serializer: nil, json: { fields: fields}, status: :ok
5966
end

spec/requests/actions_controller_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
description: nil,
3131
widget: nil,
3232
}
33+
enum = {
34+
field: 'enum',
35+
type: 'Enum',
36+
enums: %w[a b c],
37+
}
38+
3339
action_definition = {
3440
name: 'my_action',
3541
fields: [foo],
@@ -76,11 +82,25 @@
7682
}
7783
}
7884
}
85+
enums_action_definition = {
86+
name: 'enums_action',
87+
fields: [foo, enum],
88+
hooks: {
89+
:change => {
90+
'foo' => -> (context) {
91+
fields = context[:fields]
92+
fields['enum'][:enums] = %w[c d e]
93+
return fields
94+
}
95+
}
96+
}
97+
}
7998
action = ForestLiana::Model::Action.new(action_definition)
8099
fail_action = ForestLiana::Model::Action.new(fail_action_definition)
81100
cheat_action = ForestLiana::Model::Action.new(cheat_action_definition)
101+
enums_action = ForestLiana::Model::Action.new(enums_action_definition)
82102
island = ForestLiana.apimap.find {|collection| collection.name.to_s == ForestLiana.name_for(Island)}
83-
island.actions = [action, fail_action, cheat_action]
103+
island.actions = [action, fail_action, cheat_action, enums_action]
84104

85105
describe 'call /load' do
86106
params = {recordIds: [1], collectionName: 'Island'}
@@ -134,6 +154,16 @@
134154
post '/forest/actions/cheat_action/hooks/change', JSON.dump(params), 'CONTENT_TYPE' => 'application/json'
135155
expect(response.status).to eq(500)
136156
end
157+
158+
it 'should reset value when enums has changed' do
159+
updated_enum = enum.clone.merge({:previousValue => nil, :value => 'a'}) # set value to a
160+
p = {recordIds: [1], fields: [updated_foo, updated_enum], collectionName: 'Island', changedField: 'foo'}
161+
post '/forest/actions/enums_action/hooks/change', JSON.dump(p), 'CONTENT_TYPE' => 'application/json'
162+
expect(response.status).to eq(200)
163+
expected_enum = updated_enum.clone.merge({ :enums => %w[c d e], :value => nil}).stringify_keys
164+
expect(JSON.parse(response.body)).to eq({'fields' => [updated_foo.stringify_keys, expected_enum]})
165+
end
166+
137167
end
138168
end
139169
end

0 commit comments

Comments
 (0)