Skip to content

Commit

Permalink
Allow criteria to be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanHatch committed Jul 17, 2019
1 parent d37afb0 commit d599416
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/forms/need_criteria_form.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class NeedCriteriaForm < Rectify::Form
class CriterionForm < Rectify::Form
attribute :value, String
attribute :delete, Boolean

def deleted?
self.delete == true
end
end

mimic :need_criteria
Expand All @@ -15,7 +20,7 @@ def map_model(model)
def criteria_attributes=(attributes)
self.criteria = attributes.map {|_, item|
CriterionForm.from_params(item)
}
}.reject(&:deleted?)
end

def add_extra_criteria!
Expand Down
7 changes: 6 additions & 1 deletion app/views/need_criteria/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

<%= semantic_form_for @form, url: need_criteria_path(need), as: :need_criteria do |f| %>

<ul>
<%= f.fields_for :criteria, f.object.criteria do |cf| %>
<%= cf.input :value, as: :string, label: "Criteria #{cf.index+1}" %>
<li>
<%= cf.input :value, as: :string, label: "Criteria #{cf.index+1}" %>
<%= cf.input :delete, as: :boolean %>
</li>
<% end %>
</ul>

<%= 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') } %>
Expand Down
14 changes: 14 additions & 0 deletions spec/features/editing_need_criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@
expect(inputs.size).to eq(expected_criteria.size + 1)
end

it 'can delete a criteria' do
visit need_criteria_path(need)
click_on 'Edit'

input_container = page.all('form li').first
within(input_container) do
check 'Delete'
end
click_on 'Save criteria'

criteria = page.all('.need-criteria li').map(&:text)
expect(criteria).to contain_exactly(expected_criteria.last)
end

end

0 comments on commit d599416

Please sign in to comment.