scoped_rolify 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -1
- data/CHANGELOG.md +9 -0
- data/README.md +6 -2
- data/lib/scoped_rolify/finders.rb +23 -4
- data/lib/scoped_rolify/version.rb +1 -1
- data/spec/scoped_rolify/finders_spec.rb +13 -3
- data/spec/support/data.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3e13a8a7e65850a3b96cda280e51f1b418c4d5b
|
4
|
+
data.tar.gz: 9d8c277b01b4dbffbcc7b17236034b218b405c89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e34de7d161615d1289a0e8605581745d5ba347de05f79a012eb62a6edb7d547c639e39bf853112fbc9cb8dea8d3f38a38cb6c6af4ec0ced36bd727d5e0130d0
|
7
|
+
data.tar.gz: a6887d2590d790aa7399adac031bc9f7895e023762b94deeb9d5cb3cb69982d8fd41b8660b19747fabe9122f4e8555194e07f8b04bb5b3ff0553e26b33f77415
|
data/.coveralls.yml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
repo_token:
|
1
|
+
repo_token: SDzsRXsu69Pye4AaTsoAP94UsBRverfMU
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
[![Build Status](https://travis-ci.org/joel/scoped_rolify.png?branch=master)](https://travis-ci.org/joel/scoped_rolify) (Travis CI)
|
10
10
|
|
11
11
|
[![Coverage Status](https://coveralls.io/repos/joel/scoped_rolify/badge.png)](https://coveralls.io/r/joel/scoped_rolify)
|
12
|
-
|
12
|
+
https://coveralls.io/r/joel/scoped_rolify#
|
13
13
|
This is a monkey patch of rolify for specifics purposes. We want only have users scoped on specific instance of resource. We are no really interesting by hierarchy.
|
14
14
|
|
15
15
|
## Installation
|
@@ -40,7 +40,9 @@ Only this case it's possible
|
|
40
40
|
|
41
41
|
user.add_scope_role :moderator, Forum.first #
|
42
42
|
|
43
|
-
Method with_scoped_role
|
43
|
+
Method with_scoped_role and method #with_any_scoped_role
|
44
|
+
|
45
|
+
Theu methods return users for asked roles
|
44
46
|
|
45
47
|
You can not call method without instance of resource
|
46
48
|
|
@@ -51,6 +53,8 @@ Only this case it's possible
|
|
51
53
|
|
52
54
|
User.with_scoped_role :moderator, Forum.first #
|
53
55
|
|
56
|
+
Method ```with_any_scoped_role``` return an ```ActiveRecord::Relation``` of all users with all roles asked for one resource
|
57
|
+
|
54
58
|
## Contributing
|
55
59
|
|
56
60
|
1. Fork it ( http://github.com/<my-github-username>/scoped_rolify/fork )
|
@@ -1,11 +1,30 @@
|
|
1
1
|
module Rolify
|
2
2
|
module Finders
|
3
|
-
|
4
|
-
def with_scoped_role
|
3
|
+
|
4
|
+
def with_scoped_role role_name, resource
|
5
5
|
raise MissingResourceError, "You should provide resource" unless resource
|
6
6
|
raise InstanceResourceError, "You should provide INSTANCE resource" unless resource.respond_to?(:id)
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
self.joins(:roles).where(rolify_constraints(role_name, resource))
|
9
9
|
end
|
10
|
+
|
11
|
+
def with_any_scoped_role role_names, resource
|
12
|
+
self.joins(:roles).where(rolify_constraints(role_names, resource))
|
13
|
+
end
|
14
|
+
|
15
|
+
def rolify_constraints role_names, resource
|
16
|
+
raise 'You should give somes role' if role_names.nil? or (role_names||[]).empty?
|
17
|
+
table = Arel::Table.new(:roles)
|
18
|
+
[].tap do |_constraints|
|
19
|
+
Array.wrap(role_names).each do |name|
|
20
|
+
_constraints << [].tap do |_constraint|
|
21
|
+
_constraint << table[:name].eq(name)
|
22
|
+
_constraint << table[:resource_type].eq(resource.class.name)
|
23
|
+
_constraint << table[:resource_id].eq(resource.id)
|
24
|
+
end.reduce(:and)
|
25
|
+
end
|
26
|
+
end.reduce(:or)
|
27
|
+
end
|
28
|
+
|
10
29
|
end
|
11
30
|
end
|
@@ -2,9 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Rolify::Finders do
|
4
4
|
let(:resource) { Forum.first }
|
5
|
-
let(:
|
5
|
+
let(:admin) { User.where(login: 'admin').first }
|
6
6
|
|
7
|
-
before {
|
7
|
+
before { admin.add_role(:admin, resource) }
|
8
8
|
|
9
9
|
subject { User }
|
10
10
|
|
@@ -12,8 +12,18 @@ describe Rolify::Finders do
|
|
12
12
|
it { expect { subject.with_scoped_role(:admin, resource) }.to_not raise_error }
|
13
13
|
|
14
14
|
context 'regular way' do
|
15
|
-
it {
|
15
|
+
it { subject.with_scoped_role(:admin, resource).should eq([admin]) }
|
16
16
|
end
|
17
17
|
|
18
|
+
context '#with_any_scoped_role' do
|
19
|
+
let(:moderator) { subject.where(login: 'moderator').first }
|
20
|
+
before { moderator.add_role(:moderator, resource) }
|
21
|
+
it do
|
22
|
+
expect(subject.with_any_scoped_role([:admin, :moderator], resource)).to be_a(ActiveRecord::Relation)
|
23
|
+
end
|
24
|
+
it do
|
25
|
+
expect(subject.with_any_scoped_role([:admin, :moderator], resource).to_a).to eq([admin, moderator])
|
26
|
+
end
|
27
|
+
end
|
18
28
|
end
|
19
29
|
|
data/spec/support/data.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
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-
|
11
|
+
date: 2014-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rolify
|