diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 686a5f36c..6672d79c2 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -74,14 +74,20 @@ class OrganisationsController < ApplicationController def new @resource = Organisation.new + @rent_periods = helpers.rent_periods_with_checked_attr render "new", layout: "application" end def create + selected_rent_periods = params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank @resource = Organisation.new(org_params) if @resource.save + OrganisationRentPeriod.transaction do + selected_rent_periods.each { |period| OrganisationRentPeriod.create!(organisation: @resource, rent_period: period) } + end redirect_to organisations_path else + @rent_periods = helpers.rent_periods_with_checked_attr(checked_periods: selected_rent_periods) render :new, status: :unprocessable_entity end end diff --git a/app/views/organisations/new.html.erb b/app/views/organisations/new.html.erb index 5d1747ed7..cb62157df 100644 --- a/app/views/organisations/new.html.erb +++ b/app/views/organisations/new.html.erb @@ -56,6 +56,17 @@ :name, legend: { text: "Does the organisation hold its own stock?", size: "m" } %> + <%= f.govuk_check_boxes_fieldset :rent_periods, + legend: { text: "What are the rent periods for the organisation?" } do %> + <% @rent_periods.map do |key, period| %> + <%= f.govuk_check_box :rent_periods, + key, + label: { text: period["value"] }, + checked: period[:checked] + %> + <% end %> + <% end %> + <%= f.govuk_submit "Create organisation" %> diff --git a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb new file mode 100644 index 000000000..1c4d70fe8 --- /dev/null +++ b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb @@ -0,0 +1,54 @@ +require "rails_helper" + +RSpec.describe OrganisationsController, type: :request do + let(:user) { create(:user, :support) } + let(:headers) { { "Accept" => "text/html" } } + let(:page) { Capybara::Node::Simple.new(response.body) } + + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + end + + describe "#new" do + before do + get new_organisation_path + end + + it "displays the rent periods question" do + expect(page).to have_content "What are the rent periods for the organisation?" + end + + it "the checkboxes for each rent period are checked by default" do + checkboxes = page.all "input[type='checkbox']" + expect(checkboxes.count).to be 10 + expect(checkboxes.all? { |box| box[:checked] }).to be true + end + end + + describe "#create" do + let(:org_name) { "abode team" } + let(:expected_rent_periods) { [1, 2, 3] } + let(:params) do + { + "organisation": { + name: org_name, + provider_type: "LA", + rent_periods: expected_rent_periods, + }, + } + end + + before do + post organisations_path headers:, params: + end + + it "creates organisation rent periods with the correct rent period and organisation id" do + org = Organisation.find_by_name org_name + org_rent_periods = OrganisationRentPeriod.all + expect(org_rent_periods.count).to be expected_rent_periods.count + expect(org_rent_periods.map(&:rent_period)).to match_array expected_rent_periods + expect(org_rent_periods.map(&:organisation_id)).to all be org.id + end + end +end