summaryrefslogtreecommitdiff
path: root/app/policies
diff options
context:
space:
mode:
authorhttp://jneen.net/ <jneen@jneen.net>2016-08-16 12:55:44 -0700
committerhttp://jneen.net/ <jneen@jneen.net>2016-08-30 11:39:22 -0700
commit3656d3b88a01a50a5eaf66a16b6ac47d3c58352c (patch)
tree4382c67269cd684322f496348756bb63410add6e /app/policies
parent16fe6dc7b159a0e6b68a586065de1f95d6acecfa (diff)
downloadgitlab-ce-3656d3b88a01a50a5eaf66a16b6ac47d3c58352c.tar.gz
add automatic detection of the policy class
Diffstat (limited to 'app/policies')
-rw-r--r--app/policies/base_policy.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/app/policies/base_policy.rb b/app/policies/base_policy.rb
index e1757d97e89..12f60d8f76e 100644
--- a/app/policies/base_policy.rb
+++ b/app/policies/base_policy.rb
@@ -4,7 +4,21 @@ class BasePolicy
end
def self.class_for(subject)
- "#{subject.class.name}Policy".constantize
+ subject.class.ancestors.each do |klass|
+ next unless klass.name
+
+ begin
+ policy_class = "#{klass.name}Policy".constantize
+
+ # NB: the < operator here tests whether policy_class
+ # inherits from BasePolicy
+ return policy_class if policy_class < BasePolicy
+ rescue NameError
+ nil
+ end
+ end
+
+ raise "no policy for #{subject.class.name}"
end
attr_reader :user, :subject