cantango-api 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +36 -0
  4. data/Gemfile.lock +152 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.mdown +19 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/cantango-api.gemspec +117 -0
  10. data/lib/cantango/api/ability/account.rb +46 -0
  11. data/lib/cantango/api/ability/user.rb +45 -0
  12. data/lib/cantango/api/ability.rb +5 -0
  13. data/lib/cantango/api/account.rb +20 -0
  14. data/lib/cantango/api/attributes.rb +15 -0
  15. data/lib/cantango/api/can/account.rb +32 -0
  16. data/lib/cantango/api/can/user.rb +26 -0
  17. data/lib/cantango/api/can.rb +5 -0
  18. data/lib/cantango/api/common.rb +14 -0
  19. data/lib/cantango/api/model/account.rb +39 -0
  20. data/lib/cantango/api/model/user.rb +39 -0
  21. data/lib/cantango/api/model.rb +5 -0
  22. data/lib/cantango/api/options.rb +19 -0
  23. data/lib/cantango/api/scope/account.rb +26 -0
  24. data/lib/cantango/api/scope/user.rb +26 -0
  25. data/lib/cantango/api/scope.rb +5 -0
  26. data/lib/cantango/api/session/account.rb +38 -0
  27. data/lib/cantango/api/session/user.rb +38 -0
  28. data/lib/cantango/api/session.rb +5 -0
  29. data/lib/cantango/api/user.rb +19 -0
  30. data/lib/cantango/api.rb +26 -0
  31. data/spec/cantango/api/ability/account_spec.rb +34 -0
  32. data/spec/cantango/api/ability/user_spec.rb +57 -0
  33. data/spec/cantango/api/account_spec.rb +0 -0
  34. data/spec/cantango/api/attributes_spec.rb +25 -0
  35. data/spec/cantango/api/can/account_spec.rb +85 -0
  36. data/spec/cantango/api/can/user_spec.rb +113 -0
  37. data/spec/cantango/api/options_spec.rb +0 -0
  38. data/spec/cantango/api/scope/account_spec.rb +0 -0
  39. data/spec/cantango/api/scope/user_spec.rb +73 -0
  40. data/spec/cantango/api/user_spec.rb +0 -0
  41. data/spec/cantango/api_spec.rb +11 -0
  42. data/spec/cantango_api_spec.rb +8 -0
  43. data/spec/fixtures/models/admin.rb +2 -0
  44. data/spec/fixtures/models/admin_account.rb +22 -0
  45. data/spec/fixtures/models/items.rb +8 -0
  46. data/spec/fixtures/models/permission.rb +12 -0
  47. data/spec/fixtures/models/project.rb +2 -0
  48. data/spec/fixtures/models/simple_roles.rb +49 -0
  49. data/spec/fixtures/models/user.rb +52 -0
  50. data/spec/fixtures/models/user_account.rb +21 -0
  51. data/spec/fixtures/models.rb +2 -0
  52. data/spec/helpers/current_user_accounts.rb +20 -0
  53. data/spec/helpers/current_users.rb +10 -0
  54. data/spec/spec_helper.rb +14 -0
  55. metadata +203 -0
