Skip to content

Commit

Permalink
Add comparison to flo_if (#63)
Browse files Browse the repository at this point in the history
* If condition rendering.
Related to #37

* Remove except clause from payload rendering in FloTask view.
  • Loading branch information
jfrioux authored Feb 20, 2021
1 parent 8bd1a3a commit 4925e25
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
20 changes: 20 additions & 0 deletions app/models/floristry/if.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 13 additions & 9 deletions app/views/floristry/workflows/_flo_if.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<div class="flo-sequence">

<strong><%= flo_if.comparison %></strong>
<p>then</p>
<%- 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 %>
<p>then</p>
<% elsif index != flo_if.size - 1 %>
<p>else</p>
<% if index > 0 && index != flo_if.size - 1 %>
<p>else</p>
<% end %>
<% end %>
<%- end %>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/floristry/workflows/_flo_task.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</div>
<div class="flo-task">
<pre>
<%= flo_task.payload.except('scoped', 'procedure_id') %>
<%= flo_task.payload %>
</pre>
</div>
</div>
13 changes: 10 additions & 3 deletions spec/controllers/workflows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 &gt; 3\).*/im
expect(response.body).to match /if \(3 &gt; 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
Expand Down
9 changes: 6 additions & 3 deletions spec/fixtures/floristry_trails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,{},{}]'
Expand Down

0 comments on commit 4925e25

Please sign in to comment.