Skip to content

Commit 9c6f8b5

Browse files
authored
Merge pull request #1914 from instedd/fix/1910-box-form-errors
Fixes issues with boxes#new form
2 parents 8b12613 + 4259505 commit 9c6f8b5

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

app/helpers/cdx_form_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ def log_unhandled_errors
123123
end
124124

125125
Rails.logger.info "Unhandled form errors in #{@object.model_name}: #{unhandled_errors}"
126-
Raven.capture_message("Unhandled form errors",
126+
Raven.capture_message("Unhandled form errors", extra: {
127127
form: @object.model_name,
128128
errors: unhandled_errors,
129-
)
129+
})
130130
end
131131
end

app/models/box.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def self.entity_scope
3030
attribute_field :purpose, copy: true
3131

3232
validates_presence_of :purpose
33-
validates_inclusion_of :purpose, in: ->(_) { Box.purposes }
33+
validates_inclusion_of :purpose, in: ->(_) { Box.purposes }, allow_blank: true
3434
validates_associated :samples
3535

3636
scope :count_samples, -> {

app/models/box_form.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def validate_batches_or_samples_for_purpose
131131
case @option
132132
when "add_batches"
133133
case @box.purpose
134-
when "LOD", "Other"
135-
@box.errors.add(:base, "A batch is required") unless unique_batch_count >= 1
136134
when "Variants"
137135
@box.errors.add(:base, "You must select at least two different batches") unless unique_batch_count >= 2
138136
when "Challenge"
139137
@box.errors.add(:base, "You must select at least one non-distractor batch") unless have_virus_batch?
140138
@box.errors.add(:base, "You must select at least one distractor batch") unless have_distractor_batch?
139+
else
140+
@box.errors.add(:base, "A batch is required") unless unique_batch_count >= 1
141141
end
142142
when "add_samples"
143143
if @samples.empty?
@@ -153,6 +153,8 @@ def validate_batches_or_samples_for_purpose
153153
@box.errors.add(:base, "You must select at least one distractor sample") unless have_distractor_sample?
154154
end
155155
end
156+
else
157+
@box.errors.add(:base, "You must create or select samples")
156158
end
157159
end
158160

app/views/boxes/_form.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.col
88
= cdx_select form: f, name: :purpose, searchable: false, :class => 'input-large' do |select|
99
- select.items Box.purposes
10+
= f.field_errors :purpose
1011

1112
= f.form_field :media do
1213
= cdx_select form: f, name: :media, searchable: false, class: "input-large" do |select|

spec/controllers/boxes_controller_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ def expect_samples(batch, concentrations: [1], replicates:)
442442
end.to change(institution.boxes, :count).by(0)
443443
end
444444
end
445+
446+
it "validates option" do
447+
expect do
448+
post :create, params: { box: { purpose: "Other", option: "" } }
449+
expect(response).to have_http_status(:unprocessable_entity)
450+
end.to change(institution.boxes, :count).by(0)
451+
end
445452
end
446453

447454
describe "destroy" do

0 commit comments

Comments
 (0)