cantango-api 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -5
- data/Gemfile.lock +20 -24
- data/README.mdown +73 -0
- data/VERSION +1 -1
- data/cantango-api.gemspec +42 -23
- data/lib/cantango/api/ability/account.rb +6 -6
- data/lib/cantango/api/ability/user.rb +1 -0
- data/lib/cantango/api/account.rb +9 -11
- data/lib/cantango/api/attributes.rb +1 -1
- data/lib/cantango/api/can/account.rb +2 -2
- data/lib/cantango/api/can/dsl/relation.rb +67 -0
- data/lib/cantango/api/can/dsl/scope.rb +24 -0
- data/lib/cantango/api/can/dsl.rb +26 -0
- data/lib/cantango/api/can/user.rb +1 -1
- data/lib/cantango/api/can.rb +1 -1
- data/lib/cantango/api/common.rb +16 -3
- data/lib/cantango/api/model/account.rb +9 -16
- data/lib/cantango/api/model/user.rb +10 -17
- data/lib/cantango/api/model.rb +3 -1
- data/lib/cantango/api/scope/account.rb +8 -5
- data/lib/cantango/api/scope/user.rb +1 -0
- data/lib/cantango/api/session/account.rb +1 -1
- data/lib/cantango/api/user.rb +9 -11
- data/lib/cantango/api.rb +7 -4
- data/lib/cantango/api_ext/macros/account.rb +8 -0
- data/lib/cantango/api_ext/macros/clazz.rb +17 -0
- data/lib/cantango/api_ext/macros/user.rb +8 -0
- data/lib/cantango/api_ext/macros.rb +12 -0
- data/lib/cantango/api_ext.rb +5 -0
- data/spec/cantango/api/ability/account_spec.rb +14 -15
- data/spec/cantango/api/ability/user_spec.rb +18 -22
- data/spec/cantango/api/account_spec.rb +40 -0
- data/spec/cantango/api/attributes_spec.rb +13 -14
- data/spec/cantango/api/can/account_spec.rb +40 -47
- data/spec/cantango/api/can/user_spec.rb +56 -83
- data/spec/cantango/api/common_spec.rb +37 -0
- data/spec/cantango/api/model/account_spec.rb +38 -0
- data/spec/cantango/api/model/user_spec.rb +35 -0
- data/spec/cantango/api/options_spec.rb +23 -0
- data/spec/cantango/api/scope/account_spec.rb +43 -0
- data/spec/cantango/api/scope/user_spec.rb +19 -51
- data/spec/cantango/api/session/account_spec.rb +0 -0
- data/spec/cantango/api/session/user_spec.rb +0 -0
- data/spec/cantango/api/user_spec.rb +40 -0
- data/spec/cantango/api_ext/macros/account_spec.rb +16 -0
- data/spec/cantango/api_ext/macros/clazz_spec.rb +26 -0
- data/spec/cantango/api_ext/macros/user_spec.rb +17 -0
- data/spec/fixtures/models/user_account.rb +1 -15
- data/spec/helpers/current_user_accounts.rb +1 -1
- data/spec/spec_helper.rb +0 -1
- metadata +55 -28
data/Gemfile
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
3
|
group :default do
|
4
|
-
gem 'rails',
|
5
|
-
gem 'sugar-high', '
|
6
|
-
gem 'sweetloader', '~> 0.1.
|
7
|
-
gem 'hashie'
|
4
|
+
gem 'rails', '>= 3.1'
|
5
|
+
gem 'sugar-high', '~> 0.6.2'
|
6
|
+
gem 'sweetloader', '~> 0.1.6'
|
7
|
+
gem 'hashie', '~> 1.2'
|
8
8
|
|
9
|
-
gem 'cantango-config',
|
9
|
+
gem 'cantango-config', '>= 0.1.8.1'
|
10
|
+
gem 'cantango-core', '>= 0.1.9.1'
|
10
11
|
end
|
11
12
|
|
12
13
|
group :development do
|
@@ -18,6 +19,8 @@ end
|
|
18
19
|
group :test do
|
19
20
|
gem 'require_all', '~> 1.2.0'
|
20
21
|
|
22
|
+
# gem 'cantango-masquerade', :git => 'git://github.com/kristianmandrup/cantango-masquerade.git'
|
23
|
+
|
21
24
|
gem 'forgery'
|
22
25
|
gem 'sqlite3'
|
23
26
|
# Data
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,3 @@
|
|
1
|
-
GIT
|
2
|
-
remote: git://github.com/kristianmandrup/cantango-config.git
|
3
|
-
revision: 852141acab7fec0eb61930bdda07c16461140a7e
|
4
|
-
specs:
|
5
|
-
cantango-config (0.1.0)
|
6
|
-
cantango-core
|
7
|
-
hashie
|
8
|
-
rails (>= 3.1)
|
9
|
-
sugar-high (>= 0.6.0)
|
10
|
-
sweetloader (~> 0.1.0)
|
11
|
-
|
12
1
|
GEM
|
13
2
|
remote: http://rubygems.org/
|
14
3
|
specs:
|
@@ -43,12 +32,18 @@ GEM
|
|
43
32
|
arel (2.2.1)
|
44
33
|
builder (3.0.0)
|
45
34
|
cancan (1.6.7)
|
46
|
-
cantango-
|
35
|
+
cantango-config (0.1.9.2)
|
36
|
+
cantango-core (~> 0.1.9)
|
37
|
+
hashie (~> 1.2)
|
38
|
+
rails (>= 3.1)
|
39
|
+
sugar-high (>= 0.6.1)
|
40
|
+
sweetloader (~> 0.1.6)
|
41
|
+
cantango-core (0.1.9.2)
|
47
42
|
cancan (>= 1.4)
|
48
|
-
hashie
|
43
|
+
hashie (~> 1.2)
|
49
44
|
rails (>= 3.0.1)
|
50
45
|
sugar-high (>= 0.6.0)
|
51
|
-
sweetloader (~> 0.1.
|
46
|
+
sweetloader (~> 0.1.5)
|
52
47
|
colorize (0.5.8)
|
53
48
|
cutter (0.8.2)
|
54
49
|
activesupport (>= 2.3.5)
|
@@ -56,7 +51,7 @@ GEM
|
|
56
51
|
database_cleaner (0.7.0)
|
57
52
|
diff-lcs (1.1.3)
|
58
53
|
erubis (2.7.0)
|
59
|
-
factory_girl (2.3.
|
54
|
+
factory_girl (2.3.2)
|
60
55
|
activesupport
|
61
56
|
forgery (0.5.0)
|
62
57
|
git (1.2.5)
|
@@ -67,13 +62,13 @@ GEM
|
|
67
62
|
bundler (~> 1.0)
|
68
63
|
git (>= 1.2.5)
|
69
64
|
rake
|
70
|
-
json (1.6.
|
65
|
+
json (1.6.3)
|
71
66
|
mail (2.3.0)
|
72
67
|
i18n (>= 0.4.0)
|
73
68
|
mime-types (~> 1.16)
|
74
69
|
treetop (~> 1.4.8)
|
75
70
|
mime-types (1.17.2)
|
76
|
-
multi_json (1.0.
|
71
|
+
multi_json (1.0.4)
|
77
72
|
polyglot (0.3.3)
|
78
73
|
rack (1.3.5)
|
79
74
|
rack-cache (1.1)
|
@@ -117,10 +112,10 @@ GEM
|
|
117
112
|
hike (~> 1.2)
|
118
113
|
rack (~> 1.0)
|
119
114
|
tilt (~> 1.1, != 1.3.0)
|
120
|
-
sqlite3 (1.3.
|
121
|
-
sugar-high (0.6.
|
115
|
+
sqlite3 (1.3.5)
|
116
|
+
sugar-high (0.6.2.1)
|
122
117
|
activesupport (>= 3.0.1)
|
123
|
-
sweetloader (0.1.
|
118
|
+
sweetloader (0.1.6)
|
124
119
|
activesupport (>= 3.0.1)
|
125
120
|
i18n
|
126
121
|
thor (0.14.6)
|
@@ -135,12 +130,13 @@ PLATFORMS
|
|
135
130
|
|
136
131
|
DEPENDENCIES
|
137
132
|
bundler (>= 1.1.rc)
|
138
|
-
cantango-config
|
133
|
+
cantango-config (>= 0.1.8.1)
|
134
|
+
cantango-core (>= 0.1.9.1)
|
139
135
|
cutter
|
140
136
|
database_cleaner
|
141
137
|
factory_girl
|
142
138
|
forgery
|
143
|
-
hashie
|
139
|
+
hashie (~> 1.2)
|
144
140
|
jeweler (>= 1.6.4)
|
145
141
|
rails (>= 3.1)
|
146
142
|
rcov
|
@@ -148,5 +144,5 @@ DEPENDENCIES
|
|
148
144
|
rspec (>= 2.6.0)
|
149
145
|
spork
|
150
146
|
sqlite3
|
151
|
-
sugar-high (
|
152
|
-
sweetloader (~> 0.1.
|
147
|
+
sugar-high (~> 0.6.2)
|
148
|
+
sweetloader (~> 0.1.6)
|
data/README.mdown
CHANGED
@@ -2,6 +2,79 @@
|
|
2
2
|
|
3
3
|
The main API for [CanTango](https://github.com/kristianmandrup/cantango)
|
4
4
|
|
5
|
+
## Status: Dec 1 2011
|
6
|
+
|
7
|
+
The API is being spec'ed...
|
8
|
+
|
9
|
+
## Dependencies
|
10
|
+
|
11
|
+
This extension depends on the CanTango [core](https://github.com/kristianmandrup/cantango-core) and follows the extension conventions as described there.
|
12
|
+
|
13
|
+
## Core extensions
|
14
|
+
|
15
|
+
* Macros
|
16
|
+
* User
|
17
|
+
* Account
|
18
|
+
|
19
|
+
These macros allow registration os CanTango User and Account models.
|
20
|
+
|
21
|
+
Usage example:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
tango_user
|
26
|
+
end
|
27
|
+
|
28
|
+
class Admin
|
29
|
+
tango_user
|
30
|
+
end
|
31
|
+
|
32
|
+
class UserAccount
|
33
|
+
tango_account
|
34
|
+
end
|
35
|
+
|
36
|
+
class AdminAccount
|
37
|
+
tango_account
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Main APIs
|
42
|
+
|
43
|
+
The main API consists of the following:
|
44
|
+
|
45
|
+
* Ability
|
46
|
+
* Can
|
47
|
+
* Model
|
48
|
+
* Scope
|
49
|
+
* Session
|
50
|
+
|
51
|
+
Each of these have a specific variant for both `Account` and `User`.
|
52
|
+
|
53
|
+
### Can API
|
54
|
+
|
55
|
+
* Dsl
|
56
|
+
* Relation
|
57
|
+
* Scope
|
58
|
+
|
59
|
+
The Dsl API extends the CanCan Ability API with Relation and Scope APIs.
|
60
|
+
|
61
|
+
## Model APIs
|
62
|
+
|
63
|
+
* Account
|
64
|
+
* User
|
65
|
+
|
66
|
+
Use by including the `All` module
|
67
|
+
|
68
|
+
Usage example:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class User
|
72
|
+
include Cantango::Api::User::All
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Note: The `tango_user` and `tango_account` macros should include the relevant All module.
|
77
|
+
|
5
78
|
## Contributing to cantango-api
|
6
79
|
|
7
80
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/cantango-api.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = %q{cantango-api}
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
11
|
+
s.authors = [%q{Kristian Mandrup}]
|
12
|
+
s.date = %q{2011-12-08}
|
13
|
+
s.description = %q{Ability, Can, Scope, Session, User, Account and more APIs for CanTango}
|
14
|
+
s.email = %q{kristian@unity3d.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
17
|
"README.mdown"
|
@@ -34,6 +34,9 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/cantango/api/attributes.rb",
|
35
35
|
"lib/cantango/api/can.rb",
|
36
36
|
"lib/cantango/api/can/account.rb",
|
37
|
+
"lib/cantango/api/can/dsl.rb",
|
38
|
+
"lib/cantango/api/can/dsl/relation.rb",
|
39
|
+
"lib/cantango/api/can/dsl/scope.rb",
|
37
40
|
"lib/cantango/api/can/user.rb",
|
38
41
|
"lib/cantango/api/common.rb",
|
39
42
|
"lib/cantango/api/model.rb",
|
@@ -47,16 +50,29 @@ Gem::Specification.new do |s|
|
|
47
50
|
"lib/cantango/api/session/account.rb",
|
48
51
|
"lib/cantango/api/session/user.rb",
|
49
52
|
"lib/cantango/api/user.rb",
|
53
|
+
"lib/cantango/api_ext.rb",
|
54
|
+
"lib/cantango/api_ext/macros.rb",
|
55
|
+
"lib/cantango/api_ext/macros/account.rb",
|
56
|
+
"lib/cantango/api_ext/macros/clazz.rb",
|
57
|
+
"lib/cantango/api_ext/macros/user.rb",
|
50
58
|
"spec/cantango/api/ability/account_spec.rb",
|
51
59
|
"spec/cantango/api/ability/user_spec.rb",
|
52
60
|
"spec/cantango/api/account_spec.rb",
|
53
61
|
"spec/cantango/api/attributes_spec.rb",
|
54
62
|
"spec/cantango/api/can/account_spec.rb",
|
55
63
|
"spec/cantango/api/can/user_spec.rb",
|
64
|
+
"spec/cantango/api/common_spec.rb",
|
65
|
+
"spec/cantango/api/model/account_spec.rb",
|
66
|
+
"spec/cantango/api/model/user_spec.rb",
|
56
67
|
"spec/cantango/api/options_spec.rb",
|
57
68
|
"spec/cantango/api/scope/account_spec.rb",
|
58
69
|
"spec/cantango/api/scope/user_spec.rb",
|
70
|
+
"spec/cantango/api/session/account_spec.rb",
|
71
|
+
"spec/cantango/api/session/user_spec.rb",
|
59
72
|
"spec/cantango/api/user_spec.rb",
|
73
|
+
"spec/cantango/api_ext/macros/account_spec.rb",
|
74
|
+
"spec/cantango/api_ext/macros/clazz_spec.rb",
|
75
|
+
"spec/cantango/api_ext/macros/user_spec.rb",
|
60
76
|
"spec/cantango/api_spec.rb",
|
61
77
|
"spec/cantango_api_spec.rb",
|
62
78
|
"spec/fixtures/models.rb",
|
@@ -72,31 +88,33 @@ Gem::Specification.new do |s|
|
|
72
88
|
"spec/helpers/current_users.rb",
|
73
89
|
"spec/spec_helper.rb"
|
74
90
|
]
|
75
|
-
s.homepage =
|
76
|
-
s.licenses = [
|
77
|
-
s.require_paths = [
|
78
|
-
s.rubygems_version =
|
79
|
-
s.summary =
|
91
|
+
s.homepage = %q{http://github.com/kristianmandrup/cantango-api}
|
92
|
+
s.licenses = [%q{MIT}]
|
93
|
+
s.require_paths = [%q{lib}]
|
94
|
+
s.rubygems_version = %q{1.8.6}
|
95
|
+
s.summary = %q{The main API for CanTango}
|
80
96
|
|
81
97
|
if s.respond_to? :specification_version then
|
82
98
|
s.specification_version = 3
|
83
99
|
|
84
100
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
85
101
|
s.add_runtime_dependency(%q<rails>, [">= 3.1"])
|
86
|
-
s.add_runtime_dependency(%q<sugar-high>, ["
|
87
|
-
s.add_runtime_dependency(%q<sweetloader>, ["~> 0.1.
|
88
|
-
s.add_runtime_dependency(%q<hashie>, ["
|
89
|
-
s.add_runtime_dependency(%q<cantango-config>, [">= 0"])
|
102
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.6.2"])
|
103
|
+
s.add_runtime_dependency(%q<sweetloader>, ["~> 0.1.6"])
|
104
|
+
s.add_runtime_dependency(%q<hashie>, ["~> 1.2"])
|
105
|
+
s.add_runtime_dependency(%q<cantango-config>, [">= 0.1.8.1"])
|
106
|
+
s.add_runtime_dependency(%q<cantango-core>, [">= 0.1.9.1"])
|
90
107
|
s.add_development_dependency(%q<bundler>, [">= 1.1.rc"])
|
91
108
|
s.add_development_dependency(%q<jeweler>, [">= 1.6.4"])
|
92
109
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
93
110
|
s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
|
94
111
|
else
|
95
112
|
s.add_dependency(%q<rails>, [">= 3.1"])
|
96
|
-
s.add_dependency(%q<sugar-high>, ["
|
97
|
-
s.add_dependency(%q<sweetloader>, ["~> 0.1.
|
98
|
-
s.add_dependency(%q<hashie>, ["
|
99
|
-
s.add_dependency(%q<cantango-config>, [">= 0"])
|
113
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.6.2"])
|
114
|
+
s.add_dependency(%q<sweetloader>, ["~> 0.1.6"])
|
115
|
+
s.add_dependency(%q<hashie>, ["~> 1.2"])
|
116
|
+
s.add_dependency(%q<cantango-config>, [">= 0.1.8.1"])
|
117
|
+
s.add_dependency(%q<cantango-core>, [">= 0.1.9.1"])
|
100
118
|
s.add_dependency(%q<bundler>, [">= 1.1.rc"])
|
101
119
|
s.add_dependency(%q<jeweler>, [">= 1.6.4"])
|
102
120
|
s.add_dependency(%q<rcov>, [">= 0"])
|
@@ -104,10 +122,11 @@ Gem::Specification.new do |s|
|
|
104
122
|
end
|
105
123
|
else
|
106
124
|
s.add_dependency(%q<rails>, [">= 3.1"])
|
107
|
-
s.add_dependency(%q<sugar-high>, ["
|
108
|
-
s.add_dependency(%q<sweetloader>, ["~> 0.1.
|
109
|
-
s.add_dependency(%q<hashie>, ["
|
110
|
-
s.add_dependency(%q<cantango-config>, [">= 0"])
|
125
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.6.2"])
|
126
|
+
s.add_dependency(%q<sweetloader>, ["~> 0.1.6"])
|
127
|
+
s.add_dependency(%q<hashie>, ["~> 1.2"])
|
128
|
+
s.add_dependency(%q<cantango-config>, [">= 0.1.8.1"])
|
129
|
+
s.add_dependency(%q<cantango-core>, [">= 0.1.9.1"])
|
111
130
|
s.add_dependency(%q<bundler>, [">= 1.1.rc"])
|
112
131
|
s.add_dependency(%q<jeweler>, [">= 1.6.4"])
|
113
132
|
s.add_dependency(%q<rcov>, [">= 0"])
|
@@ -1,21 +1,21 @@
|
|
1
1
|
module CanTango::Api
|
2
2
|
module Ability
|
3
3
|
module Account
|
4
|
-
def
|
5
|
-
@account_ability ||= create_ability(
|
4
|
+
def account_ability account, options = {}
|
5
|
+
@account_ability ||= create_ability(account, ability_options.merge(options))
|
6
6
|
end
|
7
|
-
alias_method :account_ability, :user_account_ability
|
8
7
|
|
9
8
|
def current_account_ability user_type = :user
|
10
|
-
|
9
|
+
account_ability(get_ability_account user_type)
|
11
10
|
end
|
12
11
|
|
13
12
|
protected
|
14
13
|
|
15
14
|
include CanTango::Api::Common
|
15
|
+
include CanTango::Api::Attributes
|
16
16
|
include CanTango::Api::Options
|
17
17
|
|
18
|
-
def
|
18
|
+
def get_ability_account user_type = :user
|
19
19
|
account_meth = :"current_#{user_type}_account"
|
20
20
|
return AbilityAccount.guest if !respond_to?(account_meth)
|
21
21
|
AbilityAccount.resolve_account(send account_meth)
|
@@ -31,7 +31,7 @@ module CanTango::Api
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.is_account? account
|
34
|
-
::CanTango.config.
|
34
|
+
::CanTango.config.accounts.registered_class? account.class
|
35
35
|
end
|
36
36
|
|
37
37
|
def self.guest
|
data/lib/cantango/api/account.rb
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
module CanTango::Api
|
2
2
|
module Account
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
base.send :include, clazz(api)
|
7
|
-
end
|
3
|
+
def self.included base
|
4
|
+
apis.each do |api|
|
5
|
+
base.send :include, clazz(api)
|
8
6
|
end
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def self.apis
|
10
|
+
[:ability, :can, :scope, :session]
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
13
|
+
def self.clazz api
|
14
|
+
"CanTango::Api::#{api.to_s.camelize}::Account".constantize
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module CanTango::Api
|
2
2
|
module Can
|
3
3
|
module Account
|
4
|
-
include Ability
|
4
|
+
include CanTango::Api::Ability::Account
|
5
5
|
# Example: generated from types of accounts! (see below)
|
6
6
|
# def admin_account_can?(*args)
|
7
7
|
# current_account_ability(:admin).can?(*args)
|
8
8
|
# end
|
9
9
|
def self.included(base)
|
10
|
-
::CanTango.config.
|
10
|
+
::CanTango.config.accounts.registered.each do |account|
|
11
11
|
|
12
12
|
# by default alias call to current_xxx_account to current_xxx (devise user method) unless already defined!
|
13
13
|
unless base.methods.include? :"current_#{account}_account"
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
module CanTango
|
4
|
+
module Dsl
|
5
|
+
class Relation
|
6
|
+
attr_reader :attribute, :permit, :scope, :models
|
7
|
+
|
8
|
+
include CanTango::Adaptor
|
9
|
+
include CanTango::CanCan::RuleClass
|
10
|
+
|
11
|
+
def initialize attribute, permit, scope, *models, &block
|
12
|
+
@attribute = attribute
|
13
|
+
@scope = scope
|
14
|
+
@permit = permit
|
15
|
+
@models = models
|
16
|
+
|
17
|
+
check_models
|
18
|
+
use_adaptor! self, user_scope
|
19
|
+
end
|
20
|
+
|
21
|
+
def can(action)
|
22
|
+
models.each do |model|
|
23
|
+
rules << rule_class.new(true, action, model, nil, condition_block(model))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def cannot(action)
|
28
|
+
models.each do |model|
|
29
|
+
rules << rule_class.new(false, action, model, nil, condition_block(model))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def condition_block model
|
36
|
+
return attribute_condition(attribute, user_scope) if model.new.respond_to?(attribute)
|
37
|
+
return include_condition(plural_attribute, user_scope) if model.new.respond_to?(plural_attribute)
|
38
|
+
end
|
39
|
+
|
40
|
+
def user_scope
|
41
|
+
@user_scope ||= (scope == :account) ? permit.user_account : permit.user
|
42
|
+
end
|
43
|
+
|
44
|
+
def scope_key
|
45
|
+
@scope_key ||= (scope == :account) ? :user_account : :user
|
46
|
+
end
|
47
|
+
|
48
|
+
def ability
|
49
|
+
permit.ability
|
50
|
+
end
|
51
|
+
|
52
|
+
def rules
|
53
|
+
ability.send :rules
|
54
|
+
end
|
55
|
+
|
56
|
+
def plural_attribute
|
57
|
+
:"#{attribute.to_s.pluralize}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def check_models
|
61
|
+
models.each do |model|
|
62
|
+
raise "#{model} has no :#{attribute} or :#{plural_attribute} defined" if !model.new.respond_to?(attribute) && !model.new.respond_to?(plural_attribute)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CanTango
|
2
|
+
module Dsl
|
3
|
+
class Scope
|
4
|
+
attr_reader :name, :permit
|
5
|
+
|
6
|
+
def initialize name, permit, &block
|
7
|
+
@name = name.to_sym
|
8
|
+
@permit = permit
|
9
|
+
end
|
10
|
+
|
11
|
+
::CanTango.config.user.relations.each do |relationship|
|
12
|
+
class_eval %{
|
13
|
+
def #{relationship}_of *models, &block
|
14
|
+
options = models.extract_options!
|
15
|
+
scope = options[:scope] || name
|
16
|
+
relation = UserRelation.new :#{relationship}, permit, scope, models, &block
|
17
|
+
yield relation if block
|
18
|
+
relation
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module CanTango::Api
|
2
|
+
module Can
|
3
|
+
module Dsl
|
4
|
+
autoload_modules :Relation, :Scope
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
::CanTango.config.user.relations.each do |relationship|
|
8
|
+
base.class_eval %{
|
9
|
+
def #{relationship}_of *models, &block
|
10
|
+
options = models.extract_options!
|
11
|
+
scope = options[:scope] || :user_account
|
12
|
+
relation = Relation.new :#{relationship}, self, scope, *models, &block
|
13
|
+
yield relation if block
|
14
|
+
relation
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# creates a scope that enforces either using the user or user_account for determining relationship matches on the models
|
21
|
+
def scope name, &block
|
22
|
+
yield Scope.new name, self, &block
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/cantango/api/can.rb
CHANGED
data/lib/cantango/api/common.rb
CHANGED
@@ -1,14 +1,27 @@
|
|
1
1
|
module CanTango::Api
|
2
2
|
module Common
|
3
|
-
include CanTango::Api::Attributes
|
4
|
-
|
5
3
|
def create_ability candidate, opts = {}
|
6
|
-
|
4
|
+
ability = default_ability_class.new candidate, opts
|
5
|
+
config.ability.factory_build ability, opts.merge(:modes => execution_modes)
|
7
6
|
end
|
8
7
|
|
9
8
|
def category label
|
10
9
|
config.models.by_category label
|
11
10
|
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def default_ability_class
|
15
|
+
CanTango.config.ability.default_class
|
16
|
+
end
|
17
|
+
|
18
|
+
def execution_modes
|
19
|
+
CanTango.config.ability.modes || [:no_cache]
|
20
|
+
end
|
21
|
+
|
22
|
+
def config
|
23
|
+
CanTango.config
|
24
|
+
end
|
12
25
|
end
|
13
26
|
end
|
14
27
|
|
@@ -3,8 +3,8 @@ module CanTango::Api
|
|
3
3
|
module Account
|
4
4
|
attr_writer :active_user, :active_account, :ability_class
|
5
5
|
|
6
|
-
def self.included(
|
7
|
-
CanTango.config.
|
6
|
+
def self.included(clazz)
|
7
|
+
CanTango.config.accounts.register_account clazz
|
8
8
|
end
|
9
9
|
|
10
10
|
def active_user
|
@@ -17,23 +17,16 @@ module CanTango::Api
|
|
17
17
|
@active_account || self
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def cannot? *args
|
25
|
-
ability.cannot?(*args)
|
26
|
-
end
|
20
|
+
# from sugar-high or Active Support
|
21
|
+
delegate :can?, :cannot?, :to => :ability
|
27
22
|
|
28
23
|
protected
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
CanTango::Ability::Executor::Modes
|
36
|
-
end
|
25
|
+
include CanTango::Api::Common
|
26
|
+
|
27
|
+
def ability opts = {}
|
28
|
+
create_ability active_account, opts = {}
|
29
|
+
end
|
37
30
|
end
|
38
31
|
end
|
39
32
|
end
|