indulgence 0.1.0 → 0.1.1

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTA1YTM4YmFhMGUzMTIxNzYwOWQ4YzYzZTM3NmRhNjc2YjY3NzdiZQ==
4
+ NGZlMzlkNTA5ZjYyN2RmNjMxMjJkODc4MzY3ZDEyYWUwNmIxMTg5OA==
5
5
  data.tar.gz: !binary |-
6
- OGNkZDE1ZDk0ZGE4ZjcyZTQzYjlhZGQxNTczOTVhMjY2ZTRlZTYyNg==
6
+ MWI5NDhhZjljNGJlNTI0MGNmNGRhN2RjNjc0M2Y1MjkxOGVkOWE4Mw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjNmNGMyZWEyNWIyNmRlYTJjMTcxODVkMGQ2ZWM0ODVmZGJhZTcyZWZmYzNi
10
- ZTFlMTg2ODVmNzY4NDgzMzM1Y2NmN2Q0Mzc3NzM0NDBmMDEzNDYwZTc0ZGVk
11
- YzY2ZWI2NzQ5ZDk3MjM3OWViNjNiYmQ0ZGNiNGM5NDA2OWNjMWU=
9
+ Mjk3YmNkZDQ1NmI4YzQ3YTk3ZjUzYjlmNmY5MzBjMTk4NTdkZmExYzRmNWI1
10
+ YzRhZDE5ZjBmYzQ4YTY2YmQwZjg3Y2Y2YmQ0OWI0ODY3ZTZhZjlkZjBmNjU5
11
+ MzA5YWQzNWVmNzkyZmIzZWQwYjZlOTI3YmUxMzAxNzYwNzNjMjE=
12
12
  data.tar.gz: !binary |-
13
- NTg5ODNkMmJiYTllM2M5NjU5ODg3ODcxYzdmY2M1NTc2NTBiZGQ4YmI2NjY3
14
- ZjM1YWY1OWZlMDY5NTdiMjE1NjBiMGFiYzcwYjA1MDBhNjZjNTI2MzMwYzdi
15
- NGFiYzhkMWRiZDE0ZTYwMjZiMjhlYjE5ZWU0YWFlNDRmMGQ5NDA=
13
+ NGYwZGE1N2I1Y2RjNGJkYzg4ODUzZWZlZGU0ODgwYTllNDg0NGYzNWQ5ZGQz
14
+ OWY2OWNmOTc0ZjQzOTE4MjA5NDZkOWZjNjQzYzE0ZTVlOTYxNDE4YWJkYzNl
15
+ ZTMyMmU4M2U0M2FjYzUxNjEzMGJiN2FjODRkOGI0MDYzYjlhYjA=
data/README.rdoc CHANGED
@@ -380,6 +380,81 @@ Indulgence falls back to the *none* ability if _nil_ is passed as the entity.
380
380
  thing.indule?(nil, :read) == false
381
381
  Thing.indulgence(nil, :read) --> raises ActiveRecord::RecordNotFound
382
382
 
