scoped_rolify 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -1
- data/CHANGELOG.md +14 -0
- data/README.md +7 -1
- data/bin/publish +1 -0
- data/lib/scoped_rolify.rb +2 -0
- data/lib/scoped_rolify/finders.rb +3 -3
- data/lib/scoped_rolify/policy.rb +15 -0
- data/lib/scoped_rolify/role.rb +39 -5
- data/lib/scoped_rolify/version.rb +1 -1
- data/spec/scoped_rolify/finders_spec.rb +1 -1
- data/spec/scoped_rolify/role_spec.rb +26 -6
- metadata +21 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02c456aa8ca494528222680ebc0024fc8069a91b
|
4
|
+
data.tar.gz: afe192eba189a99fec5f5eed531bb46e137e8c21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfb764ac9b475af72a8361aebcf9cb1b2ea416187dc70eceec89e691763ac9324a3b5d13c398ee9b233e4569cf732327d9cbb5c97e013486931d27559fa818d5
|
7
|
+
data.tar.gz: ad768d2fc9a2f853be116476ec50381225fcef6834789f0962cd8196d1a797ffb60b2d5451af7a3dbbd732fc1dc8642e03edd41f34f7c5ffa8611819a7d0bf7a
|
data/.ruby-gemset
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
scoped_rolify
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
### VERSION 0.0.4
|
2
|
+
|
3
|
+
* bug fix
|
4
|
+
* monkey patch has_role? of Rolify for fixing behavior with heterogeneous role through in sym or string
|
5
|
+
|
6
|
+
* enhancements
|
7
|
+
* add_scope_role can be add role on none persisted object
|
8
|
+
* add method remove_scope_role for manage none persisted object
|
9
|
+
* move check into ScopedRolify::Policy
|
10
|
+
|
11
|
+
* backwards incompatible changes
|
12
|
+
|
13
|
+
* deprecations
|
14
|
+
|
1
15
|
### VERSION 0.0.3
|
2
16
|
|
3
17
|
* little changes
|
data/README.md
CHANGED
@@ -38,7 +38,10 @@ You can not add right without instance of resource
|
|
38
38
|
|
39
39
|
Only this case it's possible
|
40
40
|
|
41
|
-
user.add_scope_role :moderator, Forum.first
|
41
|
+
user.add_scope_role :moderator, Forum.first
|
42
|
+
user.add_scope_role :moderator, Forum.new
|
43
|
+
|
44
|
+
You can play also with remove_scope_role
|
42
45
|
|
43
46
|
Method with_scoped_role and method #with_any_scoped_role
|
44
47
|
|
@@ -53,6 +56,9 @@ Only this case it's possible
|
|
53
56
|
|
54
57
|
User.with_scoped_role :moderator, Forum.first #
|
55
58
|
|
59
|
+
You can't play with none persisted object
|
60
|
+
|
61
|
+
|
56
62
|
Method ```with_any_scoped_role``` return an ```ActiveRecord::Relation``` of all users with all roles asked for one resource
|
57
63
|
|
58
64
|
## Contributing
|
data/bin/publish
CHANGED
data/lib/scoped_rolify.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'rolify'
|
2
2
|
|
3
3
|
require 'scoped_rolify/version'
|
4
|
+
require 'scoped_rolify/policy'
|
4
5
|
require 'scoped_rolify/role'
|
5
6
|
require 'scoped_rolify/finders'
|
6
7
|
|
7
8
|
class MissingResourceError < StandardError; end
|
8
9
|
class InstanceResourceError < StandardError; end
|
10
|
+
class PersistenceError < StandardError; end
|
9
11
|
|
10
12
|
module ScopedRolify
|
11
13
|
# Your code goes here...
|
@@ -2,18 +2,18 @@ module Rolify
|
|
2
2
|
module Finders
|
3
3
|
|
4
4
|
def with_scoped_role role_name, resource
|
5
|
-
|
6
|
-
raise InstanceResourceError, "You should provide INSTANCE resource" unless resource.respond_to?(:id)
|
7
|
-
|
5
|
+
ScopedRolify::Policy.new(self, resource).check_persisted!
|
8
6
|
self.joins(:roles).where(rolify_constraints(role_name, resource))
|
9
7
|
end
|
10
8
|
|
11
9
|
def with_any_scoped_role role_names, resource
|
10
|
+
ScopedRolify::Policy.new(self, resource).check_persisted!
|
12
11
|
self.joins(:roles).where(rolify_constraints(role_names, resource))
|
13
12
|
end
|
14
13
|
|
15
14
|
def rolify_constraints role_names, resource
|
16
15
|
raise 'You should give somes role' if role_names.nil? or (role_names||[]).empty?
|
16
|
+
ScopedRolify::Policy.new(self, resource).check_persisted!
|
17
17
|
table = Arel::Table.new(:roles)
|
18
18
|
[].tap do |_constraints|
|
19
19
|
Array.wrap(role_names).each do |name|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ScopedRolify
|
2
|
+
class Policy < Struct.new(:model, :resource)
|
3
|
+
|
4
|
+
def check!
|
5
|
+
raise MissingResourceError, "You should provide resource" unless self.resource
|
6
|
+
raise InstanceResourceError, "You should provide INSTANCE resource" unless self.resource.respond_to?(:id)
|
7
|
+
end
|
8
|
+
|
9
|
+
def check_persisted!
|
10
|
+
check!
|
11
|
+
raise PersitenceError, "You should ask on persisted resource" unless self.resource.persisted?
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/scoped_rolify/role.rb
CHANGED
@@ -1,11 +1,45 @@
|
|
1
1
|
module Rolify
|
2
2
|
module Role
|
3
|
-
|
3
|
+
|
4
4
|
def add_scope_role(role_name, resource)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
ScopedRolify::Policy.new(self, resource).check!
|
6
|
+
add_resourced_role(role_name, resource)
|
7
|
+
end
|
8
|
+
|
9
|
+
def remove_scope_role(role_name, resource)
|
10
|
+
ScopedRolify::Policy.new(self, resource).check!
|
11
|
+
remove_resourced_role(role_name, resource)
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_role?(role_name, resource = nil)
|
15
|
+
if self.new_record?
|
16
|
+
self.roles.detect { |r| r.name.to_s == role_name.to_s && (r.resource == resource || resource.nil?) }
|
17
|
+
else
|
18
|
+
self.class.adapter.where(self.roles, :name => role_name, :resource => resource)
|
19
|
+
end.present?
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def add_resourced_role(role_name, resource)
|
25
|
+
if self.new_record?
|
26
|
+
self.class.adapter.role_class.new(name: role_name, resource: resource).tap do |role|
|
27
|
+
self.roles << role
|
28
|
+
if Rolify.dynamic_shortcuts and !self.respond_to?("is_#{role_name}?".to_sym)
|
29
|
+
self.class.define_dynamic_method(role_name, resource)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
else
|
33
|
+
add_role(role_name, resource)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def remove_resourced_role(role_name, resource)
|
38
|
+
if self.new_record?
|
39
|
+
self.roles = self.roles - [self.roles.detect { |r| r.name.to_s == role_name.to_s && r.resource == resource }]
|
40
|
+
else
|
41
|
+
remove_role(role_name, resource)
|
42
|
+
end
|
9
43
|
end
|
10
44
|
|
11
45
|
end
|
@@ -1,16 +1,36 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Rolify::Role do
|
4
|
-
let(:
|
4
|
+
let(:resource) { Forum.first }
|
5
|
+
let(:user) { User.first }
|
5
6
|
|
6
|
-
subject {
|
7
|
+
subject { user }
|
7
8
|
|
8
9
|
it { expect { subject.add_scope_role(:admin, Forum) }.to raise_error InstanceResourceError }
|
9
|
-
it { expect { subject.add_scope_role(:admin,
|
10
|
+
it { expect { subject.add_scope_role(:admin, resource) }.to_not raise_error }
|
10
11
|
|
11
|
-
context '
|
12
|
-
|
13
|
-
|
12
|
+
context 'persisted way' do
|
13
|
+
let(:resource) { Forum.first }
|
14
|
+
let(:user) { User.first }
|
15
|
+
|
16
|
+
before { subject.add_scope_role(:admin, resource) }
|
17
|
+
|
18
|
+
it { expect { subject.add_scope_role(:admin, resource) }.to_not raise_error }
|
19
|
+
it { should have_role :admin, resource }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'new record way' do
|
23
|
+
let(:resource) { Forum.new name: 'forum 2' }
|
24
|
+
let(:user) { User.new login: 'john' }
|
25
|
+
|
26
|
+
before { subject.add_scope_role(:whatever, resource) }
|
27
|
+
|
28
|
+
it { should have_role :whatever, resource }
|
29
|
+
|
30
|
+
context 'remove' do
|
31
|
+
before { subject.remove_scope_role(:whatever, resource) }
|
32
|
+
it { should_not have_role :whatever, resource }
|
33
|
+
end
|
14
34
|
end
|
15
35
|
|
16
36
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scoped_rolify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel AZEMAR
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rolify
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.4'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.1'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.1'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '2.14'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '2.14'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec-rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '2.14'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.14'
|
83
83
|
description: This is a monkey patch of rolify, and add new methods
|
@@ -88,11 +88,11 @@ executables:
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
-
|
92
|
-
-
|
93
|
-
-
|
94
|
-
-
|
95
|
-
-
|
91
|
+
- .coveralls.yml
|
92
|
+
- .gitignore
|
93
|
+
- .ruby-gemset
|
94
|
+
- .ruby-version
|
95
|
+
- .travis.yml
|
96
96
|
- CHANGELOG.md
|
97
97
|
- Gemfile
|
98
98
|
- Guardfile
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- bin/publish
|
103
103
|
- lib/scoped_rolify.rb
|
104
104
|
- lib/scoped_rolify/finders.rb
|
105
|
+
- lib/scoped_rolify/policy.rb
|
105
106
|
- lib/scoped_rolify/role.rb
|
106
107
|
- lib/scoped_rolify/version.rb
|
107
108
|
- scoped_rolify.gemspec
|
@@ -122,17 +123,17 @@ require_paths:
|
|
122
123
|
- lib
|
123
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
125
|
requirements:
|
125
|
-
- -
|
126
|
+
- - ~>
|
126
127
|
- !ruby/object:Gem::Version
|
127
128
|
version: '2.0'
|
128
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
130
|
requirements:
|
130
|
-
- -
|
131
|
+
- - '>='
|
131
132
|
- !ruby/object:Gem::Version
|
132
133
|
version: '0'
|
133
134
|
requirements: []
|
134
135
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.0.14
|
136
137
|
signing_key:
|
137
138
|
specification_version: 4
|
138
139
|
summary: This is a monkey patch of rolify
|