@@ -0,0 +1,22 @@
1
+ class AdminAccount
2
+ attr_accessor :user, :roles, :role_groups
3
+
4
+ def initialize user, options = {}
5
+ @user = user
6
+ @roles = options[:roles]
7
+ @role_groups = options[:role_groups]
8
+ end
9
+
10
+ def has_role? name
11
+ true
12
+ end
13
+
14
+ def roles_list
15
+ roles
16
+ end
17
+
18
+ def role_groups_list
19
+ role_groups
20
+ end
21
+ end
22
+
@@ -0,0 +1,8 @@
1
+ class Post
2
+ end
3
+
4
+ class Article
5
+ end
6
+
7
+ class Comment
8
+ end
@@ -0,0 +1,12 @@
1
+ class Permission
2
+ attr_accessor :thing, :thing_type, :action, :user
3
+
4
+ def initialize user, action, thing
5
+ @user, @action, @thing = [user, action, thing]
6
+ @thing_type = @thing.class.to_s
7
+ end
8
+
9
+ def thing_id
10
+ thing.id
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ class Project #< ActiveRecord::Base
2
+ end
@@ -0,0 +1,49 @@
1
+ module SimpleRoles
2
+ def self.included(base)
3
+ base.send :include, InstanceMethods
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def is_role_in_group?(role, group)
9
+ raise "No group #{group} defined in User model" if !role_groups.has_key?(group)
10
+ role_groups[group].include?(role)
11
+ end
12
+
13
+ def role_groups
14
+ {:bloggers => [:editor]}
15
+ end
16
+
17
+ def roles
18
+ [:guest, :user, :admin, :editor]
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+
24
+ attr_accessor :role_groups_list
25
+
26
+ def has_role? role
27
+ roles_list.include? role
28
+ end
29
+
30
+ def has_any_role? roles
31
+ roles.include?(role.to_sym)
32
+ end
33
+
34
+ def roles_list
35
+ role.to_s.scan(/\w+/).map{|r| r.to_sym}
36
+ end
37
+
38
+ def is_in_group? group
39
+ role_groups_list.include? group
40
+ end
41
+ alias_method :in_role_group?, :is_in_group?
42
+
43
+ def role_groups_list
44
+ return role_groups.scan(/\w+/).map(&:to_sym) if respond_to?(:role_groups) && !role_groups.nil?
45
+ @role_groups_list || [] #[:bloggers]
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,52 @@
1
+ require 'fixtures/models/simple_roles'
2
+
3
+ class User
4
+ attr_accessor :name, :account, :email
5
+
6
+ # tango_user # see macros
7
+
8
+ include ::SimpleRoles
9
+
10
+ def initialize name, email = nil, options = {}
11
+ @name = name
12
+ @email = email
13
+ set_option_vars options
14
+ end
15
+
16
+ def set_option_vars options = {}
17
+ options.each_pair do |name, value|
18
+ var = :"@#{name}"
19
+ self.instance_variable_set(var, value)
20
+ end
21
+ end
22
+
23
+ def email
24
+ @email ||= 'default@gmail.com'
25
+ end
26
+
27
+ def role
28
+ @role || ''
29
+ end
30
+
31
+ # This hash should be recalculated anytime the permissions collection changes
32
+ #
33
+
34
+ # after_update :recalculate_permissions_hash
35
+
36
+ def permissions_hash
37
+ @permissions_hash = permissions.hash
38
+ end
39
+
40
+ def permissions
41
+ @permissions ||= []
42
+ end
43
+
44
+ # allows implementation specific to ORM, fx using #all on some datastores such as Mongoid etc.
45
+ alias_method :all_permissions, :permissions
46
+
47
+ protected
48
+
49
+ def recalculate_permissions_hash
50
+ @permissions_hash = nil if self.permissions_changed?
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ class UserAccount
2
+ attr_accessor :user, :roles, :role_groups
3
+
4
+ def initialize user, options = {}
5
+ @user = user
6
+ @roles = options[:roles]
7
+ @role_groups = options[:role_groups]
8
+ end
9
+
10
+ def has_role? name
11
+ true
12
+ end
13
+
14
+ def roles_list
15
+ roles
16
+ end
17
+
18
+ def role_groups_list
19
+ role_groups
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'require_all'
2
+ require_all File.dirname(__FILE__) + '/models'
@@ -0,0 +1,20 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'current_users'
4
+
5
+ module CurrentUserAccounts
6
+ include ::CurrentUsers
7
+
8
+ def self.included(base)
9
+ base.extend ::CurrentUsers
10
+ end
11
+
12
+ def current_user_account
13
+ ::UserAccount.new(current_user, :roles => [:user])
14
+ end
15
+
16
+ def current_admin_account
17
+ ::UserAccount.new(current_admin, :roles => [:admin])
18
+ end
19
+ end
20
+
@@ -0,0 +1,10 @@
1
+ module CurrentUsers
2
+ def current_user
3
+ @current_user ||= ::User.new 'stan', 'stan@mail.ru', :role => 'user'
4
+ end
5
+
6
+ def current_admin
7
+ @current_admin ||= ::User.new 'admin', 'admin@mail.ru', :role => 'admin'
8
+ end
9
+ end
10
+
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'require_all'
3
+
4
+
5
+ require 'cantango/core'
6
+ require 'cantango/configuration'
7
+ require 'cantango/api'
8
+
9
+ # require_all File.join("#{File.dirname(__FILE__)}", "support")
10
+ require_all File.join("#{File.dirname(__FILE__)}", "fixtures")
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cantango-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &895281780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *895281780
25
+ - !ruby/object:Gem::Dependency
26
+ name: sugar-high
27
+ requirement: &895281260 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *895281260
36
+ - !ruby/object:Gem::Dependency
37
+ name: sweetloader
38
+ requirement: &895280240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *895280240
47
+ - !ruby/object:Gem::Dependency
48
+ name: hashie
49
+ requirement: &895279880 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *895279880
58
+ - !ruby/object:Gem::Dependency
59
+ name: cantango-config
60
+ requirement: &895279280 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *895279280
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: &895278600 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 1.1.rc
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *895278600
80
+ - !ruby/object:Gem::Dependency
81
+ name: jeweler
82
+ requirement: &895278190 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 1.6.4
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *895278190
91
+ - !ruby/object:Gem::Dependency
92
+ name: rcov
93
+ requirement: &895277780 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *895277780
102
+ - !ruby/object:Gem::Dependency
103
+ name: rspec
104
+ requirement: &895277450 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 2.6.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *895277450
113
+ description: Ability, Can, Scope, Session, User, Account and more APIs for CanTango
114
+ email: kristian@unity3d.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files:
118
+ - LICENSE.txt
119
+ - README.mdown
120
+ files:
121
+ - .document
122
+ - .rspec
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.mdown
127
+ - Rakefile
128
+ - VERSION
129
+ - cantango-api.gemspec
130
+ - lib/cantango/api.rb
131
+ - lib/cantango/api/ability.rb
132
+ - lib/cantango/api/ability/account.rb
133
+ - lib/cantango/api/ability/user.rb
134
+ - lib/cantango/api/account.rb
135
+ - lib/cantango/api/attributes.rb
136
+ - lib/cantango/api/can.rb
137
+ - lib/cantango/api/can/account.rb
138
+ - lib/cantango/api/can/user.rb
139
+ - lib/cantango/api/common.rb
140
+ - lib/cantango/api/model.rb
141
+ - lib/cantango/api/model/account.rb
142
+ - lib/cantango/api/model/user.rb
143
+ - lib/cantango/api/options.rb
144
+ - lib/cantango/api/scope.rb
145
+ - lib/cantango/api/scope/account.rb
146
+ - lib/cantango/api/scope/user.rb
147
+ - lib/cantango/api/session.rb
148
+ - lib/cantango/api/session/account.rb
149
+ - lib/cantango/api/session/user.rb
150
+ - lib/cantango/api/user.rb
151
+ - spec/cantango/api/ability/account_spec.rb
152
+ - spec/cantango/api/ability/user_spec.rb
153
+ - spec/cantango/api/account_spec.rb
154
+ - spec/cantango/api/attributes_spec.rb
155
+ - spec/cantango/api/can/account_spec.rb
156
+ - spec/cantango/api/can/user_spec.rb
157
+ - spec/cantango/api/options_spec.rb
158
+ - spec/cantango/api/scope/account_spec.rb
159
+ - spec/cantango/api/scope/user_spec.rb
160
+ - spec/cantango/api/user_spec.rb
161
+ - spec/cantango/api_spec.rb
162
+ - spec/cantango_api_spec.rb
163
+ - spec/fixtures/models.rb
164
+ - spec/fixtures/models/admin.rb
165
+ - spec/fixtures/models/admin_account.rb
166
+ - spec/fixtures/models/items.rb
167
+ - spec/fixtures/models/permission.rb
168
+ - spec/fixtures/models/project.rb
169
+ - spec/fixtures/models/simple_roles.rb
170
+ - spec/fixtures/models/user.rb
171
+ - spec/fixtures/models/user_account.rb
172
+ - spec/helpers/current_user_accounts.rb
173
+ - spec/helpers/current_users.rb
174
+ - spec/spec_helper.rb
175
+ homepage: http://github.com/kristianmandrup/cantango-api
176
+ licenses:
177
+ - MIT
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ! '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ segments:
189
+ - 0
190
+ hash: 568185265
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ! '>='
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubyforge_project:
199
+ rubygems_version: 1.8.10
200
+ signing_key:
201
+ specification_version: 3
202
+ summary: The main API for CanTango
203
+ test_files: []