383
+ == Basing permissions on an object's state rather than a user's role
384
+
385
+ A work process goes through a number of stages. Instead of permissions needing
386
+ to change depending on a user's role, it may be required that permissions
387
+ are dependent of the process stage. To achieve this, the association between
388
+ the host object and it's permission class would need to change. Rather than
389
+ associating the work process to its permission via acts_as_indulgent, the
390
+ indulge? and indulgence methods would need to be created directly:
391
+
392
+ class WorkProcess < ActiveRecord::Base
393
+
394
+ def indulge?(user, ability)
395
+ permission = WorkProcessPermission.new(user, ability, self.stage)
396
+ permission.compare_single self
397
+ end
398
+
399
+ def self.indulgence(user, ability, stage_name)
400
+ permission = WorkProcessPermission.new(user, ability, stage_name)
401
+ permission.filter_many(self).where(:stage => stage_name)
402
+
403
+ rescue Indulgence::NotFoundError, Indulgence::AbilityNotFound
404
+ raise ActiveRecord::RecordNotFound.new('Unable to find the item(s) you were looking for')
405
+ end
406
+
407
+ end
408
+
409
+ With that in place, abilities would need to defined by each stage name rather
410
+ than each user role. So:
411
+
412
+ class WorkProcessPermission < Indulgence::Permission
413
+
414
+ def abilities
415
+ {
416
+ beginning: beginning,
417
+ middle: middle,
418
+ finish: finish
419
+ }
420
+ end
421
+
422
+ def beginning
423
+ {
424
+ create: all,
425
+ read: all,
426
+ update: none,
427
+ delete: all
428
+ }
429
+ end
430
+
431
+ def middle
432
+ {
433
+ create: none,
434
+ read: all,
435
+ update: all,
436
+ delete: all
437
+ }
438
+ end
439
+
440
+ def finish
441
+ {
442
+ create: none,
443
+ read: all,
444
+ update: none,
445
+ delete: none
446
+ }
447
+ end
448
+
449
+ end
450
+
451
+ With that in place:
452
+
453
+ work_process = WorkProcess.create(stage: 'beginning')
454
+ work_process.indulge?(user, :create) == true
455
+ work_process.indulge?(user, :update) == false
456
+ WorkProcess.indulgence(user, :create, :beginning) == [work_process]
457
+
383
458
  == Examples
384
459
 
385
460
  For some examples, have a look at the tests. In particular, look at the object
data/Rakefile CHANGED
@@ -11,3 +11,10 @@ end
11
11
  require 'standalone_migrations'
12
12
  StandaloneMigrations::Tasks.load_tasks
13
13
 
14
+ task :console do
15
+ require 'irb'
16
+ require 'irb/completion'
17
+ require 'indulgence'
18
+ ARGV.clear
19
+ IRB.start
20
+ end
@@ -2,9 +2,10 @@ module Indulgence
2
2
  class Permission
3
3
  attr_reader :entity, :ability
4
4
 
5
- def initialize(entity, ability)
5
+ def initialize(entity, ability, role_method = nil)
6
6
  self
7
7
  @entity = entity
8
+ @role_name = role_method.to_sym if role_method
8
9
  @ability = abilities_for_role[ability]
9
10
  raise AbilityNotFound, "Unable to find an ability called #{ability}" unless @ability
10
11
  end
@@ -1,9 +1,12 @@
1
1
  module Indulgence
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
+ #
8
+ # 0.1.1 Makes it easier to use the state of an object to determine permissions
9
+ # rather than a user's role.
7
10
  #
8
11
  # 0.1.0 Handles a nil entity being passed to either indulge? or indulgence.
9
12
  # Code now tried and tested in a number of applications, so
@@ -0,0 +1,9 @@
1
+ class CreateWorkProcesses < ActiveRecord::Migration
2
+ def change
3
+ create_table :work_processes do |t|
4
+ t.string :name
5
+ t.string :stage
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
data/test/db/schema.rb CHANGED
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20130408132217) do
14
+ ActiveRecord::Schema.define(:version => 20141008101147) do
15
15
 
16
16
  create_table "roles", :force => true do |t|
17
17
  t.string "name"
@@ -33,4 +33,11 @@ ActiveRecord::Schema.define(:version => 20130408132217) do
33
33
  t.datetime "updated_at", :null => false
34
34
  end
35
35
 
36
+ create_table "work_processes", :force => true do |t|
37
+ t.string "name"
38
+ t.string "stage"
39
+ t.datetime "created_at", :null => false
40
+ t.datetime "updated_at", :null => false
41
+ end
42
+
36
43
  end
