Skip to content
Browse files

Controller can_xxx? methods are now in a module so you can override a…

…nd use super.
  • Loading branch information...
1 parent 27c8f8a commit 96950ecbc049421c4132e36553a3fd022da1f509 @jnunemaker committed Jun 23, 2010
Showing with 49 additions and 27 deletions.
  1. +3 −4 Rakefile
  2. +9 −2 lib/canable.rb
  3. +17 −21 test/helper.rb
  4. +5 −0 test/test_canable.rb
  5. +15 −0 test/test_enforcers.rb
View
7 Rakefile
@@ -13,11 +13,10 @@ begin
gem.homepage = "http://github.com/jnunemaker/canable"
gem.authors = ["John Nunemaker"]
gem.version = Canable::Version
- gem.add_development_dependency "shoulda", "2.10.2"
- gem.add_development_dependency "mongo_mapper", "0.7"
- gem.add_development_dependency "mocha", "0.9.8"
+ gem.add_development_dependency "shoulda", "~> 2.10.2"
+ gem.add_development_dependency "mocha", "~> 0.9.8"
+ gem.add_development_dependency "mongo_mapper"
gem.add_development_dependency "yard", ">= 0"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
11 lib/canable.rb
@@ -11,8 +11,7 @@ module Ables; end
module Enforcers
def self.included(controller)
controller.class_eval do
- Canable.actions.each do |can, able|
- delegate "can_#{can}?", :to => :current_user
+ Canable.cans.each do |can|
helper_method "can_#{can}?" if controller.respond_to?(:helper_method)
hide_action "can_#{can}?" if controller.respond_to?(:hide_action)
end
@@ -32,6 +31,10 @@ def self.actions
@actions
end
+ def self.cans
+ actions.keys
+ end
+
# Adds an action to actions and the correct methods to can and able modules.
#
# @param [Symbol] can_method The name of the can_[action]? method.
@@ -63,6 +66,10 @@ def #{able}_by?(user)
def self.add_enforcer_method(can)
Enforcers.module_eval <<-EOM
+ def can_#{can}?(resource)
+ current_user.can_#{can}?(resource)
+ end
+
def enforce_#{can}_permission(resource)
raise Canable::Transgression unless can_#{can}?(resource)
end
View
38 test/helper.rb
@@ -1,8 +1,8 @@
require 'test/unit'
-gem 'mocha', '0.9.8'
-gem 'shoulda', '2.10.2'
-gem 'mongo_mapper', '0.7'
+gem 'mocha', '~> 0.9.8'
+gem 'shoulda', '~> 2.10.2'
+gem 'mongo_mapper'
require 'mocha'
require 'shoulda'
@@ -13,26 +13,22 @@
require 'canable'
class Test::Unit::TestCase
-end
-
-def Doc(name=nil, &block)
- klass = Class.new do
- include MongoMapper::Document
- set_collection_name "test#{rand(20)}"
-
- if name
- class_eval "def self.name; '#{name}' end"
- class_eval "def self.to_s; '#{name}' end"
+ def Doc(name=nil, &block)
+ klass = Class.new do
+ include MongoMapper::Document
+ set_collection_name "test#{rand(20)}"
+
+ if name
+ class_eval "def self.name; '#{name}' end"
+ class_eval "def self.to_s; '#{name}' end"
+ end
end
- end
- klass.class_eval(&block) if block_given?
- klass.collection.remove
- klass
+ klass.class_eval(&block) if block_given?
+ klass.collection.remove
+ klass
+ end
end
-test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
-FileUtils.mkdir_p(test_dir) unless File.exist?(test_dir)
-
-MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {:logger => Logger.new(test_dir + '/test.log')})
+MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = 'test'
View
5 test/test_canable.rb
@@ -22,5 +22,10 @@ class TestCanable < Test::Unit::TestCase
Canable.add(:publish, :publishable)
assert_equal :publishable, Canable.actions[:publish]
end
+
+ should "know cans" do
+ assert_equal %w(create destroy publish update view),
+ Canable.cans.map(&:to_s).sort
+ end
end
end
View
15 test/test_enforcers.rb
@@ -7,9 +7,19 @@ class EnforcersTest < Test::Unit::TestCase
include Canable::Enforcers
attr_accessor :current_user, :article
+ # Overriding example
+ def can_update?(resource)
+ return false if current_user.nil?
+ super
+ end
+
def show
enforce_view_permission(article)
end
+
+ def update
+ enforce_update_permission(article)
+ end
end
@article = mock('article')
@@ -28,5 +38,10 @@ def show
@user.expects(:can_view?).with(@article).returns(false)
assert_raises(Canable::Transgression) { @controller.show }
end
+
+ should "be able to override can_xx? method" do
+ @controller.current_user = nil
+ assert_raises(Canable::Transgression) { @controller.update }
+ end
end
end

0 comments on commit 96950ec

Please sign in to comment.
Something went wrong with that request. Please try again.