adding basic ability module

This commit is contained in:
Ryan Bates
2009-11-16 14:28:52 -08:00
commit 0cfb8c7c41
7 changed files with 184 additions and 0 deletions

2
lib/cancan.rb Normal file
View File

@@ -0,0 +1,2 @@
$:.unshift(File.dirname(__FILE__))
require 'cancan/ability'

39
lib/cancan/ability.rb Normal file
View File

@@ -0,0 +1,39 @@
module CanCan
module Ability
def self.included(base)
base.extend ClassMethods
end
def can?(action, target)
self.class.can_history.reverse.each do |can_action, can_target, can_block|
if can_action == action && (can_target == :all || can_target == target || target.kind_of?(can_target))
if can_block.nil?
return true
else
if can_target == :all
if target.class == Class
return can_block.call(target, nil)
else
return can_block.call(target.class, target)
end
elsif can_target == target
return can_block.call(nil)
else
return can_block.call(target)
end
end
end
end
false
end
module ClassMethods
attr_reader :can_history
def can(action, target, &block)
@can_history ||= []
@can_history << [action, target, block]
end
end
end
end