scoped_rolify 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: becb9ed298cf08d0e5223cf5ee44a48876e77ea3
4
- data.tar.gz: d6ebfd57e5ae5628a8aa8bbd00137c9c58209759
3
+ metadata.gz: a832cd6b96763b2a9cbe528363b186e4e72d3773
4
+ data.tar.gz: a66071c37137dc12ae695544b66f958c4552ad95
5
5
  SHA512:
6
- metadata.gz: b47f00df9110bdacb21093efd53fb7376e848bfe9fab31ba5db9263f288e97db0f0252cf93cb86ccd82afc731149482fb152369c3ee8df6a2128044747003c67
7
- data.tar.gz: b72223b5507b3becc5663b6b4b2380955fe8559d4cc0ec88d6e2f0c504db09dec7eddb8aa4191d1eef9d26811e96365d92fefb0174c2e53f33b0b1f437ed941f
6
+ metadata.gz: fb11f0785a3f66d2b28b0ab884707eda1e1ed265a68eabd69561f774a6b60dbd70226ba69db0b0bcdfc9cdabe8a8192efac197d38acbce73607f3b5eaad142e0
7
+ data.tar.gz: 00c70582cec6e9e4abd1d0047ed060c7675f972ad89155e135db498980b623d5c7d465cefae8942fe2f190fcdb6c91115ae08c8a26931c6a608e4c387565bcba
@@ -1,17 +1,35 @@
1
+ ### VERSION 0.0.7
2
+
3
+ * bug fix
4
+
5
+ * refactoring
6
+ * Api signature has changed on with_scoped_role method
7
+
8
+ * enhancements
9
+
10
+ * backwards incompatible changes
11
+ * Api signature has changed on with_scoped_role method. Last argument is no longer boolean, is hash of option now.
12
+
13
+ * deprecations
14
+
15
+ * roadmap
16
+ * refactoring on root resource API
17
+
18
+
1
19
  ### VERSION 0.0.6
2
20
 
3
21
  * bug fix
4
22
 
5
23
  * enhancements
6
- * remove eager loading for dynamic method
24
+ * remove eager loading for dynamic method. Cause performance problem with huge table roles.
7
25
 
8
26
  * backwards incompatible changes
9
27
 
10
28
  * deprecations
11
29
 
12
30
  * roadmap
13
- * refactoring on root resource
14
-
31
+ * refactoring on root resource
32
+
15
33
  ### VERSION 0.0.5
16
34
 
17
35
  * bug fix
@@ -24,7 +42,7 @@
24
42
  * deprecations
25
43
 
26
44
  * roadmap
27
- * refactoring on root resource
45
+ * refactoring on root resource
28
46
 
29
47
  ### VERSION 0.0.4
30
48
 
data/README.md CHANGED
@@ -9,8 +9,10 @@
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
- https://coveralls.io/r/joel/scoped_rolify#
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.
12
+
13
+ ## Introduction
14
+
15
+ This is a hack of EppO/rolify for specific purpose and for bypassing some limitations. We want only have users scoped on specific instance of resource. We are no really interesting by hierarchy. We want use Rolify for binding some resource through roles, the resources have absolutely need to be existing.
14
16
 
15
17
  ## Installation
16
18
 
@@ -26,54 +28,70 @@ Or install it yourself as:
26
28
 
27
29
  $ gem install scoped_rolify
28
30
 
29
- ## Usage
30
31
 
31
- Method #add_scope_role map #add_role
32
+ # Roles
33
+
34
+ ## #add_role method
35
+
36
+ We have added ```add_scope_role(role_name, resource)``` method for mapped original ```add_role``` method.
37
+
38
+ This method force argument resource to be existing, and add extra feature for ```root resource``` discussed below.
39
+
40
+ usage:
32
41
 
33
- You can not add right without instance of resource
42
+ You can't add right without instance of resource
34
43
 
35
44
  user = User.find(1)
36
45
  user.add_scope_role :admin # Thrown MissingResourceError
37
46
  user.add_scope_role :moderator, Forum # Thrown InstanceResourceError
38
47
 
39
- Only this case it's possible
48
+ Only the following ways are possible
40
49
 
41
50
  user.add_scope_role :moderator, Forum.first
