diff options
author | http://jneen.net/ <jneen@jneen.net> | 2016-08-16 22:55:44 +0300 |
---|---|---|
committer | http://jneen.net/ <jneen@jneen.net> | 2016-08-30 21:39:22 +0300 |
commit | 3656d3b88a01a50a5eaf66a16b6ac47d3c58352c (patch) | |
tree | 4382c67269cd684322f496348756bb63410add6e /app/policies/base_policy.rb | |
parent | 16fe6dc7b159a0e6b68a586065de1f95d6acecfa (diff) |
add automatic detection of the policy class
Diffstat (limited to 'app/policies/base_policy.rb')
-rw-r--r-- | app/policies/base_policy.rb | 16 |
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 |