Skip to content

Commit

Permalink
Allow new criteria to be added
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanHatch committed Jul 17, 2019
1 parent 9117dce commit 80cf99a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
7 changes: 6 additions & 1 deletion app/controllers/need_criteria_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def update
met_when: @form.to_criteria
)

redirect_to need_criteria_path(need)
if params[:commit] == I18n.t('formtastic.actions.need_criteria.save_add_more')
@form.add_extra_criteria!
render action: :edit
else
redirect_to need_criteria_path(need)
end
else
render action: :edit
end
Expand Down
8 changes: 6 additions & 2 deletions app/forms/need_criteria_form.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class NeedCriteriaForm < Rectify::Form
class CriterionForm < Rectify::Form
attribute :value
attribute :value, String
end

mimic :need
mimic :need_criteria
attribute :criteria, Array[CriterionForm]

def map_model(model)
Expand All @@ -18,6 +18,10 @@ def criteria_attributes=(attributes)
}
end

def add_extra_criteria!
self.criteria << CriterionForm.new
end

def to_criteria
self.criteria.map(&:value)
end
Expand Down
5 changes: 3 additions & 2 deletions app/views/need_criteria/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
<h1>'Met when' criteria for this need</h1>
</header>

<%= semantic_form_for @form, url: need_criteria_path(need) do |f| %>
<%= semantic_form_for @form, url: need_criteria_path(need), as: :need_criteria do |f| %>
<%= f.fields_for :criteria, f.object.criteria do |cf| %>
<%= cf.input :value, as: :string, label: "Criteria #{cf.index+1}" %>
<% end %>
<%= f.action :submit, button_html: { class: 'btn btn-primary', value: 'Save criteria' } %>
<%= f.action :submit, button_html: { class: 'btn btn-primary' } %>
<%= f.action :submit, button_html: { class: 'btn btn-primary', value: t('formtastic.actions.need_criteria.save_add_more') } %>
<% end %>
</section>
</article>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ en:
update: 'Save team'
setup:
create: 'Start using Maslow'
need_criteria:
update: 'Save criteria'
save_add_more: 'Save and add another criteria'
labels:
need_response:
response_type: What kind of thing is it?
Expand Down
13 changes: 11 additions & 2 deletions spec/features/editing_need_criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@
new_value = 'cannot do a thing'

fill_in 'Criteria 1', with: new_value
click_on 'Save'
click_on 'Save criteria'

criteria = page.all('.need-criteria li').map(&:text)

expect(criteria.first).to eq(new_value)
end

it 'can add a new criteria' do
visit need_criteria_path(need)
click_on 'Edit'
click_on 'Save and add another criteria'

inputs = page.all('input[type=text]')

expect(inputs.size).to eq(expected_criteria.size + 1)
end

end

0 comments on commit 80cf99a

Please sign in to comment.