From 4925e254c1003d7bb2c07a4092f40d9dcb9fca7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Rioux?= Date: Sat, 20 Feb 2021 10:05:29 -0500 Subject: [PATCH] Add comparison to flo_if (#63) * If condition rendering. Related to #37 * Remove except clause from payload rendering in FloTask view. --- app/models/floristry/if.rb | 20 +++++++++++++++++ .../floristry/workflows/_flo_if.html.erb | 22 +++++++++++-------- .../floristry/workflows/_flo_task.html.erb | 2 +- spec/controllers/workflows_spec.rb | 13 ++++++++--- spec/fixtures/floristry_trails.yml | 9 +++++--- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/app/models/floristry/if.rb b/app/models/floristry/if.rb index 71caa0e..2175889 100644 --- a/app/models/floristry/if.rb +++ b/app/models/floristry/if.rb @@ -1,5 +1,25 @@ module Floristry class If < BranchProcedure + def comparison + + atts = children[0].raw_atts[0] + comparator = atts[0] + left = comparison_value(atts[1][0][1]) + right = comparison_value(atts[1][1][1]) + + "if (#{left} #{comparator} #{right})" + end + + private + + def comparison_value(att) + + comp_val = att + if comp_val.is_a? Array + comp_val = "#{comp_val[0][1]}.#{comp_val[1][1]}" + end + comp_val + end end end \ No newline at end of file diff --git a/app/views/floristry/workflows/_flo_if.html.erb b/app/views/floristry/workflows/_flo_if.html.erb index aabaecc..5ee0a01 100644 --- a/app/views/floristry/workflows/_flo_if.html.erb +++ b/app/views/floristry/workflows/_flo_if.html.erb @@ -1,14 +1,18 @@
+ + <%= flo_if.comparison %> +

then

<%- flo_if.each_with_index do |child, index| %> - <%= render :partial => child, - :layout => child.layout, - :object => child, - :locals => {:leaf => child} %> + <% unless child.class == Floristry::Att %> + <%= render :partial => child, + :layout => child.layout, + :object => child, + :locals => {:leaf => child} + %> - <% if index == 0 %> -

then

- <% elsif index != flo_if.size - 1 %> -

else

+ <% if index > 0 && index != flo_if.size - 1 %> +

else

+ <% end %> <% end %> - <%- end %> + <% end %>
\ No newline at end of file diff --git a/app/views/floristry/workflows/_flo_task.html.erb b/app/views/floristry/workflows/_flo_task.html.erb index 35de13c..dedd23d 100644 --- a/app/views/floristry/workflows/_flo_task.html.erb +++ b/app/views/floristry/workflows/_flo_task.html.erb @@ -5,7 +5,7 @@
-      <%= flo_task.payload.except('scoped', 'procedure_id') %>
+      <%= flo_task.payload %>
     
diff --git a/spec/controllers/workflows_spec.rb b/spec/controllers/workflows_spec.rb index c0522cc..411dbf8 100644 --- a/spec/controllers/workflows_spec.rb +++ b/spec/controllers/workflows_spec.rb @@ -41,11 +41,18 @@ end context "if" do - it 'renders the if partial' do - sequence = floristry_trails(:sequence_if) + it 'renders the if partial with number comparison' do + sequence = floristry_trails(:sequence_if_nums) get :edit, id: sequence.wfid expect(response).to render_template(partial: '_flo_if') - expect(response.body).to match /if \(0 > 3\).*/im + expect(response.body).to match /if \(3 > 0\).*/i + end + + it 'renders the if partial with field and string comparison' do + sequence = floristry_trails(:sequence_if_field_string) + get :edit, id: sequence.wfid + expect(response).to render_template(partial: '_flo_if') + expect(response.body).to match /if \(f.comment == yes\).*/i end end end diff --git a/spec/fixtures/floristry_trails.yml b/spec/fixtures/floristry_trails.yml index 0403931..6668376 100644 --- a/spec/fixtures/floristry_trails.yml +++ b/spec/fixtures/floristry_trails.yml @@ -10,10 +10,13 @@ sequence_cron: wfid: test-u0-11111111.1337.cronnewyear tree: '["sequence",[["schedule",[["_att",[["cron",[],3],["_sqs","0 0 1 jan *",3]],3],["alice",[["_att",[["_",[],4]],4]],4,{"ret":"alice"}]],3]],2,{},{}]' -sequence_if: - wfid: test-u0-11111111.1337.iftruebob - tree: '["sequence",[["if",[["_att",[["\u003e",[["_num",0,3],["_num",3,3]],3]],3],["sequence",[["alice",[["_att",[["_",[],5]],5]],5]],4],["sequence",[["bob",[["_att",[["_",[],7]],7]],7,{"ret":"bob","bob_tstamp":"2019-02-16 10:44:15 -0500"}]],6]],3]],2,{},{"name":"If 0 \u003e 1 alice else bob"}]' +sequence_if_nums: + wfid: test-u0-11111111.1337.ifnumsbob + tree: '["sequence",[["alice",[["_att",[["_",[],3]],3]],3,{"ret":"alice","alice_tstamp":"2021-02-07 12:49:57 -0500"}],["if",[["_att",[["\u003e",[["_num",3,4],["_num",0,4]],4]],4],["bob",[["_att",[["_",[],5]],5]],5,{"ret":"bob","alice_tstamp":"2021-02-07 12:49:57 -0500","bob_tstamp":"2021-02-07 12:49:57 -0500"}]],4]],2,{},{"name":"playing with if"}]' +sequence_if_field_string: + wfid: test-u0-11111111.1337.ifstringbob + tree: '["sequence",[["web",[["_att",[["model",[],3],["_sqs","form_task",3]],3],["_att",[["_",[],3]],3]],3,{"ret":null,"post_tstamp":"2021-02-07 12:59:36 -0500","comment":"yes"}],["if",[["_att",[["==",[["_ref",[["_sqs","f",4],["_sqs","comment",4]],4],["_sqs","yes",4]],4]],4],["bob",[["_att",[["_",[],5]],5]],5,{"ret":"bob","post_tstamp":"2021-02-07 12:59:36 -0500","comment":"yes","bob_tstamp":"2021-02-07 12:59:51 -0500"}]],4]],2,{},{"name":"playing with if"}]' sequence_set: wfid: test-u0-11111111.1337.seqthatsets tree: '["sequence",[["alice",[["_att",[["_",[],3]],3]],3,{"ret":"alice"}],["set",[["_att",[["f.a",[],4]],4],["_num",2,5]],4]],2,{},{}]'