Arthur Campbell
8 months ago
committed by
GitHub
34 changed files with 1885 additions and 1681 deletions
@ -1,5 +1,3 @@ |
|||||||
class OrganisationRentPeriod < ApplicationRecord |
class OrganisationRentPeriod < ApplicationRecord |
||||||
belongs_to :organisation |
belongs_to :organisation |
||||||
|
|
||||||
validates :organisation_id, uniqueness: { scope: :rent_period } # rubocop:disable Rails/UniqueValidationWithoutIndex |
|
||||||
end |
end |
||||||
|
@ -1,11 +0,0 @@ |
|||||||
desc "Create OrganisationRentPeriods for organisations that have none as they were previously assumed to have all" |
|
||||||
task create_organisation_rent_periods: :environment do |
|
||||||
org_ids_with_no_associated_rent_periods = Organisation.includes(:organisation_rent_periods).group(:id).count(:organisation_rent_periods).select { |_org_id, period_count| period_count.zero? }.keys |
|
||||||
org_ids_with_no_associated_rent_periods.each do |organisation_id| |
|
||||||
OrganisationRentPeriod.transaction do |
|
||||||
(1..11).each do |rent_period| |
|
||||||
OrganisationRentPeriod.create(organisation_id:, rent_period:) |
|
||||||
end |
|
||||||
end |
|
||||||
end |
|
||||||
end |
|
|
|
|
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,119 +0,0 @@ |
|||||||
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'][name='organisation[rent_periods][]']" |
|
||||||
expect(checkboxes.count).to be > 5 |
|
||||||
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.includes(:organisation_rent_periods).find_by_name(org_name) |
|
||||||
org_rent_periods = org.organisation_rent_periods |
|
||||||
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 |
|
||||||
|
|
||||||
describe "#edit" do |
|
||||||
let(:organisation) { create(:organisation, skip_rent_period_creation: true) } |
|
||||||
let(:fake_rent_periods) do |
|
||||||
{ |
|
||||||
"1" => { "value" => "Every minute" }, |
|
||||||
"2" => { "value" => "Every decade" }, |
|
||||||
} |
|
||||||
end |
|
||||||
let(:checked_rent_period_id) { "1" } |
|
||||||
|
|
||||||
before do |
|
||||||
allow(RentPeriod).to receive(:rent_period_mappings).and_return fake_rent_periods |
|
||||||
create(:organisation_rent_period, organisation:, rent_period: checked_rent_period_id) |
|
||||||
get edit_organisation_path organisation |
|
||||||
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 where appropriate" do |
|
||||||
checkboxes = page.all "input[type='checkbox']" |
|
||||||
expect(checkboxes.count).to be 2 |
|
||||||
expected_checked_checkbox = checkboxes.find { |cb| cb[:value] == checked_rent_period_id } |
|
||||||
expect(expected_checked_checkbox[:checked]).to be true |
|
||||||
expected_not_checked_checkbox = checkboxes.find { |cb| cb[:value] != checked_rent_period_id } |
|
||||||
expect(expected_not_checked_checkbox[:checked]).to be false |
|
||||||
end |
|
||||||
end |
|
||||||
|
|
||||||
describe "#update" do |
|
||||||
let(:organisation) { create(:organisation, skip_rent_period_creation: true) } |
|
||||||
let(:initially_checked_rent_period_id) { "1" } |
|
||||||
let(:initially_unchecked_rent_period_id) { "2" } |
|
||||||
let(:params) do |
|
||||||
{ |
|
||||||
"organisation": { |
|
||||||
name: organisation.name, |
|
||||||
rent_periods: [initially_unchecked_rent_period_id], |
|
||||||
all_rent_periods: [initially_unchecked_rent_period_id, initially_checked_rent_period_id], |
|
||||||
}, |
|
||||||
} |
|
||||||
end |
|
||||||
|
|
||||||
before do |
|
||||||
create(:organisation_rent_period, organisation:, rent_period: initially_checked_rent_period_id) |
|
||||||
end |
|
||||||
|
|
||||||
it "creates and destroys organisation rent periods as appropriate" do |
|
||||||
rent_periods = Organisation.includes(:organisation_rent_periods) |
|
||||||
.find(organisation.id) |
|
||||||
.organisation_rent_periods |
|
||||||
expect(rent_periods.count).to be 1 |
|
||||||
expect(rent_periods.first.rent_period.to_s).to eq initially_checked_rent_period_id |
|
||||||
|
|
||||||
patch organisation_path(organisation, headers:, params:) |
|
||||||
|
|
||||||
rent_periods = Organisation.includes(:organisation_rent_periods) |
|
||||||
.find(organisation.id) |
|
||||||
.organisation_rent_periods |
|
||||||
expect(rent_periods.count).to be 1 |
|
||||||
expect(rent_periods.first.rent_period.to_s).to eq initially_unchecked_rent_period_id |
|
||||||
end |
|
||||||
end |
|
||||||
end |
|
Loading…
Reference in new issue