indulgence 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NGZlMzlkNTA5ZjYyN2RmNjMxMjJkODc4MzY3ZDEyYWUwNmIxMTg5OA==
4
+ ZmJiMTU1MDg5ZDRhOWRmZjgwYTNhOTU1ZWRiMTcxNDMzMjU4YWU0Mg==
5
5
  data.tar.gz: !binary |-
6
- MWI5NDhhZjljNGJlNTI0MGNmNGRhN2RjNjc0M2Y1MjkxOGVkOWE4Mw==
6
+ MTA3MjYwOTkwMGQyNjk2NzZmNWE3NzdmM2I4NDYyNTMxNjFlM2Q2MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Mjk3YmNkZDQ1NmI4YzQ3YTk3ZjUzYjlmNmY5MzBjMTk4NTdkZmExYzRmNWI1
10
- YzRhZDE5ZjBmYzQ4YTY2YmQwZjg3Y2Y2YmQ0OWI0ODY3ZTZhZjlkZjBmNjU5
11
- MzA5YWQzNWVmNzkyZmIzZWQwYjZlOTI3YmUxMzAxNzYwNzNjMjE=
9
+ YTYyOWE1ZWMwODMxZjkzNjUzNmQ1NjY4OTVlM2IxZWYxODJlNGQ5YzE2YTJk
10
+ MzI4NmNiOGFmNjI0YTc0N2MxMGVhNzFlZjc4YzdmYTMxY2ZhNTJkY2VkYzQw
11
+ N2FmYjM1NTA1YTFhNGZlNjFkYzM5MzU3MTA4YjFmY2I2YjA1M2I=
12
12
  data.tar.gz: !binary |-
13
- NGYwZGE1N2I1Y2RjNGJkYzg4ODUzZWZlZGU0ODgwYTllNDg0NGYzNWQ5ZGQz
14
- OWY2OWNmOTc0ZjQzOTE4MjA5NDZkOWZjNjQzYzE0ZTVlOTYxNDE4YWJkYzNl
15
- ZTMyMmU4M2U0M2FjYzUxNjEzMGJiN2FjODRkOGI0MDYzYjlhYjA=
13
+ ZDY4Y2ZhYTZmZDE0MjBmZDZmNTQ1ZWIzNjA2OGRmYjE5ZmYwM2ZhNjczMGQ1
14
+ N2IzYmE2NjVlZGIwZGFhMmNiMTExYThlODgzMmI3ZjkzY2E0N2NlYmY3YTBl
15
+ ODFiNTJlZmE5N2U0ZmVmNDNjZmM0OTIzOTU1Y2VmYTM5ZTU3MzA=
data/README.rdoc CHANGED
@@ -455,6 +455,36 @@ With that in place:
455
455
  work_process.indulge?(user, :update) == false
456
456
  WorkProcess.indulgence(user, :create, :beginning) == [work_process]
457
457
 
458
+ == Strict mode
459
+
460
+ Imagine we have a Dog class:
461
+
462
+ class Dog
463
+ end
464
+
465
+ And we use a dog as an entity:
466
+
467
+ fido = Dog.new
468
+ thing.indulge? fido, :edit
469
+
470
+ There are two ways that we may want indulgence to behave:
471
+ * go bang;
472
+ * or handle the problem as if the entity has no role (that is: return the default abilities.)
473
+
474
+ The default behaviour is to raise a method not found error. However, if
475
+ Indulgence.strict = false, the response will match the default ability:
476
+
477
+ Indulgence.strict = true # default
478
+ thing.indulge?(fido, :edit) ---> Raises No Method Error
479
+
480
+ Indulgence.strict = false
481
+ thing.indulge?(fido, :edit) == false
482
+ thing.indulge?(fido, :read) == true
483
+
484
+ That is, in Strict mode, Indulgence will expect an entity to behave like a user
485
+ with roles, and will go bang if this is no the case. It strict mode is turned
486
+ off, Indulgence will make a best effort to guess how to behave.
487
+
458
488
  == Examples
459
489
 
460
490
  For some examples, have a look at the tests. In particular, look at the object
@@ -91,22 +91,32 @@ module Indulgence
91
91
 
92
92
  private
93
93
  def entity_role_name
94
- role = entity.send(self.class.role_method)
95
- name_method = self.class.role_name_method
96
- if role and abilities.keys.include?(role.send(name_method).to_sym)
97
- role.send(name_method).to_sym
94
+ role = entity.send(self.class.role_method) if entity_has_role_method
95
+ if role and abilities.keys.include?(role.send(role_name_method).to_sym)
96
+ role.send(role_name_method).to_sym
98
97
  else
99
98
  :default
