Skip to content

Commit 4c3fae3

Browse files
authored
Merge pull request #1804 from alphagov/revert-add-value-field
Revert "Merge pull request #1800 from alphagov/add-value-field-to-sel…
2 parents 958ce36 + 8ff9cc6 commit 4c3fae3

File tree

8 files changed

+35
-53
lines changed

8 files changed

+35
-53
lines changed

app/components/question/selection_component/view.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def none_of_the_above_question_text
6868
end
6969

7070
def radio_button_options
71-
question.selection_options_with_none_of_the_above.map.with_index do |option, index|
72-
form_builder.govuk_radio_button :selection, option.value, label: { text: option.name }, link_errors: index.zero?
71+
question.answer_settings.selection_options.map.with_index do |option, index|
72+
form_builder.govuk_radio_button :selection, option.name, label: { text: option.name }, link_errors: index.zero?
7373
end
7474
end
7575

7676
def checkbox_options
7777
question.answer_settings.selection_options.map.with_index do |option, index|
78-
form_builder.govuk_check_box :selection, option.value, label: { text: option.name }, link_errors: index.zero?
78+
form_builder.govuk_check_box :selection, option.name, label: { text: option.name }, link_errors: index.zero?
7979
end
8080
end
8181
end

app/models/question/selection.rb

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def allow_multiple_answers?
1919
end
2020

2121
def show_answer
22-
return selection_without_blanks.map { |selected| name_from_value(selected) }.join(", ") if allow_multiple_answers?
22+
return selection_without_blanks.join(", ") if allow_multiple_answers?
2323

24-
selection_name
24+
selection
2525
end
2626

2727
def show_answer_in_email
@@ -39,20 +39,6 @@ def show_answer_in_json(*)
3939
hash
4040
end
4141

42-
def selection_name
43-
return nil if selection.nil?
44-
return "" if selection.blank?
45-
46-
name_from_value(selection)
47-
end
48-
49-
# Show the selection option name, which can be different to the value. Value
50-
# should stay the same across FormDocuments in different languages.
51-
def name_from_value(selected)
52-
@options_by_value ||= answer_settings.selection_options.index_by(&:value)
53-
@options_by_value[selected]&.name
54-
end
55-
5642
def show_optional_suffix
5743
false
5844
end
@@ -80,11 +66,11 @@ def clear_none_of_the_above_answer_if_not_selected
8066
end
8167

8268
def allowed_options
83-
selection_options_with_none_of_the_above.map(&:value)
69+
selection_options_with_none_of_the_above.map(&:name)
8470
end
8571

8672
def none_of_the_above_option
87-
OpenStruct.new(name: I18n.t("page.none_of_the_above"), value: I18n.t("page.none_of_the_above"))
73+
OpenStruct.new(name: I18n.t("page.none_of_the_above"))
8874
end
8975

9076
def selection_without_blanks

spec/components/question/selection_component/view_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
let(:question) { build :single_selection_question, is_optional:, selection_options: }
5656

5757
let(:selection_options) do
58-
option_text = Faker::Lorem.sentence
59-
Array.new(30).map { |_index| OpenStruct.new(name: option_text, value: option_text) }
58+
Array.new(30).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence) }
6059
end
6160

6261
it "renders the question text as a heading" do

spec/factories/models/pages.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
trait :with_selections_settings do
2727
transient do
2828
only_one_option { "true" }
29-
selection_options { [DataStruct.new(name: "Option 1", value: "Option 1"), DataStruct.new(name: "Option 2", value: "Option 2")] }
29+
selection_options { [DataStruct.new(name: "Option 1"), DataStruct.new(name: "Option 2")] }
3030
end
3131

3232
answer_type { "selection" }

spec/factories/models/question/selection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
transient do
1616
only_one_option { "true" }
17-
selection_options { [DataStruct.new(name: "Option 1", value: "Option 1"), DataStruct.new(name: "Option 2", value: "Option 2")] }
17+
selection_options { [DataStruct.new(name: "Option 1"), DataStruct.new(name: "Option 2")] }
1818
none_of_the_above_question { nil }
1919
end
2020

spec/factories/v2_step.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
trait :with_selections_settings do
4545
transient do
4646
only_one_option { "true" }
47-
selection_options { [{ name: "Option 1", value: "Option 1" }, { name: "Option 2", value: "Option 2" }] }
47+
selection_options { [{ name: "Option 1" }, { name: "Option 2" }] }
4848
end
4949

5050
answer_type { "selection" }

spec/features/fill_in_autocomplete_question_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require "rails_helper"
22

33
feature "Fill in and submit a form with an autocomplete question", type: :feature do
4-
let(:steps) { [build(:v2_question_page_step, :with_selections_settings, id: 1, routing_conditions: [], question_text:, selection_options:)] }
4+
let(:steps) { [(build :v2_question_page_step, :with_selections_settings, id: 1, routing_conditions: [], question_text:, selection_options:)] }
55
let(:form) { build :v2_form_document, :live?, form_id: 1, name: "Fill in this form", steps:, start_page: 1 }
6-
let(:selection_options) { Array.new(31).each_with_index.map { |_element, index| { name: "Answer #{index}", value: "Answer #{index}" } } }
6+
let(:selection_options) { Array.new(31).each_with_index.map { |_element, index| { name: "Answer #{index}" } } }
77
let(:question_text) { Faker::Lorem.question }
88
let(:answer_text) { "Answer 1" }
99
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }

spec/models/question/selection_spec.rb

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
RSpec.describe Question::Selection, type: :model do
44
subject(:question) { build :selection, only_one_option:, selection_options:, is_optional:, question_text: }
55

6-
# We use different values for name and value to make it clear which is which in the tests.
7-
# Name would be the text in current locale and value doesn't change between locales.
8-
# For English forms they would be the same, but for Welsh forms they would be different.
9-
let(:selection_options) { [OpenStruct.new({ name: "display option 1", value: "option 1" }), OpenStruct.new({ name: "display option 2", value: "option 2" })] }
6+
let(:selection_options) { [OpenStruct.new({ name: "option 1" }), OpenStruct.new({ name: "option 2" })] }
107
let(:is_optional) { false }
118
let(:only_one_option) { "false" }
129
let(:question_text) { Faker::Lorem.question }
@@ -67,21 +64,21 @@
6764

6865
context "when selection has a value" do
6966
before do
70-
question.selection = ["option 1"]
67+
question.selection = %w[something]
7168
end
7269

7370
it "shows the answer" do
74-
expect(question.show_answer).to eq("display option 1")
71+
expect(question.show_answer).to eq("something")
7572
end
7673

7774
it "shows the answer in show_answer_in_csv" do
78-
expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"])
75+
expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"])
7976
end
8077

8178
it "returns a hash for show_answer_in_json" do
8279
expect(question.show_answer_in_json).to eq({
83-
selections: ["option 1"],
84-
answer_text: "display option 1",
80+
selections: %w[something],
81+
answer_text: "something",
8582
})
8683
end
8784
end
@@ -155,21 +152,21 @@
155152

156153
context "when selection has a value" do
157154
before do
158-
question.selection = ["option 1"]
155+
question.selection = %w[something]
159156
end
160157

161158
it "shows the answer" do
162-
expect(question.show_answer).to eq("display option 1")
159+
expect(question.show_answer).to eq("something")
163160
end
164161

165162
it "shows the answer in show_answer_in_csv" do
166-
expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"])
163+
expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"])
167164
end
168165

169166
it "returns a hash for show_answer_in_json" do
170167
expect(question.show_answer_in_json).to eq({
171-
selections: ["option 1"],
172-
answer_text: "display option 1",
168+
selections: %w[something],
169+
answer_text: "something",
173170
})
174171
end
175172
end
@@ -213,20 +210,20 @@
213210

214211
context "when selection has a value" do
215212
before do
216-
question.selection = "option 1"
213+
question.selection = "something"
217214
end
218215

219216
it "shows the answer" do
220-
expect(question.show_answer).to eq("display option 1")
217+
expect(question.show_answer).to eq("something")
221218
end
222219

223220
it "shows the answer in show_answer_in_csv" do
224-
expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"])
221+
expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"])
225222
end
226223

227224
it "returns a hash for show_answer_in_json" do
228225
expect(question.show_answer_in_json).to eq({
229-
answer_text: "display option 1",
226+
answer_text: "something",
230227
})
231228
end
232229
end
@@ -283,20 +280,20 @@
283280

284281
context "when selection has a value" do
285282
before do
286-
question.selection = "option 1"
283+
question.selection = "something"
287284
end
288285

289286
it "shows the answer" do
290-
expect(question.show_answer).to eq("display option 1")
287+
expect(question.show_answer).to eq("something")
291288
end
292289

293290
it "shows the answer in show_answer_in_csv" do
294-
expect(question.show_answer_in_csv).to eq(Hash[question_text, "display option 1"])
291+
expect(question.show_answer_in_csv).to eq(Hash[question_text, "something"])
295292
end
296293

297294
it "returns a hash for show_answer_in_json" do
298295
expect(question.show_answer_in_json).to eq({
299-
answer_text: "display option 1",
296+
answer_text: "something",
300297
})
301298
end
302299
end
@@ -433,7 +430,7 @@
433430

434431
describe "#selection_options_with_none_of_the_above" do
435432
let(:only_one_option) { "true" }
436-
let(:none_of_the_above_option) { OpenStruct.new(name: I18n.t("page.none_of_the_above"), value: I18n.t("page.none_of_the_above")) }
433+
let(:none_of_the_above_option) { OpenStruct.new(name: I18n.t("page.none_of_the_above")) }
437434

438435
context "when the user can select 'None of the above'" do
439436
let(:is_optional) { true }
@@ -466,15 +463,15 @@
466463

467464
describe "#autocomplete_component?" do
468465
context "when there are 30 selection options" do
469-
let(:selection_options) { Array.new(30).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence, value: Faker::Lorem.sentence) } }
466+
let(:selection_options) { Array.new(30).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence) } }
470467

471468
it "returns false" do
472469
expect(question.autocomplete_component?).to be false
473470
end
474471
end
475472

476473
context "when there are more than 30 selection options" do
477-
let(:selection_options) { Array.new(31).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence, value: Faker::Lorem.sentence) } }
474+
let(:selection_options) { Array.new(31).map { |_index| OpenStruct.new(name: Faker::Lorem.sentence) } }
478475

479476
it "returns true" do
480477
expect(question.autocomplete_component?).to be true

0 commit comments

Comments
 (0)