summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-07-02 17:02:59 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-07-25 11:26:22 +0200
commite13d75c38a09fca98dfbb52ef94119770b7a445a (patch)
treedb8276fd43875b41d52cb1a16f581511477757f4
parentd964816b9fe56679ffc0b331e701f7b24db5c6a9 (diff)
downloadgitlab-ce-bvl-port-inverse-protected-ref-to-ce.tar.gz
Explicitly define inverse of acces_level relationsbvl-port-inverse-protected-ref-to-ce
-rw-r--r--app/models/concerns/protected_ref.rb8
1 files changed, 7 insertions, 1 deletions
diff --git a/app/models/concerns/protected_ref.rb b/app/models/concerns/protected_ref.rb
index fc6b840f7a8..5dd43c36222 100644
--- a/app/models/concerns/protected_ref.rb
+++ b/app/models/concerns/protected_ref.rb
@@ -17,7 +17,13 @@ module ProtectedRef
class_methods do
def protected_ref_access_levels(*types)
types.each do |type|
- has_many :"#{type}_access_levels", dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
+ # We need to set `inverse_of` to make sure the `belongs_to`-object is set
+ # when creating children using `accepts_nested_attributes_for`.
+ #
+ # If we don't `protected_branch` or `protected_tag` would be empty and
+ # `project` cannot be delegated to it, which in turn would cause validations
+ # to fail.
+ has_many :"#{type}_access_levels", dependent: :destroy, inverse_of: self.model_name.singular # rubocop:disable Cop/ActiveRecordDependent
validates :"#{type}_access_levels", length: { is: 1, message: "are restricted to a single instance per #{self.model_name.human}." }