100
99
  end
101
100
  end
102
101
 
102
+ def entity_has_role_method
103
+ return true if entity.respond_to?(self.class.role_method)
104
+ if Indulgence.strict?
105
+ raise NoMethodError.new("#{entity} has no role method '#{role_name_method}'. Unable to process #{self.class}")
106
+ end
107
+ end
108
+
103
109
  def all
104
110
  self.class.all
105
111
  end
106
112
 
107
113
  def none
108
114
  self.class.none
109
- end
115
+ end
116
+
117
+ def role_name_method
118
+ self.class.role_name_method
119
+ end
110
120
 
111
121
  def self.ability_cache
112
122
  @ability_cache ||= {}
@@ -1,9 +1,13 @@
1
1
  module Indulgence
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
+ #
8
+ # 0.1.2 Adds strict mode - to allow more flexible use (if a none user object
9
+ # is passed to indulge?, in strict mode a no method error is raised. When
10
+ # indulgence.strict = false, indulge? will use the default abilities)
7
11
  #
8
12
  # 0.1.1 Makes it easier to use the state of an object to determine permissions
9
13
  # rather than a user's role.
data/lib/indulgence.rb CHANGED
@@ -5,5 +5,11 @@ require_relative 'indulgence/indulgent'
5
5
  require_relative 'active_record/acts/indulgent'
6
6
 
7
7
  module Indulgence
8
-
8
+ def self.strict?
9
+ @strict.nil? || @strict
10
+ end
11
+
12
+ def self.strict=(boolean)
13
+ @strict = boolean
14
+ end
9
15
  end
Binary file
@@ -1,5 +1,6 @@
1
1
  require_relative '../../test_helper'
2
2
  require 'user'
3
+ require 'indulgence'
3
4
  require 'permission'
4
5
  require 'ability'
5
6
 
@@ -30,5 +31,13 @@ module Indulgence
30
31
  assert_equal Permission.none, permission.ability
31
32
  end
32
33
 
34
+ def test_creation_with_entity_that_does_not_respond_to_role
35
+ before = Indulgence.strict?
36
+ Indulgence.strict = false
37
+ permission = Permission.new([], :read)
38
+ assert_equal Permission.none, permission.ability
39
+ Indulgence.strict = before
40
+ end
41
+
33
42
  end
34
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indulgence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nichols
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-08 00:00:00.000000000 Z
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -119,28 +119,28 @@ signing_key:
119
119
  specification_version: 4
120
120
  summary: Yet another permissions gem
121
121
  test_files:
122
- - test/lib/work_process.rb
122
+ - test/units/role_test.rb
123
+ - test/units/indulgence/permission_test.rb
124
+ - test/units/indulgence/ability_test.rb
125
+ - test/units/indulgence/ability_tests/method_only.rb
126
+ - test/units/indulgence/ability_tests/with_lambdas.rb
127
+ - test/units/user_test.rb
128
+ - test/units/role_permission_test.rb
129
+ - test/units/work_process_test.rb
130
+ - test/units/thing_permission_test.rb
131
+ - test/units/thing_test.rb
123
132
  - test/lib/role.rb
124
- - test/lib/thing_permission.rb
125
- - test/lib/role_permission.rb
126
- - test/lib/thing.rb
133
+ - test/lib/work_process.rb
127
134
  - test/lib/user.rb
135
+ - test/lib/thing.rb
136
+ - test/lib/thing_permission.rb
128
137
  - test/lib/work_process_permission.rb
129
- - test/db/migrate/20141008101147_create_work_processes.rb
130
- - test/db/migrate/20130408132217_create_things.rb
131
- - test/db/migrate/20130408085511_create_users.rb
138
+ - test/lib/role_permission.rb
132
139
  - test/db/migrate/20130408103015_create_roles.rb
140
+ - test/db/migrate/20130408085511_create_users.rb
141
+ - test/db/migrate/20130408132217_create_things.rb
142
+ - test/db/migrate/20141008101147_create_work_processes.rb
133
143
  - test/db/config.yml
134
144
  - test/db/test.sqlite3.db
135
145
  - test/db/schema.rb
136
146
  - test/test_helper.rb
137
- - test/units/role_test.rb
138
- - test/units/thing_test.rb
139
- - test/units/user_test.rb
140
- - test/units/work_process_test.rb
141
- - test/units/role_permission_test.rb
142
- - test/units/indulgence/permission_test.rb
143
- - test/units/indulgence/ability_test.rb
144
- - test/units/indulgence/ability_tests/with_lambdas.rb
145
- - test/units/indulgence/ability_tests/method_only.rb
146
- - test/units/thing_permission_test.rb