cantango-api 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/cantango-api.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cantango-api}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kristian Mandrup}]
@@ -1,6 +1,8 @@
1
1
  module CanTango::Api
2
2
  module Scope
3
3
  module User
4
+ include CanTango::Api::Ability::User
5
+
4
6
  def scope_user scope, options = {}, &block
5
7
  user = scoped_user(scope)
6
8
  ab_scope = user_ability_scope(user, options)
@@ -16,7 +18,7 @@ module CanTango::Api
16
18
  protected
17
19
 
18
20
  def user_ability_scope user, options = {}
19
- CanTango::Ability::Scope.new user_ability(user, options)
21
+ CanTango::Scope::Ability.new user_ability(user, options)
20
22
  end
21
23
 
22
24
  def scoped_user scope
@@ -15,11 +15,13 @@ module CanTango::Api
15
15
  # give me any logged in user or the guest user
16
16
  def any_account *types
17
17
  types = types.flatten.select_labels.map(&:to_sym)
18
- c_account = ::CanTango.config.user_accounts.registered.each do |type|
18
+ types = types & ::CanTango.config.accounts.registered
19
+ accounts = types.map do |type|
19
20
  meth = :"current_#{type}_account"
20
- send(meth) if respond_to?(meth) && (types.empty? || types.include?(user))
21
- end.compact.first
22
- c_account || guest_account
21
+ send(meth) if respond_to?(meth) && (types.empty? || types.include?(type))
22
+ end
23
+ chosen_account = accounts.compact.first
24
+ chosen_account || guest_account
23
25
  end
24
26
 
25
27
  def guest_account
@@ -15,11 +15,13 @@ module CanTango::Api
15
15
  # give me any logged in user or the guest user
16
16
  def any_user *types
17
17
  types = types.flatten.select_labels.map(&:to_sym)
18
- c_user = ::CanTango.config.users.registered.each do |user|
19
- meth = :"current_#{user}"
20
- send(meth) if respond_to?(meth) && (types.empty? || types.include?(user))
21
- end.compact.first
22
- c_user || guest_user
18
+ types = types & ::CanTango.config.users.registered
19
+ users = types.map do |type|
20
+ meth = :"current_#{type}"
21
+ send(meth) if respond_to?(meth) && (types.empty? || types.include?(type))
22
+ end
23
+ chosen_user = users.compact.first
24
+ chosen_user || guest_user
23
25
  end
24
26
 
25
27
  def guest_user
@@ -3,13 +3,21 @@ module CanTango::Macros
3
3
  include CanTango::Macros::Account
4
4
  include CanTango::Macros::User
5
5
 
6
- def cantango type = nil
6
+ def cantango options = {}
7
+ type = case options
8
+ when Array
9
+ options.first
10
+ when Symbol
11
+ options
12
+ when Hash
13
+ options[:type]
14
+ end
7
15
  # determine if user or account
8
16
  if !type
9
- (name =~ /.+Account$/) ? tango_account : tango_user
17
+ (name =~ /.+Account$/) ? tango_account(options) : tango_user(options)
10
18
  else
11
- tango_account and return if type.to_s == 'account'
12
- tango_user and return if type.to_s == 'user'
19
+ tango_account(options) and return if type.to_s == 'account'
20
+ tango_user(options) and return if type.to_s == 'user'
13
21
  raise ArgumentError, "Unable to determine type of class to register; Please specify :user or :account as argument"
14
22
  end
15
23
  end
@@ -3,6 +3,5 @@ module CanTango::Macros
3
3
  def tango_user options = {}
4
4
  self.send :include, CanTango::Api::Model::User
5
5
  end
6
- alias_method :cantango_user, :tango_user
7
6
  end
8
7
  end
@@ -30,7 +30,7 @@ describe CanTango::Api::Scope::Account do
30
30
  describe 'scope_account(scope, options)' do
31
31
  specify do
32
32
  subject.scope_account :user do |ability|
33
- ability.subject.class.should == subject.current_user_account.class
33
+ ability.subject.should == subject.current_user_account
34
34
  end
35
35
  end
36
36
  end
@@ -38,7 +38,7 @@ describe CanTango::Api::Scope::Account do
38
38
  describe 'real_account(scope, options)' do
39
39
  specify do
40
40
  subject.real_account :admin do |ability|
41
- ability.subject.class.should == subject.current_admin_account.class
41
+ ability.subject.should == subject.current_admin_account
42
42
  end
43
43
  end
44
44
  end
@@ -12,21 +12,21 @@ class User
12
12
  cantango
13
13
  end
14
14
 
15
- class AdminUser
16
- cantango
15
+ class Admin
16
+ tango_user :masquerade
17
17
  end
18
18
 
19
19
  describe CanTango::Api::Scope::User do
20
20
  subject { Context.new }
21
21
 
22
22
  before do
23
- subject.current_user.active_account = subject.current_admin
23
+ subject.current_admin.masquerade_as subject.current_user
24
24
  end
25
25
 
26
26
  describe 'scope_user(scope, options)' do
27
27
  specify do
28
28
  subject.scope_user :user do |user|
29
- user.candidate.should == subject.current_admin
29
+ user.subject.should == subject.current_user
30
30
  end
31
31
  end
32
32
  end
@@ -34,7 +34,7 @@ describe CanTango::Api::Scope::User do
34
34
  describe 'real_user(scope, options)' do
35
35
  specify do
36
36
  subject.real_user :user do |user|
37
- user.candidate.should == subject.current_user
37
+ user.subject.should == subject.current_user
38
38
  end
39
39
  end
40
40
  end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+ require 'helpers/current_user_accounts'
