lib/can_play/ability.rb in can_play-0.3.0 vs lib/can_play/ability.rb in can_play-0.3.1
- old
+ new
@@ -1,33 +1,39 @@
class Ability
include CanCan::Ability
attr_accessor :user
def initialize(user)
- self.user = user||CanPlay::Config.user_class_name.constantize.new
- CanPlay::Config.super_roles.each do |role_name|
- can(:manage, :all) if user.send(CanPlay::Config.role_judge_method, role_name)
- end
- CanPlay::Config.role_class_name.constantize.all.each do |role|
- next unless user.send(CanPlay::Config.role_judge_method, role)
- role.send(CanPlay::Config.role_resources_relation_name).each do |role_resource|
- resource = CanPlay.find_by_name_and_code(role_resource.resource_name, CanPlay.override_code)
- next unless resource
- if resource[:type] == 'collection'
- if resource[:behavior]
- block = resource[:behavior]
- can(resource[:verb], resource[:object]) if block.call(user)
- else
- can resource[:verb], resource[:object]
- end
- elsif resource[:type] == 'member'
- if resource[:behavior]
- block = resource[:behavior]
- can resource[:verb], resource[:object] do |object|
- block.call(user, object)
+ CanPlay::Config.tap do |i|
+ self.user = user||i.user_class_name.constantize.new
+ if i.super_roles.is_a?(Array)
+ i.super_roles.each do |role_name|
+ can(:manage, :all) if user.send(i.role_judge_method, role_name)
+ end
+ elsif i.super_roles.is_a?(Proc)
+ can(:manage, :all) if i.super_roles.call(user)
+ end
+ i.role_class_name.constantize.all.each do |role|
+ next unless user.send(i.role_judge_method, role)
+ role.send(i.role_resources_relation_name).each do |role_resource|
+ resource = CanPlay.find_by_name_and_code(role_resource.resource_name, CanPlay.override_code)
+ next unless resource
+ if resource[:type] == 'collection'
+ if resource[:behavior]
+ block = resource[:behavior]
+ can(resource[:verb], resource[:object]) if block.call(user)
+ else
+ can resource[:verb], resource[:object]
end
- else
- can resource[:verb], resource[:object]
+ elsif resource[:type] == 'member'
+ if resource[:behavior]
+ block = resource[:behavior]
+ can resource[:verb], resource[:object] do |object|
+ block.call(user, object)
+ end
+ else
+ can resource[:verb], resource[:object]
+ end
end
end
end
end
end
\ No newline at end of file