canable 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -4
- data/lib/canable.rb +10 -3
- data/test/helper.rb +17 -21
- data/test/test_canable.rb +5 -0
- data/test/test_enforcers.rb +15 -0
- metadata +47 -28
data/Rakefile
CHANGED
@@ -13,11 +13,10 @@ begin
|
|
13
13
|
gem.homepage = "http://github.com/jnunemaker/canable"
|
14
14
|
gem.authors = ["John Nunemaker"]
|
15
15
|
gem.version = Canable::Version
|
16
|
-
gem.add_development_dependency "shoulda", "2.10.2"
|
17
|
-
gem.add_development_dependency "
|
18
|
-
gem.add_development_dependency "
|
16
|
+
gem.add_development_dependency "shoulda", "~> 2.10.2"
|
17
|
+
gem.add_development_dependency "mocha", "~> 0.9.8"
|
18
|
+
gem.add_development_dependency "mongo_mapper"
|
19
19
|
gem.add_development_dependency "yard", ">= 0"
|
20
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
21
20
|
end
|
22
21
|
Jeweler::GemcutterTasks.new
|
23
22
|
rescue LoadError
|
data/lib/canable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Canable
|
2
|
-
Version = '0.1'
|
2
|
+
Version = '0.1.1'
|
3
3
|
|
4
4
|
# Module that holds all the can_action? methods.
|
5
5
|
module Cans; end
|
@@ -11,8 +11,7 @@ module Canable
|
|
11
11
|
module Enforcers
|
12
12
|
def self.included(controller)
|
13
13
|
controller.class_eval do
|
14
|
-
Canable.
|
15
|
-
delegate "can_#{can}?", :to => :current_user
|
14
|
+
Canable.cans.each do |can|
|
16
15
|
helper_method "can_#{can}?" if controller.respond_to?(:helper_method)
|
17
16
|
hide_action "can_#{can}?" if controller.respond_to?(:hide_action)
|
18
17
|
end
|
@@ -32,6 +31,10 @@ module Canable
|
|
32
31
|
@actions
|
33
32
|
end
|
34
33
|
|
34
|
+
def self.cans
|
35
|
+
actions.keys
|
36
|
+
end
|
37
|
+
|
35
38
|
# Adds an action to actions and the correct methods to can and able modules.
|
36
39
|
#
|
37
40
|
# @param [Symbol] can_method The name of the can_[action]? method.
|
@@ -63,6 +66,10 @@ module Canable
|
|
63
66
|
|
64
67
|
def self.add_enforcer_method(can)
|
65
68
|
Enforcers.module_eval <<-EOM
|
69
|
+
def can_#{can}?(resource)
|
70
|
+
current_user.can_#{can}?(resource)
|
71
|
+
end
|
72
|
+
|
66
73
|
def enforce_#{can}_permission(resource)
|
67
74
|
raise Canable::Transgression unless can_#{can}?(resource)
|
68
75
|
end
|
data/test/helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
|
3
|
-
gem 'mocha', '0.9.8'
|
4
|
-
gem 'shoulda', '2.10.2'
|
5
|
-
gem 'mongo_mapper'
|
3
|
+
gem 'mocha', '~> 0.9.8'
|
4
|
+
gem 'shoulda', '~> 2.10.2'
|
5
|
+
gem 'mongo_mapper'
|
6
6
|
|
7
7
|
require 'mocha'
|
8
8
|
require 'shoulda'
|
@@ -13,26 +13,22 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
13
13
|
require 'canable'
|
14
14
|
|
15
15
|
class Test::Unit::TestCase
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
class_eval "def self.to_s; '#{name}' end"
|
16
|
+
def Doc(name=nil, &block)
|
17
|
+
klass = Class.new do
|
18
|
+
include MongoMapper::Document
|
19
|
+
set_collection_name "test#{rand(20)}"
|
20
|
+
|
21
|
+
if name
|
22
|
+
class_eval "def self.name; '#{name}' end"
|
23
|
+
class_eval "def self.to_s; '#{name}' end"
|
24
|
+
end
|
26
25
|
end
|
27
|
-
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
klass.class_eval(&block) if block_given?
|
28
|
+
klass.collection.remove
|
29
|
+
klass
|
30
|
+
end
|
32
31
|
end
|
33
32
|
|
34
|
-
|
35
|
-
FileUtils.mkdir_p(test_dir) unless File.exist?(test_dir)
|
36
|
-
|
37
|
-
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, {:logger => Logger.new(test_dir + '/test.log')})
|
33
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
|
38
34
|
MongoMapper.database = 'test'
|
data/test/test_canable.rb
CHANGED
@@ -22,5 +22,10 @@ class TestCanable < Test::Unit::TestCase
|
|
22
22
|
Canable.add(:publish, :publishable)
|
23
23
|
assert_equal :publishable, Canable.actions[:publish]
|
24
24
|
end
|
25
|
+
|
26
|
+
should "know cans" do
|
27
|
+
assert_equal %w(create destroy publish update view),
|
28
|
+
Canable.cans.map(&:to_s).sort
|
29
|
+
end
|
25
30
|
end
|
26
31
|
end
|
data/test/test_enforcers.rb
CHANGED
@@ -7,9 +7,19 @@ class EnforcersTest < Test::Unit::TestCase
|
|
7
7
|
include Canable::Enforcers
|
8
8
|
attr_accessor :current_user, :article
|
9
9
|
|
10
|
+
# Overriding example
|
11
|
+
def can_update?(resource)
|
12
|
+
return false if current_user.nil?
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
10
16
|
def show
|
11
17
|
enforce_view_permission(article)
|
12
18
|
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
enforce_update_permission(article)
|
22
|
+
end
|
13
23
|
end
|
14
24
|
|
15
25
|
@article = mock('article')
|
@@ -28,5 +38,10 @@ class EnforcersTest < Test::Unit::TestCase
|
|
28
38
|
@user.expects(:can_view?).with(@article).returns(false)
|
29
39
|
assert_raises(Canable::Transgression) { @controller.show }
|
30
40
|
end
|
41
|
+
|
42
|
+
should "be able to override can_xx? method" do
|
43
|
+
@controller.current_user = nil
|
44
|
+
assert_raises(Canable::Transgression) { @controller.update }
|
45
|
+
end
|
31
46
|
end
|
32
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- John Nunemaker
|
@@ -9,49 +14,61 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-06-23 00:00:00 -04:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: shoulda
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
|
-
- -
|
25
|
+
- - ~>
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 10
|
30
|
+
- 2
|
23
31
|
version: 2.10.2
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: mongo_mapper
|
27
32
|
type: :development
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: "0.7"
|
34
|
-
version:
|
33
|
+
version_requirements: *id001
|
35
34
|
- !ruby/object:Gem::Dependency
|
36
35
|
name: mocha
|
37
|
-
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
38
|
requirements:
|
41
|
-
- -
|
39
|
+
- - ~>
|
42
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 9
|
44
|
+
- 8
|
43
45
|
version: 0.9.8
|
44
|
-
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
45
48
|
- !ruby/object:Gem::Dependency
|
46
|
-
name:
|
49
|
+
name: mongo_mapper
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
47
58
|
type: :development
|
48
|
-
|
49
|
-
|
59
|
+
version_requirements: *id003
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: yard
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
64
|
requirements:
|
51
65
|
- - ">="
|
52
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
53
69
|
version: "0"
|
54
|
-
|
70
|
+
type: :development
|
71
|
+
version_requirements: *id004
|
55
72
|
description: Simple permissions that I have used on my last several projects so I figured it was time to abstract and wrap up into something more easily reusable.
|
56
73
|
email: nunemaker@gmail.com
|
57
74
|
executables: []
|
@@ -87,18 +104,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
104
|
requirements:
|
88
105
|
- - ">="
|
89
106
|
- !ruby/object:Gem::Version
|
107
|
+
segments:
|
108
|
+
- 0
|
90
109
|
version: "0"
|
91
|
-
version:
|
92
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
111
|
requirements:
|
94
112
|
- - ">="
|
95
113
|
- !ruby/object:Gem::Version
|
114
|
+
segments:
|
115
|
+
- 0
|
96
116
|
version: "0"
|
97
|
-
version:
|
98
117
|
requirements: []
|
99
118
|
|
100
119
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.3.
|
120
|
+
rubygems_version: 1.3.6
|
102
121
|
signing_key:
|
103
122
|
specification_version: 3
|
104
123
|
summary: Simple permissions that I have used on my last several projects so I figured it was time to abstract and wrap up into something more easily reusable.
|