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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3e13a8a7e65850a3b96cda280e51f1b418c4d5b
4
- data.tar.gz: 9d8c277b01b4dbffbcc7b17236034b218b405c89
3
+ metadata.gz: 02c456aa8ca494528222680ebc0024fc8069a91b
4
+ data.tar.gz: afe192eba189a99fec5f5eed531bb46e137e8c21
5
5
  SHA512:
6
- metadata.gz: 7e34de7d161615d1289a0e8605581745d5ba347de05f79a012eb62a6edb7d547c639e39bf853112fbc9cb8dea8d3f38a38cb6c6af4ec0ced36bd727d5e0130d0
7
- data.tar.gz: a6887d2590d790aa7399adac031bc9f7895e023762b94deeb9d5cb3cb69982d8fd41b8660b19747fabe9122f4e8555194e07f8b04bb5b3ff0553e26b33f77415
6
+ metadata.gz: cfb764ac9b475af72a8361aebcf9cb1b2ea416187dc70eceec89e691763ac9324a3b5d13c398ee9b233e4569cf732327d9cbb5c97e013486931d27559fa818d5
7
+ data.tar.gz: ad768d2fc9a2f853be116476ec50381225fcef6834789f0962cd8196d1a797ffb60b2d5451af7a3dbbd732fc1dc8642e03edd41f34f7c5ffa8611819a7d0bf7a
@@ -1 +1 @@
1
- scopable
1
+ scoped_rolify
@@ -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
@@ -5,6 +5,7 @@
5
5
  class Publish
6
6
 
7
7
  def start version
8
+ system "rvm use `cat .ruby-version`@`cat .ruby-gemset`"
8
9
  system "bundle && bundle exec rspec"
9
10
  system "gem build scoped_rolify.gemspec"
10
11
  system "git tag -a v#{version} -m 'version #{version}'"
@@ -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
- raise MissingResourceError, "You should provide resource" unless resource
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
@@ -1,11 +1,45 @@
1
1
  module Rolify
2
2
  module Role
3
-
3
+
4
4
  def add_scope_role(role_name, resource)
5
- raise MissingResourceError, 'You should provide resource' unless resource
6
- raise InstanceResourceError, 'You should provide INSTANCE resource' unless resource.respond_to?(:id)
7
-
8
- add_role(role_name, resource)
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,3 +1,3 @@
1
1
  module ScopedRolify
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Rolify::Finders do
4
4
  let(:resource) { Forum.first }
5
- let(:admin) { User.where(login: 'admin').first }
5
+ let(:admin) { User.where(login: 'admin').first }
6
6
 
7
7
  before { admin.add_role(:admin, resource) }
8
8
 
@@ -1,16 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Rolify::Role do
4
- let(:instance) { Forum.first }
4
+ let(:resource) { Forum.first }
5
+ let(:user) { User.first }
5
6
 
6
- subject { User.first }
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, instance) }.to_not raise_error }
10
+ it { expect { subject.add_scope_role(:admin, resource) }.to_not raise_error }
10
11
 
11
- context 'regular way' do
12
- before { subject.add_scope_role(:admin, instance) }
13
- it { should have_role :admin, instance }
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.3
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-05 00:00:00.000000000 Z
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
- - ".coveralls.yml"
92
- - ".gitignore"
93
- - ".ruby-gemset"
94
- - ".ruby-version"
95
- - ".travis.yml"
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.2.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