Binary file
@@ -0,0 +1,17 @@
1
+ require 'indulgence'
2
+ class WorkProcess < ActiveRecord::Base
3
+
4
+ def indulge?(user, ability)
5
+ permission = WorkProcessPermission.new(user, ability, stage)
6
+ permission.compare_single self
7
+ end
8
+
9
+ def self.indulgence(user, ability, stage_name)
10
+ permission = WorkProcessPermission.new(user, ability, stage_name)
11
+ permission.filter_many(self).where(:stage => stage_name)
12
+
13
+ rescue Indulgence::NotFoundError, Indulgence::AbilityNotFound
14
+ raise ActiveRecord::RecordNotFound.new('Unable to find the item(s) you were looking for')
15
+ end
16
+
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'indulgence'
2
+
3
+ class WorkProcessPermission < Indulgence::Permission
4
+
5
+ def abilities
6
+ {
7
+ beginning: beginning,
8
+ middle: middle,
9
+ finish: finish
10
+ }
11
+ end
12
+
13
+ def beginning
14
+ {
15
+ create: all,
16
+ read: all,
17
+ update: none,
18
+ delete: all
19
+ }
20
+ end
21
+
22
+ def middle
23
+ {
24
+ create: none,
25
+ read: all,
26
+ update: all,
27
+ delete: all
28
+ }
29
+ end
30
+
31
+ def finish
32
+ {
33
+ create: none,
34
+ read: all,
35
+ update: none,
36
+ delete: none
37
+ }
38
+ end
39
+
40
+ end
@@ -0,0 +1,79 @@
1
+ require_relative '../test_helper'
2
+ require 'user'
3
+ require 'work_process'
4
+ require 'work_process_permission'
5
+
6
+ class WorkProcessTest < Test::Unit::TestCase
7
+
8
+ def teardown
9
+ User.delete_all
10
+ WorkProcess.delete_all
11
+ end
12
+
13
+ def test_indulge_beginning
14
+ {
15
+ create: true,
16
+ read: true,
17
+ update: false,
18
+ delete: true
19
+ }.each do |ability, expected|
20
+
21
+ assert_equal(
22
+ expected,
23
+ beginning_process.indulge?(user, ability),
24
+ "User should #{'not ' unless expected}be able to #{ability}"
25
+ )
26
+ end
27
+ end
28
+
29
+ def test_indulge_middle
30
+ {
31
+ create: false,
32
+ read: true,
33
+ update: true,
34
+ delete: true
35
+ }.each do |ability, expected|
36
+
37
+ assert_equal(
38
+ expected,
39
+ middle_process.indulge?(user, ability),
40
+ "User should #{'not ' unless expected}be able to #{ability}"
41
+ )
42
+
43
+ end
44
+ end
45
+
46
+ def test_indulgence
47
+ expected = [beginning_process]
48
+ assert_equal expected, WorkProcess.indulgence(user, :create, :beginning)
49
+ end
50
+
51
+ def test_indulgence_when_not_permitted
52
+ assert_raise ActiveRecord::RecordNotFound do
53
+ WorkProcess.indulgence(user, :update, :beginning)
54
+ end
55
+ end
56
+
57
+ def user
58
+ @user ||= User.create(:name => 'Bob')
59
+ end
60
+
61
+ def beginning_process
62
+ @beginning_process ||= create_process(:beginning)
63
+ end
64
+
65
+ def middle_process
66
+ @middle_process ||= create_process(:middle)
67
+ end
68
+
69
+ def finist_process
70
+ @finish_process ||= create_process(:finist)
71
+ end
72
+
73
+ def create_process(stage)
74
+ WorkProcess.create(
75
+ name: "#{stage} process",
76
+ stage: stage.to_s
77
+ )
78
+ end
79
+ 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.0
4
+ version: 0.1.1
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-02-05 00:00:00.000000000 Z
11
+ date: 2014-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -59,37 +59,41 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - MIT-LICENSE
63
+ - README.rdoc
64
+ - Rakefile
62
65
  - lib/active_record/acts/indulgent.rb
63
- - lib/indulgence/version.rb
66
+ - lib/indulgence.rb
67
+ - lib/indulgence/ability.rb
64
68
  - lib/indulgence/exceptions.rb
65
69
  - lib/indulgence/indulgent.rb
66
- - lib/indulgence/ability.rb
67
70
  - lib/indulgence/permission.rb
