cancancan-system 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +330 -345
- data/app/models/concerns/can_can_can/system/ability.rb +155 -156
- data/lib/cancancan-system.rb +5 -5
- data/lib/cancancan-system/engine.rb +10 -8
- data/lib/cancancan-system/version.rb +7 -7
- data/lib/generators/cancancan_system_generator.rb +24 -22
- data/lib/generators/templates/README.md +1 -1
- data/lib/generators/templates/migration.rb.erb +7 -5
- metadata +38 -25
- data/CHANGELOG.md +0 -24
@@ -1,156 +1,155 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
public_abilities: true
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rubocop:disable Metrics/ModuleLength
|
4
|
+
module CanCanCan
|
5
|
+
module System
|
6
|
+
module Ability
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
def method_missing(method, *args)
|
10
|
+
if method.to_s[/(.+)_abilities/]
|
11
|
+
membership_abilities($1, *args)
|
12
|
+
else
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to_missing?(method, include_private = false)
|
18
|
+
super || method.to_s[/(.+)_abilities/]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def modify(aliases)
|
24
|
+
alias_action(*aliases, to: :modify)
|
25
|
+
end
|
26
|
+
|
27
|
+
# rubocop:disable Metrics/MethodLength
|
28
|
+
def abilities(klass, user, column: 'user', polymorphic: false,
|
29
|
+
public_abilities: true)
|
30
|
+
public_abilities(klass) if public_abilities
|
31
|
+
return unless user
|
32
|
+
|
33
|
+
if polymorphic
|
34
|
+
can(:manage, klass,
|
35
|
+
"#{get_column(column)}": user.id,
|
36
|
+
"#{get_column(column, 'type')}": user.class.name)
|
37
|
+
else
|
38
|
+
can(:manage, klass,
|
39
|
+
"#{get_column(column)}": user.id)
|
40
|
+
end
|
41
|
+
yield if block_given?
|
42
|
+
end
|
43
|
+
# rubocop:enable Metrics/MethodLength
|
44
|
+
|
45
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity,
|
46
|
+
# rubocop:disable Metrics/MethodLength, Metrics/ParameterLists,
|
47
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
48
|
+
def membership_abilities(class_name, klass, user, scope: :membership,
|
49
|
+
column: nil, polymorphic: false,
|
50
|
+
acts_as_belongable: false)
|
51
|
+
user.belongable_belongings.where(scope: scope).each do |belonging|
|
52
|
+
next unless belonging.belonger_type == class_name.camelize
|
53
|
+
|
54
|
+
ability = ability(belonging)
|
55
|
+
if acts_as_belongable
|
56
|
+
can(ability, klass,
|
57
|
+
"#{column || class_name.pluralize}":
|
58
|
+
{ id: belonging.belonger_id })
|
59
|
+
elsif polymorphic
|
60
|
+
can(ability, klass,
|
61
|
+
"#{get_column(column || class_name)}": belonging.belonger_id,
|
62
|
+
"#{get_column(column || class_name, 'type')}":
|
63
|
+
belonging.belonger_type)
|
64
|
+
else
|
65
|
+
can(ability, klass,
|
66
|
+
"#{get_column(column || class_name)}": belonging.belonger_id)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
user.send(class_name.pluralize).each do |object|
|
70
|
+
if acts_as_belongable
|
71
|
+
can(:manage, klass,
|
72
|
+
"#{column || class_name.pluralize}": { id: object.id })
|
73
|
+
elsif polymorphic
|
74
|
+
can(:manage, klass,
|
75
|
+
"#{get_column(column || class_name)}": object.id,
|
76
|
+
"#{get_column(column || class_name, 'type')}":
|
77
|
+
object.class.name)
|
78
|
+
else
|
79
|
+
can(:manage, klass,
|
80
|
+
"#{get_column(column || class_name)}": object.id)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity,
|
85
|
+
# rubocop:enable Metrics/MethodLength, Metrics/ParameterLists,
|
86
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
87
|
+
|
88
|
+
def belongable_abilities(klass, user, scope: nil)
|
89
|
+
if scope.nil?
|
90
|
+
user.belongable_belongings.each do |belonging|
|
91
|
+
belongable_belonging(klass, belonging)
|
92
|
+
end
|
93
|
+
else
|
94
|
+
user.belongable_belongings
|
95
|
+
.where(scope: scope).each do |belonging|
|
96
|
+
belongable_belonging(klass, belonging)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def belongable_belonging(klass, belonging)
|
102
|
+
return unless belonging.belonger_type == klass.name
|
103
|
+
|
104
|
+
ability = ability(belonging)
|
105
|
+
can(ability, klass, id: belonging.belonger_id) if ability
|
106
|
+
end
|
107
|
+
|
108
|
+
def belonger_abilities(klass, user, scope: nil)
|
109
|
+
if scope.nil?
|
110
|
+
user.belonger_belongings.each do |belonging|
|
111
|
+
belonger_belonging(klass, belonging)
|
112
|
+
end
|
113
|
+
else
|
114
|
+
user.belonger_belongings
|
115
|
+
.where(scope: scope).each do |belonging|
|
116
|
+
belonger_belonging(klass, belonging)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def belonger_belonging(klass, belonging)
|
122
|
+
return unless belonging.belongable_type == klass.name
|
123
|
+
|
124
|
+
ability = ability(belonging)
|
125
|
+
can(ability, klass, id: belonging.belongable_id) if ability
|
126
|
+
end
|
127
|
+
|
128
|
+
def public_abilities(klass)
|
129
|
+
can(:manage, klass, ability: 'admin', visibility: 'public')
|
130
|
+
can(:modify, klass, ability: 'user', visibility: 'public')
|
131
|
+
can(:read, klass, ability: 'guest', visibility: 'public')
|
132
|
+
end
|
133
|
+
|
134
|
+
def ability(object)
|
135
|
+
case object.ability
|
136
|
+
when 'admin'
|
137
|
+
:manage
|
138
|
+
when 'user'
|
139
|
+
:modify
|
140
|
+
when 'guest'
|
141
|
+
:read
|
142
|
+
else
|
143
|
+
object.ability&.to_sym
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def get_column(column, name = 'id')
|
148
|
+
return name if column.nil? || column == ''
|
149
|
+
|
150
|
+
"#{column}_#{name}"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
# rubocop:enable Metrics/ModuleLength
|
data/lib/cancancan-system.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'cancancan'
|
2
4
|
require 'acts_as_belongable'
|
3
5
|
require 'cancancan-system/version'
|
4
6
|
|
5
7
|
module CanCanCan
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
8
|
+
module System
|
9
|
+
require 'cancancan-system/engine'
|
10
|
+
end
|
11
11
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
|
5
|
+
module CanCanCan
|
6
|
+
module System
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CanCanCan
|
4
|
+
module System
|
5
|
+
VERSION = '1.1.3'
|
6
|
+
end
|
7
|
+
end
|
@@ -1,35 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
class CancancanSystemGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
source_root File.join File.dirname(__FILE__), 'templates'
|
9
|
-
desc 'Install CanCanCan System'
|
9
|
+
source_root File.join File.dirname(__FILE__), 'templates'
|
10
|
+
desc 'Install CanCanCan System'
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
12
|
+
def self.next_migration_number(dirname)
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
15
|
+
else
|
16
|
+
format('%.3d', current_migration_number(dirname) + 1)
|
17
17
|
end
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def create_migration_file
|
21
|
+
migration_template 'migration.rb.erb',
|
22
|
+
'db/migrate/cancancan_system_migration.rb',
|
23
|
+
migration_version: migration_version
|
24
|
+
end
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
def show_readme
|
27
|
+
readme 'README.md'
|
28
|
+
end
|
26
29
|
|
27
|
-
|
30
|
+
private
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
32
|
-
end
|
33
|
-
end
|
32
|
+
def migration_version
|
33
|
+
return unless Rails.version >= '5.0.0'
|
34
34
|
|
35
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
36
|
+
end
|
35
37
|
end
|
@@ -1 +1 @@
|
|
1
|
-
Now run `rails db:migrate` to add CanCanCan System to your database.
|
1
|
+
Now run `rails db:migrate` to add CanCanCan System to your database.
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CancancanSystemMigration < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def change
|
5
|
+
add_column :belongings, :ability, :string, default: 'guest'
|
6
|
+
end
|
7
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cancancan-system
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Hübotter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
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
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: acts_as_belongable
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '2.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '2.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: cancancan
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,47 +53,61 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.1'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: railties
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '2
|
61
|
+
version: '5.2'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '2
|
68
|
+
version: '5.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
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
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0
|
110
|
+
version: '0'
|
97
111
|
description: Conventions & helpers simplifying the use of CanCanCan in complex Rails
|
98
112
|
applications.
|
99
113
|
email: me@jonhue.me
|
@@ -101,7 +115,6 @@ executables: []
|
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
103
117
|
files:
|
104
|
-
- CHANGELOG.md
|
105
118
|
- LICENSE
|
106
119
|
- README.md
|
107
120
|
- app/models/concerns/can_can_can/system/ability.rb
|
@@ -131,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
144
|
version: '0'
|
132
145
|
requirements: []
|
133
146
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.7.
|
147
|
+
rubygems_version: 2.7.6
|
135
148
|
signing_key:
|
136
149
|
specification_version: 4
|
137
150
|
summary: Conventions & helpers simplifying the use of CanCanCan in complex Rails applications
|