Skip to content

Commit 8d71e9c

Browse files
fix: add user inside context for smart actions hooks (#532)
1 parent a0fae59 commit 8d71e9c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

app/controllers/forest_liana/actions_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_smart_action_load_ctx(fields)
3232
field[:value] = nil unless field[:value]
3333
field
3434
end
35-
{:fields => fields, :params => params}
35+
{:fields => fields, :params => params, :user => forest_user}
3636
end
3737

3838
def get_smart_action_change_ctx(fields, field_changed)
@@ -42,7 +42,7 @@ def get_smart_action_change_ctx(fields, field_changed)
4242
ForestLiana::WidgetsHelper.set_field_widget(field)
4343
field
4444
end
45-
{:field_changed => found_field_changed, :fields => fields, :params => params}
45+
{:field_changed => found_field_changed, :fields => fields, :params => params, :user => forest_user}
4646
end
4747

4848
def handle_result(result, action)

spec/dummy/tmp/cache/11D/A92/http%3A%2F%2Flocalhost%3A3000%2Fforest%2Fauthentication%2Fcallback-env_secret_test-client-data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
o: ActiveSupport::Cache::Entry : @value{:client_idI"random_id:ET: issuerI"api.forestadmin.com;T:@version0:@created_atf1639392521.7375941:@expires_in0

spec/requests/actions_controller_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,26 @@
136136
}
137137
}
138138

139+
use_user_context_action_definition = {
140+
name: 'use_user_context',
141+
fields: [foo],
142+
hooks: {
143+
:load => -> (context) {
144+
foo = context[:fields].find{|field| field[:field] == 'foo'}
145+
foo[:value] = context[:user]['first_name']
146+
context[:fields]
147+
}
148+
}
149+
}
150+
139151
action = ForestLiana::Model::Action.new(action_definition)
140152
fail_action = ForestLiana::Model::Action.new(fail_action_definition)
141153
cheat_action = ForestLiana::Model::Action.new(cheat_action_definition)
142154
enums_action = ForestLiana::Model::Action.new(enums_action_definition)
143155
multiple_enums_action = ForestLiana::Model::Action.new(multiple_enums_action_definition)
156+
use_user_context_action = ForestLiana::Model::Action.new(use_user_context_action_definition)
144157
island = ForestLiana.apimap.find {|collection| collection.name.to_s == ForestLiana.name_for(Island)}
145-
island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action]
158+
island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action, use_user_context_action]
146159

147160
describe 'call /load' do
148161
params = {
@@ -174,6 +187,12 @@
174187
expect(response.status).to eq(500)
175188
expect(JSON.parse(response.body)).to eq({'error' => 'Error in smart action load hook: hook must return an array of fields'})
176189
end
190+
191+
it 'should return the first_name of the user who call the action' do
192+
post '/forest/actions/use_user_context/hooks/load', params: JSON.dump(params), headers: headers
193+
expect(response.status).to eq(200)
194+
expect(JSON.parse(response.body)).to eq({'fields' => [foo.merge({:value => 'Michael'}).transform_keys { |key| key.to_s.camelize(:lower) }.stringify_keys]})
195+
end
177196
end
178197

179198
describe 'call /change' do

0 commit comments

Comments
 (0)