scoped_rolify 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deaaabae41d436b0c393ceb4ecb219d420163d97
4
- data.tar.gz: 6b8e3f4dfadf46dbd23bc12bcc7548845a62390d
3
+ metadata.gz: c3e13a8a7e65850a3b96cda280e51f1b418c4d5b
4
+ data.tar.gz: 9d8c277b01b4dbffbcc7b17236034b218b405c89
5
5
  SHA512:
6
- metadata.gz: e6e5906f5c4bd2c4d87f4543f18a227d3ffb5d7cee36c91dad8e71a4f1457263026fb531d720a4b099aa0689577cf56615b7f1e86d417c2adabae981c2aa1d4d
7
- data.tar.gz: 566ae4ca33bd94717c6df4082803ee1f256b7228002d1e54d12aa6339ddcc82d2b853935029ed368fc287de4cafb4b3ada9b5d8c23f614be66c768554b681916
6
+ metadata.gz: 7e34de7d161615d1289a0e8605581745d5ba347de05f79a012eb62a6edb7d547c639e39bf853112fbc9cb8dea8d3f38a38cb6c6af4ec0ced36bd727d5e0130d0
7
+ data.tar.gz: a6887d2590d790aa7399adac031bc9f7895e023762b94deeb9d5cb3cb69982d8fd41b8660b19747fabe9122f4e8555194e07f8b04bb5b3ff0553e26b33f77415
@@ -1 +1 @@
1
- repo_token: GErR5C0zFnYNumgFd8UUE5LTq8w0ggvr4
1
+ repo_token: SDzsRXsu69Pye4AaTsoAP94UsBRverfMU
@@ -1,3 +1,12 @@
1
+ ### VERSION 0.0.3
2
+
3
+ * little changes
4
+
5
+ ### VERSION 0.0.2
6
+
7
+ * Rename gem from scopable to scoped_rolify
8
+ * Rename method from scope_role to add_scope_role
9
+
1
10
  ### VERSION 0.0.1
2
11
 
3
12
  * Resctricted add_role method
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 map #with_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(role_name, resource)
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
- with_role(role_name, resource)
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
@@ -1,3 +1,3 @@
1
1
  module ScopedRolify
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Rolify::Finders do
4
4
  let(:resource) { Forum.first }
5
- let(:user) { User.first }
5
+ let(:admin) { User.where(login: 'admin').first }
6
6
 
7
- before { user.add_role(:admin, resource) }
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 { User.with_scoped_role(:admin, resource).should eq([user]) }
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
 
@@ -1,6 +1,7 @@
1
1
  # Users
2
2
  User.destroy_all
3
3
  User.create login: 'admin'
4
+ User.create login: 'moderator'
4
5
 
5
6
  # Roles
6
7
  Role.destroy_all
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.2
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-02-28 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rolify