4
+
5
+ class SessionContext
6
+ include Singleton
7
+ include CanTango::Api::Session::Account
8
+
9
+ include_and_extend ::CurrentUserAccounts
10
+
11
+ attr_writer :session
12
+ def session
13
+ @session ||= {}
14
+ end
15
+ end
16
+
17
+ # mock
18
+ module CanTango::Api
19
+ module Session
20
+ module Account
21
+ def guest_account
22
+ SessionContext.instance.current_guest_account
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ class UserAccount
29
+ tango_account
30
+ end
31
+
32
+ class AdminAccount
33
+ tango_account
34
+ end
35
+
36
+ describe CanTango::Api::Session::Account do
37
+ subject { SessionContext.instance }
38
+
39
+ describe 'registered accounts' do
40
+ specify { CanTango.config.accounts.registered.should include(:user, :admin) }
41
+ end
42
+
43
+ describe 'any_account' do
44
+ describe 'default to :guest if none found' do
45
+ specify do
46
+ subject.any_account(:unknown).should == subject.guest_account
47
+ end
48
+ end
49
+
50
+ describe 'find :admin' do
51
+ specify do
52
+ subject.any_account(:admin, :user).should == subject.current_admin_account
53
+ end
54
+ end
55
+ end
56
+
57
+ describe 'guest_account' do
58
+ specify do
59
+ subject.guest_account.role?(:guest).should be_true
60
+ end
61
+ end
62
+
63
+ describe 'active_account' do
64
+ specify do
65
+ subject.active_account.should == nil
66
+ end
67
+ end
68
+
69
+ describe 'active_account=' do
70
+ specify do
71
+ subject.active_account = subject.current_admin_account
72
+ subject.active_account.should == subject.current_admin_account
73
+ end
74
+ end
75
+ end
@@ -1,7 +1,17 @@
1
1
  class UserAccount
2
- attr_accessor :user
2
+ attr_accessor :user, :roles
3
3
 
4
4
  def initialize user, options = {}
5
5
  @user = user
6
+ @options = options
7
+ @roles = options[:roles]
8
+ end
9
+
10
+ def role? name
11
+ roles.include? name
12
+ end
13
+
14
+ def roles
15
+ @roles ||= []
6
16
  end
7
17
  end
@@ -12,6 +12,10 @@ module CurrentUserAccounts
12
12
  def current_user_account
13
13
  @current_user_account ||= ::UserAccount.new(current_user, :roles => [:user])
14
14
  end
15
+
16
+ def current_guest_account
17
+ @current_guest_account ||= ::UserAccount.new(current_guest, :roles => [:guest])
18
+ end
15
19
 
16
20
  def current_admin_account
17
21
  @current_admin_account ||= ::AdminAccount.new(current_admin, :roles => [:admin])
@@ -3,8 +3,12 @@ module CurrentUsers
3
3
  @current_user ||= ::User.new 'stan', 'stan@mail.ru', :role => 'user'
4
4
  end
5
5
 
6
+ def current_guest
7
+ @current_guest ||= ::User.new 'guest', 'guest@mail.ru', :role => 'guest'
8
+ end
9
+
6
10
  def current_admin
7
- @current_admin ||= ::User.new 'admin', 'admin@mail.ru', :role => 'admin'
11
+ @current_admin ||= ::Admin.new 'admin', 'admin@mail.ru', :role => 'admin'
8
12
  end
9
13
  end
10
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cantango-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70206619585940 !ruby/object:Gem::Requirement
16
+ requirement: &70276899472420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70206619585940
24
+ version_requirements: *70276899472420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sugar-high
27
- requirement: &70206619584480 !ruby/object:Gem::Requirement
27
+ requirement: &70276899471460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.6.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70206619584480
35
+ version_requirements: *70276899471460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sweetloader
38
- requirement: &70206619583740 !ruby/object:Gem::Requirement
38
+ requirement: &70276899469500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.1.6
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70206619583740
46
+ version_requirements: *70276899469500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hashie
49
- requirement: &70206619583100 !ruby/object:Gem::Requirement
49
+ requirement: &70276899467420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '1.2'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70206619583100
57
+ version_requirements: *70276899467420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: cantango-config
60
- requirement: &70206619582400 !ruby/object:Gem::Requirement
60
+ requirement: &70276899465860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.1.9.2
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70206619582400
68
+ version_requirements: *70276899465860
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: cantango-core
71
- requirement: &70206619581180 !ruby/object:Gem::Requirement
71
+ requirement: &70276899461080 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.1.9.2
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70206619581180
79
+ version_requirements: *70276899461080
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &70206619579920 !ruby/object:Gem::Requirement
82
+ requirement: &70276899459540 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.1.rc
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70206619579920
90
+ version_requirements: *70276899459540
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &70206619577860 !ruby/object:Gem::Requirement
93
+ requirement: &70276899457320 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 1.6.4
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70206619577860
101
+ version_requirements: *70276899457320
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rcov
104
- requirement: &70206619575720 !ruby/object:Gem::Requirement
104
+ requirement: &70276899456140 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70206619575720
112
+ version_requirements: *70276899456140
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rspec
115
- requirement: &70206619573580 !ruby/object:Gem::Requirement
115
+ requirement: &70276899455300 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: 2.6.0
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70206619573580
123
+ version_requirements: *70276899455300
124
124
  description: Ability, Can, Scope, Session, User, Account and more APIs for CanTango
125
125
  email: kristian@unity3d.com
126
126
  executables: []
@@ -214,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
214
  version: '0'
215
215
  segments:
216
216
  - 0
217
- hash: -2854044970758451050
217
+ hash: -1309343168374128705
218
218
  required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  none: false
220
220
  requirements: