2016-08-11 18:12:52 -04:00
|
|
|
class BasePolicy
|
2016-08-12 14:36:16 -04:00
|
|
|
def self.abilities(user, subject)
|
|
|
|
new(user, subject).abilities
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :user, :subject
|
2016-08-11 18:12:52 -04:00
|
|
|
def initialize(user, subject)
|
|
|
|
@user = user
|
|
|
|
@subject = subject
|
|
|
|
end
|
|
|
|
|
|
|
|
def abilities
|
2016-08-12 14:36:16 -04:00
|
|
|
return anonymous_abilities if @user.nil?
|
|
|
|
collect_rules { rules }
|
|
|
|
end
|
|
|
|
|
|
|
|
def anonymous_abilities
|
|
|
|
collect_rules { anonymous_rules }
|
2016-08-11 18:12:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate!
|
|
|
|
raise 'abstract'
|
|
|
|
end
|
|
|
|
|
|
|
|
def can!(*rules)
|
|
|
|
@can.merge(rules)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cannot!(*rules)
|
|
|
|
@cannot.merge(rules)
|
|
|
|
end
|
2016-08-12 14:36:16 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def collect_rules(&b)
|
|
|
|
return Set.new if @subject.nil?
|
|
|
|
|
|
|
|
@can = Set.new
|
|
|
|
@cannot = Set.new
|
|
|
|
yield
|
|
|
|
@can - @cannot
|
|
|
|
end
|
2016-08-11 18:12:52 -04:00
|
|
|
end
|