-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): support layout elements in smart action form hooks (#691)
- Loading branch information
1 parent
af1bc99
commit d2a2d47
Showing
8 changed files
with
276 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module ForestLiana | ||
class SmartActionFormParser | ||
def self.extract_fields_and_layout(form) | ||
fields = [] | ||
layout = [] | ||
form&.each do |element| | ||
if element[:type] == 'Layout' | ||
validate_layout_element(element) | ||
element[:component] = element[:component].camelize(:lower) | ||
if %w[page row].include?(element[:component]) | ||
extract = extract_fields_and_layout_for_component(element) | ||
layout << element | ||
fields.concat(extract[:fields]) | ||
else | ||
layout << element | ||
end | ||
else | ||
fields << element | ||
# frontend rule | ||
layout << { component: 'input', fieldId: element[:field] } | ||
end | ||
end | ||
|
||
{ fields: fields, layout: layout } | ||
end | ||
|
||
def self.extract_fields_and_layout_for_component(element) | ||
# 'page' is in camel case because at this step the 'component' attribute is already convert for the response | ||
key = element[:component] == 'page' ? :elements : :fields | ||
extract = extract_fields_and_layout(element[key]) | ||
element[key] = extract[:layout] | ||
|
||
extract | ||
end | ||
|
||
def self.validate_layout_element(element) | ||
valid_components = %w[Page Row Separator HtmlBlock] | ||
unless valid_components.include?(element[:component]) | ||
raise ForestLiana::Errors::HTTP422Error.new( | ||
"#{element[:component]} is not a valid component. Valid components are #{valid_components.join(' or ')}" | ||
) | ||
end | ||
|
||
if element[:component] == 'Page' | ||
unless element[:elements].is_a? Array | ||
raise ForestLiana::Errors::HTTP422Error.new( | ||
"Page components must contain an array of fields or layout elements in property 'elements'" | ||
) | ||
end | ||
|
||
if element[:elements].any? { |element| element[:component] === 'Page' } | ||
raise ForestLiana::Errors::HTTP422Error.new('Pages cannot contain other pages') | ||
end | ||
end | ||
|
||
if element[:component] == 'Row' | ||
unless element[:fields].is_a? Array | ||
raise ForestLiana::Errors::HTTP422Error.new( | ||
"Row components must contain an array of fields in property 'fields'" | ||
) | ||
end | ||
|
||
if element[:fields].any? { |element| element[:type] === 'Layout' } | ||
raise ForestLiana::Errors::HTTP422Error.new('Row components can only contain fields') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
spec/services/forest_liana/smart_action_form_parser_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module ForestLiana | ||
describe SmartActionFormParser do | ||
describe "self.validate_layout_element" do | ||
it "raise an error with an invalid component" do | ||
expect { SmartActionFormParser.validate_layout_element({ type: 'Layout', component: 'foo' }) } | ||
.to raise_error( | ||
ForestLiana::Errors::HTTP422Error, | ||
'foo is not a valid component. Valid components are Page or Row or Separator or HtmlBlock' | ||
) | ||
end | ||
|
||
it "raise an error with an invalid Page" do | ||
expect do | ||
SmartActionFormParser.validate_layout_element( | ||
{ type: 'Layout', component: 'Page', elements: 'foo' } | ||
) | ||
end.to raise_error( | ||
ForestLiana::Errors::HTTP422Error, | ||
"Page components must contain an array of fields or layout elements in property 'elements'" | ||
) | ||
end | ||
|
||
it "raise an error with a Page that contains page" do | ||
expect do | ||
SmartActionFormParser.validate_layout_element( | ||
{ type: 'Layout', component: 'Page', elements: [{ type: 'Layout', component: 'Page', elements: [] }] } | ||
) | ||
end.to raise_error(ForestLiana::Errors::HTTP422Error, 'Pages cannot contain other pages') | ||
end | ||
|
||
it "should raise an error with an invalid Row" do | ||
expect do | ||
SmartActionFormParser.validate_layout_element( | ||
{ type: 'Layout', component: 'Row', fields: 'foo' } | ||
) | ||
end.to raise_error( | ||
ForestLiana::Errors::HTTP422Error, | ||
"Row components must contain an array of fields in property 'fields'" | ||
) | ||
end | ||
|
||
it "raise an error with a row that contains layout element" do | ||
expect do | ||
SmartActionFormParser.validate_layout_element( | ||
{ | ||
type: 'Layout', | ||
component: 'Row', | ||
fields: [ { type: 'Layout', component: 'HtmlBlock', fields: 'Row components can only contain fields' }] | ||
} | ||
) | ||
end.to raise_error(ForestLiana::Errors::HTTP422Error, 'Row components can only contain fields') | ||
end | ||
end | ||
end | ||
end |