Verified Commit 0c7d8ff1 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

feat(semester_process): add the duplication of the mission relation logic and...

feat(semester_process): add the duplication of the mission relation logic and validation into a concern
parent 7f99224c
module MissionEitherOneRelation
extend ActiveSupport::Concern
included do
# relates to either Assignment or GroupAssignment (not GroupOffer!)
belongs_to :assignment, optional: true
belongs_to :group_assignment, optional: true
validate :validate_group_assignment_or_assignment_present
def mission=(mission)
if mission.class.name == 'Assignment'
self.assignment = mission
else
self.group_assignment = mission
end
end
def mission
group_assignment || assignment
end
private
def validate_group_assignment_or_assignment_present
errors.add(:association_insuficient) if assignment.blank? && group_assignment.blank?
end
end
end
class SemesterFeedback < ApplicationRecord
include MissionEitherOneRelation
belongs_to :author, -> { with_deleted }, class_name: 'User', inverse_of: 'semester_feedbacks'
belongs_to :semester_process_volunteer
has_one :semester_process, through: :semester_process_volunteer
has_one :volunteer, through: :semester_process_volunteer
# relates to either Assignment or GroupAssignment (not GroupOffer!)
belongs_to :assignment, optional: true
belongs_to :group_assignment, optional: true
validate :validate_group_assignment_or_assignment_present
def mission=(mission)
if mission.class.name == 'Assignment'
self.assignment = mission
else
self.group_assignment = mission
end
end
def mission
group_assignment || assignment
end
private
def validate_group_assignment_or_assignment_present
errors.add(:association_insuficient) if assignment.blank? && group_assignment.blank?
end
end
class SemesterProcessVolunteerMission < ApplicationRecord
belongs_to :semester_process_volunteer
# relates to either Assignment or GroupAssignment (not GroupOffer!)
belongs_to :assignment, optional: true
belongs_to :group_assignment, optional: true
validate :validate_group_assignment_or_assignment_present
def mission=(mission)
if mission.class.name == 'Assignment'
self.assignment = mission
else
self.group_assignment = mission
end
end
include MissionEitherOneRelation
def mission
group_assignment || assignment
end
private
def validate_group_assignment_or_assignment_present
errors.add(:association_insuficient) if assignment.blank? && group_assignment.blank?
end
belongs_to :semester_process_volunteer
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment