Skip to content

Commit

Permalink
fix(smart-actions): transform legacy widgets in hooks (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeCisco authored Dec 7, 2020
1 parent 93ab8f7 commit 0183d08
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
11 changes: 9 additions & 2 deletions app/controllers/forest_liana/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ def get_record
end

def get_smart_action_load_ctx(fields)
fields = fields.reduce({}) {|p, c| p.update(c[:field] => c.merge!(value: nil))}
fields = fields.reduce({}) do |p, c|
ForestLiana::WidgetsHelper.set_field_widget(c)
p.update(c[:field] => c.merge!(value: nil))
end
{:record => get_record, :fields => fields}
end

def get_smart_action_change_ctx(fields)
fields = fields.reduce({}) {|p, c| p.update(c[:field] => c.permit!.to_h)}
fields = fields.reduce({}) do |p, c|
field = c.permit!.to_h.symbolize_keys
ForestLiana::WidgetsHelper.set_field_widget(field)
p.update(c[:field] => field)
end
{:record => get_record, :fields => fields}
end

Expand Down
59 changes: 59 additions & 0 deletions app/helpers/forest_liana/widgets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'set'

module ForestLiana
module WidgetsHelper

@widget_edit_list = [
'address editor',
'belongsto typeahead',
'belongsto dropdown',
'boolean editor',
'checkboxes',
'color editor',
'date editor',
'dropdown',
'embedded document editor',
'file picker',
'json code editor',
'input array',
'multiple select',
'number input',
'point editor',
'price editor',
'radio button',
'rich text',
'text area editor',
'text editor',
'time input',
]

@v1_to_v2_edit_widgets_mapping = {
address: 'address editor',
'belongsto select': 'belongsto dropdown',
'color picker': 'color editor',
'date picker': 'date editor',
price: 'price editor',
'JSON editor': 'json code editor',
'rich text editor': 'rich text',
'text area': 'text area editor',
'text input': 'text editor',
}

def self.set_field_widget(field)

if field[:widget]
if @v1_to_v2_edit_widgets_mapping[field[:widget].to_sym]
field[:widgetEdit] = {name: @v1_to_v2_edit_widgets_mapping[field[:widget].to_sym], parameters: {}}
elsif @widget_edit_list.include?(field[:widget])
field[:widgetEdit] = {name: field[:widget], parameters: {}}
end
end

if !field.key?(:widgetEdit)
field[:widgetEdit] = nil
end

field.delete(:widget)
end
end
end
5 changes: 4 additions & 1 deletion spec/requests/actions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@
it 'should respond 200' do
post '/forest/actions/my_action/hooks/change', JSON.dump(params), 'CONTENT_TYPE' => 'application/json'
expect(response.status).to eq(200)
expect(JSON.parse(response.body)).to eq({'fields' => [updated_foo.merge({:value => 'baz'}).stringify_keys]})
expected = updated_foo.merge({:value => 'baz'})
expected[:widgetEdit] = nil
expected.delete(:widget)
expect(JSON.parse(response.body)).to eq({'fields' => [expected.stringify_keys]})
end

it 'should respond 500 with bad params' do
Expand Down

0 comments on commit 0183d08

Please sign in to comment.