Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate VisitGroup day order coming out of Step 2 [#128321943] #774

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ServiceRequest < ActiveRecord::Base
has_many :tokens, :dependent => :destroy
has_many :approvals, :dependent => :destroy
has_many :arms, :through => :protocol
has_many :visit_groups, through: :arms
has_many :notes, as: :notable, dependent: :destroy

after_save :set_original_submitted_date
Expand Down Expand Up @@ -112,6 +113,11 @@ def validate_arms
end

def validate_service_calendar
vg = visit_groups.to_a.find { |vg| !vg.in_order? }
if vg
errors.add(:base, I18n.t('errors.visit_groups.days_out_of_order', arm_name: vg.arm.name))
end

if USE_EPIC
self.arms.each do |arm|
days = arm.visit_groups.map(&:day)
Expand Down
20 changes: 14 additions & 6 deletions app/models/visit_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class VisitGroup < ActiveRecord::Base
include Comparable

audited

attr_accessible :name
attr_accessible :position
attr_accessible :arm_id
Expand All @@ -49,6 +49,10 @@ class VisitGroup < ActiveRecord::Base
:window_before,
:window_after,
presence: true, numericality: { only_integer: true }

# TODO: fix. Currently, this validation fails for all VisitGroups with
# position == 0. This fails because the position attribute is changed
# to 1 from 0 by, I think, acts_as_list before validations are performed.
validate :day_must_be_in_order

def set_arm_edited_flag_on_subjects
Expand Down Expand Up @@ -79,9 +83,16 @@ def any_visit_quantities_customized?(service_request)
visits.any? { |visit| ((visit.quantities_customized?) && (visit.line_items_visit.line_item.service_request_id == service_request.id)) }
end

# TODO: remove after day_must_be_in_order validation is fixed.
def in_order?
position_col = VisitGroup.arel_table[:position]
day_col = VisitGroup.arel_table[:day]
arm.visit_groups.where(position_col.lt(position).and(day_col.gteq(day)).or(
position_col.gt(position).and(day_col.lteq(day)))).none?
end

private

def remove_appointments
appointments = self.appointments
appointments.each do |app|
Expand All @@ -94,10 +105,7 @@ def remove_appointments
end

def day_must_be_in_order
position_col = VisitGroup.arel_table[:position]
day_col = VisitGroup.arel_table[:day]
if arm.visit_groups.where(position_col.lt(position).and(day_col.gteq(day)).or(
position_col.gt(position).and(day_col.lteq(day)))).any?
unless in_order?
errors.add(:day, 'must be in order')
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ en:
displayed_cost_gte_zero: 'must be greater than or equal to 0'
line_items_visits:
subject_count_invalid: "Subject Count cannot exceed the Arm Subject Count (%{arm_subject_count})"
visit_groups:
days_out_of_order: "Please make sure visit days are in sequential order (%{arm_name})."
protocols:
start_date_missing: "Start Date can\'t be blank"
end_date_missing: "End Date can\'t be blank"
Expand Down