2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# SameProjectAssociationValidator
|
|
|
|
#
|
|
|
|
# Custom validator to validate that the same project associated with
|
|
|
|
# the record is also associated with the value
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# class ZoomMeeting < ApplicationRecord
|
|
|
|
# belongs_to :project, optional: false
|
|
|
|
# belongs_to :issue, optional: false
|
|
|
|
|
|
|
|
# validates :issue, same_project_association: true
|
|
|
|
# end
|
|
|
|
class SameProjectAssociationValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
return if record.project == value&.project
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
record.errors.add(attribute, 'must associate the same project')
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|