From b26eb96c14d0345fdd46544503d634b1fe4c508b Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 20 Jul 2022 15:59:38 +0100 Subject: [PATCH] extract selection methods into a helper --- app/helpers/locations_helper.rb | 20 +++++++++++ app/views/locations/edit.html.erb | 6 ---- app/views/locations/new.html.erb | 8 ----- spec/helpers/locations_helper_spec.rb | 49 +++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 app/helpers/locations_helper.rb create mode 100644 spec/helpers/locations_helper_spec.rb diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb new file mode 100644 index 000000000..1d2412184 --- /dev/null +++ b/app/helpers/locations_helper.rb @@ -0,0 +1,20 @@ +module LocationsHelper + def mobility_type_selection + mobility_types_to_display = Location.mobility_types.excluding("Property designed to accessible general standard", "Missing") + mobility_types_to_display.map { |key, value| OpenStruct.new(id: key, name: key.to_s.humanize, description: I18n.t("questions.descriptions.location.mobility_type.#{value}")) } + end + + def another_location_selection + selection_options(%w[Yes No]) + end + + def type_of_units_selection + selection_options(Location.type_of_units) + end + + def selection_options(resource) + return [] if resource.blank? + + resource.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } + end +end diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb index 19d008544..bee510eaf 100644 --- a/app/views/locations/edit.html.erb +++ b/app/views/locations/edit.html.erb @@ -29,17 +29,12 @@ hint: { text: I18n.t("hints.location.units") }, autofocus: true %> - <% type_of_units_selection = Location.type_of_units.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :type_of_unit, type_of_units_selection, :id, :name, legend: { text: I18n.t("questions.location.type_of_unit"), size: "m" } %> - <% mobility_types_to_display = Location.mobility_types.excluding("Property designed to accessible general standard", "Missing") %> - - <% mobility_type_selection = mobility_types_to_display.map { |key, value| OpenStruct.new(id: key, name: key.to_s.humanize, description: I18n.t("questions.descriptions.location.mobility_type.#{value}")) } %> - <%= f.govuk_collection_radio_buttons :mobility_type, mobility_type_selection, :id, @@ -53,7 +48,6 @@ <%= govuk_section_break(visible: true, size: "m") %> - <% another_location_selection = %w[Yes no].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :add_another_location, another_location_selection, :id, diff --git a/app/views/locations/new.html.erb b/app/views/locations/new.html.erb index 63c160bc5..184fed8bb 100644 --- a/app/views/locations/new.html.erb +++ b/app/views/locations/new.html.erb @@ -29,18 +29,12 @@ hint: { text: I18n.t("hints.location.units") }, autofocus: true %> - <% type_of_units_selection = Location.type_of_units.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> - <%= f.govuk_collection_radio_buttons :type_of_unit, type_of_units_selection, :id, :name, legend: { text: I18n.t("questions.location.type_of_unit"), size: "m" } %> - <% mobility_types_to_display = Location.mobility_types.excluding("Property designed to accessible general standard", "Missing") %> - - <% mobility_type_selection = mobility_types_to_display.map { |key, value| OpenStruct.new(id: key, name: key.to_s.humanize, description: I18n.t("questions.descriptions.location.mobility_type.#{value}")) } %> - <%= f.govuk_collection_radio_buttons :mobility_type, mobility_type_selection, :id, @@ -54,8 +48,6 @@ <%= govuk_section_break(visible: true, size: "m") %> - <% another_location_selection = %w[Yes No].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> - <%= f.govuk_collection_radio_buttons :add_another_location, another_location_selection, :id, diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb new file mode 100644 index 000000000..402772dec --- /dev/null +++ b/spec/helpers/locations_helper_spec.rb @@ -0,0 +1,49 @@ +require "rails_helper" + +RSpec.describe LocationsHelper do + describe "mobility type selection" do + expected_selection = [OpenStruct.new(id: "Wheelchair-user standard", name: "Wheelchair-user standard", description: "The majority of units are suitable for someone who uses a wheelchair and offer the full use of all rooms and facilities."), + OpenStruct.new(id: "Fitted with equipment and adaptations", name: "Fitted with equipment and adaptations", description: "For example, the majority of units have been fitted with stairlifts, ramps, level access showers or grab rails."), + OpenStruct.new(id: "None", name: "None", description: "The majority of units are not designed to wheelchair-user standards or fitted with any equipment and adaptations.")] + it "returns correct selection to display" do + expect(mobility_type_selection).to eq(expected_selection) + end + end + + describe "another location selection" do + it "returns correct selection to display" do + expected_selection = [OpenStruct.new(id: "Yes", name: "Yes"), OpenStruct.new(id: "No", name: "No")] + expect(another_location_selection).to eq(expected_selection) + end + end + + describe "type of units selection" do + it "returns correct selection to display" do + expected_selection = [OpenStruct.new(id: "Bungalow", name: "Bungalow"), + OpenStruct.new(id: "Self-contained flat or bedsit", name: "Self-contained flat or bedsit"), + OpenStruct.new(id: "Self-contained flat or bedsit with common facilities", name: "Self-contained flat or bedsit with common facilities"), + OpenStruct.new(id: "Self-contained house", name: "Self-contained house"), + OpenStruct.new(id: "Shared flat", name: "Shared flat"), + OpenStruct.new(id: "Shared house or hostel", name: "Shared house or hostel")] + expect(type_of_units_selection).to eq(expected_selection) + end + end + + describe "selection options" do + it "returns empty array for nil" do + expect(selection_options(nil)).to eq([]) + end + + it "returns empty array for empty string" do + expect(selection_options("")).to eq([]) + end + + it "returns empty array for empty object" do + expect(selection_options({})).to eq([]) + end + + it "can map a resource with values" do + expect(selection_options(%w[example])).to eq([OpenStruct.new(id: "example", name: "Example")]) + end + end +end