diff options
author | http://jneen.net/ <jneen@jneen.net> | 2016-08-12 21:36:16 +0300 |
---|---|---|
committer | http://jneen.net/ <jneen@jneen.net> | 2016-08-30 21:39:22 +0300 |
commit | 1ca9b3354a350b83d1e025b3d46280bc5bb60f2b (patch) | |
tree | 02d4eb19aa65547badc63f7d41c5adbd2fe5439b /app/policies/base_policy.rb | |
parent | 29b1623a3615fb7683702f4de2dfeafca10f9c1c (diff) |
add support for anonymous abilities
Diffstat (limited to 'app/policies/base_policy.rb')
-rw-r--r-- | app/policies/base_policy.rb | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/app/policies/base_policy.rb b/app/policies/base_policy.rb index 3f52b0b005a..10ce38329c4 100644 --- a/app/policies/base_policy.rb +++ b/app/policies/base_policy.rb @@ -1,14 +1,21 @@ class BasePolicy + def self.abilities(user, subject) + new(user, subject).abilities + end + + attr_reader :user, :subject def initialize(user, subject) @user = user @subject = subject end def abilities - @can = Set.new - @cannot = Set.new - generate! - @can - @cannot + return anonymous_abilities if @user.nil? + collect_rules { rules } + end + + def anonymous_abilities + collect_rules { anonymous_rules } end def generate! @@ -22,4 +29,15 @@ class BasePolicy def cannot!(*rules) @cannot.merge(rules) end + + private + + def collect_rules(&b) + return Set.new if @subject.nil? + + @can = Set.new + @cannot = Set.new + yield + @can - @cannot + end end |