Skip to content

Commit 8c5c778

Browse files
thomasleesetvararu
authored andcommitted
Add Draftable concern
This adds a new concern to encapsulate the logic around models which have an active field, and two states of either draft or active.
1 parent ff43986 commit 8c5c778

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

app/models/campaign.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# fk_rails_... (team_id => teams.id)
2525
#
2626
class Campaign < ApplicationRecord
27+
include Draftable
2728
include WizardStepConcern
2829

2930
self.inheritance_column = nil
@@ -44,8 +45,6 @@ class Campaign < ApplicationRecord
4445

4546
enum :type, { flu: "flu", hpv: "hpv" }, validate: { allow_nil: true }
4647

47-
scope :active, -> { where(active: true) }
48-
4948
normalizes :name, with: ->(name) { name&.strip }
5049

5150
on_wizard_step :details do

app/models/concerns/draftable.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Draftable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
scope :active, -> { where(active: true) }
8+
scope :draft, -> { where(active: false) }
9+
end
10+
11+
def draft?
12+
!active
13+
end
14+
end

app/models/patient_session.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#
2525

2626
class PatientSession < ApplicationRecord
27+
include Draftable
28+
2729
audited
2830
has_associated_audits
2931

@@ -50,12 +52,6 @@ class PatientSession < ApplicationRecord
5052
through: :patient,
5153
class_name: "Consent"
5254

53-
scope :active, -> { where(active: true) }
54-
55-
def draft?
56-
!active
57-
end
58-
5955
def vaccination_record
6056
# HACK: in future, it will be possible to have multiple vaccination records for a patient session
6157
vaccination_records.recorded.last

app/models/session.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
# fk_rails_... (imported_from_id => immunisation_imports.id)
2828
#
2929
class Session < ApplicationRecord
30+
include Draftable
3031
include WizardStepConcern
32+
3133
audited
3234

3335
DEFAULT_DAYS_FOR_REMINDER = 2
@@ -46,20 +48,14 @@ class Session < ApplicationRecord
4648

4749
enum :time_of_day, %w[morning afternoon all_day]
4850

49-
scope :active, -> { where(active: true) }
50-
scope :draft, -> { where(active: false) }
5151
scope :past, -> { where(date: ..Time.zone.yesterday) }
5252
scope :in_progress, -> { where(date: Time.zone.today) }
5353
scope :future, -> { where(date: Time.zone.tomorrow..) }
5454

5555
after_initialize :set_timeline_attributes
5656
after_validation :set_timeline_timestamps
5757

58-
validates :time_of_day,
59-
inclusion: {
60-
in: time_of_days.keys
61-
},
62-
unless: -> { draft? }
58+
validates :time_of_day, inclusion: { in: time_of_days.keys }, unless: :draft?
6359

6460
on_wizard_step :location, exact: true do
6561
validates :location_id, presence: true
@@ -98,10 +94,6 @@ class Session < ApplicationRecord
9894
if: -> { close_consent_on == "custom" }
9995
end
10096

101-
def draft?
102-
!active
103-
end
104-
10597
def health_questions
10698
campaign.vaccines.first.health_questions
10799
end

spec/factories/patient_sessions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
patient { association :patient, session:, **patient_attributes }
3434
created_by { association :user }
3535

36-
active { session.active? }
36+
active { session.active }
3737

3838
trait :active do
3939
active { true }

0 commit comments

Comments
 (0)