lib/abilities/definitions.rb in abilities-0.0.1 vs lib/abilities/definitions.rb in abilities-0.0.2
- old
+ new
@@ -9,18 +9,21 @@
def add(actions, subjects, behavior, &block)
actions = [actions] unless actions.is_a? Array
subjects = [subjects] unless subjects.is_a? Array
subjects.each do |subject|
actions.each do |action|
- (all[find_subject_class(subject)] ||= {})[action.to_s] = block_given? ? block : behavior
+ (all[find_subject_id(subject)] ||= {})[action.to_s] = block_given? ? block : behavior
end
end
end
def can?(action, subject)
- if actions = all[find_subject_class(subject)]
- if behavior = actions[action.to_s]
+ subject_id = find_subject_id(subject)
+ if subject_id != 'all' and can?(action, 'all')
+ true
+ elsif actions = all[subject_id]
+ if behavior = (actions[action.to_s] || actions['manage'])
if behavior.is_a? Proc
@actor.instance_exec subject, &behavior
else
behavior
end
@@ -40,11 +43,13 @@
def all
@all ||= {}
end
- def find_subject_class(subject)
- if subject.is_a? Class
+ def find_subject_id(subject)
+ if subject.to_s == 'all'
+ subject.to_s
+ elsif subject.is_a? Class
subject.name
else
subject.class.name
end
end