Skip to content

Commit

Permalink
Merge pull request #9723 from DFE-Digital/cv/use-breakable-column
Browse files Browse the repository at this point in the history
Use ApplicationWorkHistoryBreak#breakable column
  • Loading branch information
CatalinVoineag authored Aug 20, 2024
2 parents 38ea74f + 8f7639d commit 62d1402
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApplicationForm < ApplicationRecord
has_many :application_volunteering_experiences, as: :experienceable
has_many :application_qualifications
has_many :application_references
has_many :application_work_history_breaks
has_many :application_work_history_breaks, as: :breakable
has_many :emails

belongs_to :previous_application_form, class_name: 'ApplicationForm', optional: true, inverse_of: 'subsequent_application_form'
Expand Down
11 changes: 8 additions & 3 deletions app/models/application_work_history_break.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class ApplicationWorkHistoryBreak < ApplicationRecord
include TouchApplicationChoices

belongs_to :application_form, touch: true
belongs_to :breakable, polymorphic: true, optional: true
belongs_to :application_form, touch: true, optional: true
belongs_to :breakable, polymorphic: true

before_save -> { self.breakable = application_form }, if: -> { breakable.nil? }
before_save -> { self.application_form_id = breakable_id }, if: -> { application_form_id.nil? }

audited associated_with: :application_form

def application_form=(value)
super
self.breakable = value
end

def length
((end_date.year * 12) + end_date.month) - ((start_date.year * 12) + start_date.month) - 1
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/application_work_history_break_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rails_helper'

RSpec.describe ApplicationWorkHistoryBreak do
it { is_expected.to belong_to(:application_form).touch(true) }
it { is_expected.to belong_to(:breakable).optional }
it { is_expected.to belong_to(:application_form).touch(true).optional }
it { is_expected.to belong_to(:breakable) }

describe 'auditing', :with_audited do
it { is_expected.to be_audited.associated_with :application_form }
Expand Down

0 comments on commit 62d1402

Please sign in to comment.