68
- - lib/indulgence.rb
69
- - MIT-LICENSE
70
- - Rakefile
71
- - README.rdoc
72
- - test/units/role_test.rb
73
- - test/units/indulgence/permission_test.rb
71
+ - lib/indulgence/version.rb
72
+ - test/db/config.yml
73
+ - test/db/migrate/20130408085511_create_users.rb
74
+ - test/db/migrate/20130408103015_create_roles.rb
75
+ - test/db/migrate/20130408132217_create_things.rb
76
+ - test/db/migrate/20141008101147_create_work_processes.rb
77
+ - test/db/schema.rb
78
+ - test/db/test.sqlite3.db
79
+ - test/lib/role.rb
80
+ - test/lib/role_permission.rb
81
+ - test/lib/thing.rb
82
+ - test/lib/thing_permission.rb
83
+ - test/lib/user.rb
84
+ - test/lib/work_process.rb
85
+ - test/lib/work_process_permission.rb
86
+ - test/test_helper.rb
74
87
  - test/units/indulgence/ability_test.rb
75
88
  - test/units/indulgence/ability_tests/method_only.rb
76
89
  - test/units/indulgence/ability_tests/with_lambdas.rb
77
- - test/units/user_test.rb
90
+ - test/units/indulgence/permission_test.rb
78
91
  - test/units/role_permission_test.rb
92
+ - test/units/role_test.rb
79
93
  - test/units/thing_permission_test.rb
80
94
  - test/units/thing_test.rb
81
- - test/lib/role.rb
82
- - test/lib/user.rb
83
- - test/lib/thing.rb
84
- - test/lib/thing_permission.rb
85
- - test/lib/role_permission.rb
86
- - test/db/migrate/20130408103015_create_roles.rb
87
- - test/db/migrate/20130408085511_create_users.rb
88
- - test/db/migrate/20130408132217_create_things.rb
89
- - test/db/config.yml
90
- - test/db/test.sqlite3.db
91
- - test/db/schema.rb
92
- - test/test_helper.rb
95
+ - test/units/user_test.rb
96
+ - test/units/work_process_test.rb
93
97
  homepage: https://github.com/reggieb/indulgence
94
98
  licenses:
95
99
  - MIT-LICENSE
@@ -110,29 +114,33 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
114
  version: '0'
111
115
  requirements: []
112
116
  rubyforge_project:
113
- rubygems_version: 2.1.10
117
+ rubygems_version: 2.2.2
114
118
  signing_key:
115
119
  specification_version: 4
116
120
  summary: Yet another permissions gem
117
121
  test_files:
118
- - test/units/role_test.rb
119
- - test/units/indulgence/permission_test.rb
120
- - test/units/indulgence/ability_test.rb
121
- - test/units/indulgence/ability_tests/method_only.rb
122
- - test/units/indulgence/ability_tests/with_lambdas.rb
123
- - test/units/user_test.rb
124
- - test/units/role_permission_test.rb
125
- - test/units/thing_permission_test.rb
126
- - test/units/thing_test.rb
122
+ - test/lib/work_process.rb
127
123
  - test/lib/role.rb
128
- - test/lib/user.rb
129
- - test/lib/thing.rb
130
124
  - test/lib/thing_permission.rb
131
125
  - test/lib/role_permission.rb
132
- - test/db/migrate/20130408103015_create_roles.rb
133
- - test/db/migrate/20130408085511_create_users.rb
126
+ - test/lib/thing.rb
127
+ - test/lib/user.rb
128
+ - test/lib/work_process_permission.rb
129
+ - test/db/migrate/20141008101147_create_work_processes.rb
134
130
  - test/db/migrate/20130408132217_create_things.rb
131
+ - test/db/migrate/20130408085511_create_users.rb
132
+ - test/db/migrate/20130408103015_create_roles.rb
135
133
  - test/db/config.yml
136
134
  - test/db/test.sqlite3.db
137
135
  - test/db/schema.rb
138
136
  - 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