diff --git a/app/controllers/legislation/processes_controller.rb b/app/controllers/legislation/processes_controller.rb index ad33000b0..7aff709d0 100644 --- a/app/controllers/legislation/processes_controller.rb +++ b/app/controllers/legislation/processes_controller.rb @@ -8,10 +8,12 @@ class Legislation::ProcessesController < Legislation::BaseController end def show - if @process.show_phase?(:allegations) && draft_version = @process.draft_versions.published.last + if @process.active_phase?(:allegations) && @process.show_phase?(:allegations) && draft_version = @process.draft_versions.published.last redirect_to legislation_process_draft_version_path(@process, draft_version) - else + elsif @process.active_phase?(:debate) redirect_to legislation_process_debate_path(@process) + else + redirect_to legislation_process_allegations_path(@process) end end diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 489dccffb..a68e667e3 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -9,12 +9,10 @@ class Legislation::Process < ActiveRecord::Base validates :title, presence: true validates :start_date, presence: true validates :end_date, presence: true - validates :debate_start_date, presence: true - validates :debate_end_date, presence: true - validates :draft_publication_date, presence: true - validates :allegations_start_date, presence: true - validates :allegations_end_date, presence: true - validates :final_publication_date, presence: true + validates :debate_start_date, presence: true, if: :debate_end_date? + validates :debate_end_date, presence: true, if: :debate_start_date? + validates :allegations_start_date, presence: true, if: :allegations_end_date? + validates :allegations_end_date, presence: true, if: :allegations_start_date? validate :valid_date_ranges scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current).order('id DESC') } @@ -26,13 +24,13 @@ class Legislation::Process < ActiveRecord::Base case phase when :debate - today >= debate_start_date && today <= debate_end_date + active_phase?(:debate) && today >= debate_start_date && today <= debate_end_date when :draft_publication - today >= draft_publication_date + active_phase?(:draft_publication) && today >= draft_publication_date when :allegations - today >= allegations_start_date && today <= allegations_end_date + active_phase?(:allegations) && today >= allegations_start_date && today <= allegations_end_date when :final_version_publication - today >= final_publication_date + active_phase?(:final_version_publication) && today >= final_publication_date end end @@ -42,13 +40,26 @@ class Legislation::Process < ActiveRecord::Base case phase when :debate - today >= debate_start_date + active_phase?(:debate) && today >= debate_start_date when :draft_publication - today >= draft_publication_date + active_phase?(:draft_publication) && today >= draft_publication_date when :allegations - today >= allegations_start_date + active_phase?(:allegations) && today >= allegations_start_date when :final_version_publication - today >= final_publication_date + active_phase?(:final_version_publication) && today >= final_publication_date + end + end + + def active_phase?(phase) + case phase + when :debate + debate_start_date.present? && debate_end_date.present? + when :draft_publication + draft_publication_date.present? + when :allegations + allegations_start_date.present? && allegations_end_date.present? + when :final_version_publication + final_publication_date.present? end end diff --git a/app/views/legislation/processes/_key_dates.html.erb b/app/views/legislation/processes/_key_dates.html.erb index 7e53f7097..3e5823ae2 100644 --- a/app/views/legislation/processes/_key_dates.html.erb +++ b/app/views/legislation/processes/_key_dates.html.erb @@ -3,30 +3,41 @@ diff --git a/spec/models/legislation/process_spec.rb b/spec/models/legislation/process_spec.rb index fd83d8989..f89a4bfcf 100644 --- a/spec/models/legislation/process_spec.rb +++ b/spec/models/legislation/process_spec.rb @@ -7,6 +7,32 @@ RSpec.describe Legislation::Process, type: :model do expect(process).to be_valid end + describe "dates validations" do + it "is invalid if debate_start_date is present but debate_end_date is not" do + process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: "") + expect(process).to be_invalid + expect(process.errors.messages[:debate_end_date]).to include("can't be blank") + end + + it "is invalid if debate_end_date is present but debate_start_date is not" do + process = build(:legislation_process, debate_start_date: nil, debate_end_date: Date.current) + expect(process).to be_invalid + expect(process.errors.messages[:debate_start_date]).to include("can't be blank") + end + + it "is invalid if allegations_start_date is present but debate_end_date is not" do + process = build(:legislation_process, allegations_start_date: Date.current, allegations_end_date: "") + expect(process).to be_invalid + expect(process.errors.messages[:allegations_end_date]).to include("can't be blank") + end + + it "is invalid if debate_end_date is present but allegations_start_date is not" do + process = build(:legislation_process, allegations_start_date: nil, allegations_end_date: Date.current) + expect(process).to be_invalid + expect(process.errors.messages[:allegations_start_date]).to include("can't be blank") + end + end + describe "date ranges validations" do it "is invalid if end_date is before start_date" do process = build(:legislation_process, start_date: Date.current, end_date: Date.current - 1.day)