42
51
  user.add_scope_role :moderator, Forum.new
43
52
 
44
- You can play also with remove_scope_role
53
+ ## #remove_role method
45
54
 
46
- Method with_scoped_role and method #with_any_scoped_role
55
+ This method is mapped by #remove_scope_role
47
56
 
48
- Theu methods return users for asked roles
57
+ # Finders
49
58
 
50
- You can not call method without instance of resource
59
+ ## #with_role and #with_any_role methods
60
+
61
+ Those methods are mapped by ```#with_scoped_role``` and method ```#with_any_scoped_role```
62
+
63
+ An important enhancement here, the new methods return an ```ActiveRecord::Relation``` instead array of result! Is a big feature for us.
64
+
65
+ usage:
66
+
67
+ The methods return users for asked roles
68
+
69
+ You can't call method without instance of resource
51
70
 
52
71
  User.with_scoped_role :admin # Thrown MissingResourceError
53
72
  User.with_scoped_role :moderator, Forum # Thrown InstanceResourceError
54
73
 
55
- Only this case it's possible
56
-
57
- User.with_scoped_role :moderator, Forum.first #
74
+ Only the following ways are possible
58
75
 
59
- You can't play with none persisted object
76
+ User.with_scoped_role :moderator, Forum.first
60
77
 
78
+ ### Important limitation of finders
61
79
 
62
- Method ```with_any_scoped_role``` return an ```ActiveRecord::Relation``` of all users with all roles asked for one resource
80
+ You can't play with none persisted object
63
81
 
64
82
  ## Root Resource
65
83
 
66
- In some case you can add right on child resource instead of parent resource, the problem is you haven't access directly to objects throught Parent resource, for exemple
84
+ In some case you can add right on child resource instead of parent resource, the problem is you haven't access directly to objects through Parent resource, for exemple
67
85
 
68
- You grant user on specifc Forum, user.add_role(:moderator, Forum.first) the Forum have one Category, if you want get all moderators of this Category you can't. This modification make this possible.
86
+ You grant user on specific ```Forum```, ```user.add_scope_role(:moderator, Forum.first)``` the ```Forum``` have one ```Category```, if you want get all moderators of this ```Category``` you can't. This modification make this way possible.
69
87
 
70
88
  moderator_john = User.new
71
89
  moderator_jane = User.new
72
90
 
73
- geek_world = Category.new
91
+ hack_category = Category.new
74
92
 
75
- geek_forum = Forum.new
76
- nerd_forum = Forum.new
93
+ fair_hack_forum = Forum.new
94
+ blackhat_hack_forum = Forum.new
77
95
 
78
96
  class Forum < ActiveRecord::Base
79
97
  belongs_to :category
@@ -87,29 +105,27 @@ You grant user on specifc Forum, user.add_role(:moderator, Forum.first) the Foru
87
105
  has_many :forums
88
106
  end
89
107
 
90
- geek_world.forums << geek_forum
91
- geek_world.forums << nerd_forum
92
-
93
- moderator_john.add_role(:moderator, nerd_forum)
94
- moderator_jane.add_role(:moderator, geek_forum)
108
+ hack_category.forums << fair_hack_forum
109
+ hack_category.forums << blackhat_hack_forum
95
110
 
96
- For grab all moderators of this Category
111
+ moderator_john.add_scope_role(:moderator, blackhat_hack_forum)
112
+ moderator_jane.add_scope_role(:moderator, fair_hack_forum)
97
113
 
98
- User.with_scoped_role :moderator, geek_world, root=true
114
+ For get all moderators of this Category
99
115
 
100
- ## Roadmap
116
+ User.with_scoped_role :moderator, hack_category, scope: :root
101
117
 
102
- Refactoring on root resource API
118
+ ## Bugfix
103
119
 
104
- Change
120
+ We have fixed a bug on ```has_role?``` method for object none persisted
105
121
 
106
- User.with_scoped_role :moderator, geek_world, root=true
122
+ ## Roadmap
107
123
 
108
- to
124
+ Possible enhancements
109
125
 
110
- User.with_scoped_role :moderator, geek_world, scope: :forum
126
+ Refactoring on root resource API
111
127
 
