Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/validators/same_project_association_validator.rb')
-rw-r--r--app/validators/same_project_association_validator.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/app/validators/same_project_association_validator.rb b/app/validators/same_project_association_validator.rb
new file mode 100644
index 00000000000..2af2a21fa9a
--- /dev/null
+++ b/app/validators/same_project_association_validator.rb
@@ -0,0 +1,21 @@
+# 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
+
+ record.errors[attribute] << 'must associate the same project'
+ end
+end