112
- And change
128
+ Change
113
129
 
114
130
  class Forum < ActiveRecord::Base
115
131
  belongs_to :category
@@ -118,14 +134,16 @@ And change
118
134
  :category
119
135
  end
120
136
  end
121
-
137
+
122
138
  to
123
139
 
124
140
  class Forum < ActiveRecord::Base
125
141
  belongs_to :category
126
- scoped_roles belongs_to: :category
142
+ scoped_roles belongs_to: :category
127
143
  end
128
144
 
145
+ This way seem pretty nice, but need spend more time on it.
146
+
129
147
  ## Contributing
130
148
 
131
149
  1. Fork it ( http://github.com/<my-github-username>/scoped_rolify/fork )
@@ -1,34 +1,39 @@
1
1
  module Rolify
2
2
  module Finders
3
3
 
4
- def with_scoped_role role_name, resource, root_only=false
4
+ def with_scoped_role role_name, resource, options={}
5
5
  ScopedRolify::Policy.new(self, resource).check_persisted!
6
- self.joins(:roles).where(rolify_constraints(role_name, resource, root_only))
6
+ self.joins(:roles).where(rolify_constraints(role_name, resource, options))
7
7
  end
8
8
 
9
- def with_any_scoped_role role_names, resource, root_only=false
9
+ def with_any_scoped_role role_names, resource, options={}
10
10
  ScopedRolify::Policy.new(self, resource).check_persisted!
11
- self.joins(:roles).where(rolify_constraints(role_names, resource, root_only))
11
+ self.joins(:roles).where(rolify_constraints(role_names, resource, options))
12
12
  end
13
13
 
14
- def rolify_constraints role_names, resource, root_only=false
15
- raise 'You should give somes role' if role_names.nil? or (role_names||[]).empty?
16
-
14
+ def rolify_constraints role_names, resource, options={}
15
+ raise 'You should give somes role' if role_names.nil? or Array(role_names).empty?
17
16
  ScopedRolify::Policy.new(self, resource).check_persisted!
18
-
19
- table = Arel::Table.new(:roles)
20
17
 
21
- [].tap do |_constraints|
18
+ scope = options.fetch(:scope, :resource)
19
+ raise 'Only scope :resource or :root are supported' unless %i(resource root).include?(scope)
20
+
21
+ field_type, field_id = begin
22
+ case scope
23
+ when :resource
24
+ [ :resource_type, :resource_id]
25
+ when :root
26
+ [ :root_resource_type, :root_resource_id]
27
+ end
28
+ end
29
+
30
+ table = Arel::Table.new(:roles)
31
+ [].tap do |constraints|
22
32
  Array.wrap(role_names).each do |name|
23
- _constraints << [].tap do |_constraint|
24
- _constraint << table[:name].eq(name)
25
- if root_only
26
- _constraint << table[:root_resource_type].eq(resource.class.name)
27
- _constraint << table[:root_resource_id].eq(resource.id)
28
- else
29
- _constraint << table[:resource_type].eq(resource.class.name)
30
- _constraint << table[:resource_id].eq(resource.id)
31
- end
33
+ constraints << [].tap do |constraint|
34
+ constraint << table[:name].eq(name)
35
+ constraint << table[field_type].eq(resource.class.name)
36
+ constraint << table[field_id].eq(resource.id)
32
37
  end.reduce(:and)
33
38
  end
34
39
  end.reduce(:or)
@@ -1,3 +1,3 @@
1
1
  module ScopedRolify
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -42,7 +42,7 @@ describe Rolify::Finders do
42
42
  end
43
43
 
44
44
  it('should retreive super_admin') { expect(User.with_scoped_role(:super_admin, resource).to_a).to eq([super_admin]) }
45
- it('should retreive all user for Category') { expect(User.with_scoped_role(:super_admin, root_resource, true).to_a).to eq([super_admin, moderator]) }
45
+ it('should retreive all user for Category') { expect(User.with_scoped_role(:super_admin, root_resource, scope: :root).to_a).to eq([super_admin, moderator]) }
46
46
  end
47
47
  end
48
48
  end
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.6
4
+ version: 0.0.7
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-20 00:00:00.000000000 Z
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rolify