cancancan-system 1.1.2 → 1.1.3

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.
@@ -1,156 +1,155 @@
1
- module CanCanCan
2
- module System
3
- module Ability
4
-
5
- extend ActiveSupport::Concern
6
-
7
- def method_missing m, *args
8
- if m.to_s[/(.+)_abilities/]
9
- membership_abilities $1, *args
10
- else
11
- super
12
- end
13
- end
14
-
15
- def respond_to? m, include_private = false
16
- super || m.to_s[/(.+)_abilities/]
17
- end
18
-
19
- private
20
-
21
- def modify aliases
22
- alias_action *aliases, to: :modify
23
- end
24
-
25
- def abilities record_class, user, options = {}
26
- defaults = {
27
- column: 'user',
28
- polymorphic: false,
29
- public_abilities: true
30
- }
31
- options = defaults.merge options
32
-
33
- public_abilities record_class if options[:public_abilities]
34
- if user
35
- if options[:polymorphic]
36
- can :manage, record_class, "#{get_column(options[:column])}": user.id, "#{get_column(options[:column], 'type')}": user.class.name
37
- else
38
- can :manage, record_class, "#{get_column(options[:column])}": user.id
39
- end
40
- yield if block_given?
41
- end
42
- end
43
-
44
- def membership_abilities class_name, record_class, user, options = {}
45
- defaults = {
46
- scope: :membership,
47
- column: nil,
48
- polymorphic: false,
49
- acts_as_belongable: false
50
- }
51
- options = defaults.merge options
52
-
53
- user.belongable_belongings.where(scope: options[:scope].to_s).each do |belonging|
54
- if belonging.belonger_type == class_name.camelize
55
- ability = ability belonging
56
- if options[:acts_as_belongable]
57
- can ability, record_class, "#{options[:column] || class_name.pluralize}": { id: belonging.belonger_id }
58
- else
59
- if options[:polymorphic]
60
- can ability, record_class, "#{get_column(options[:column] || class_name)}": belonging.belonger_id, "#{get_column(options[:column] || class_name, 'type')}": belonging.belonger_type
61
- else
62
- can ability, record_class, "#{get_column(options[:column] || class_name)}": belonging.belonger_id
63
- end
64
- end
65
- end
66
- end
67
- user.send("#{class_name.pluralize}").each do |object|
68
- if options[:acts_as_belongable]
69
- can :manage, record_class, "#{options[:column] || class_name.pluralize}": { id: object.id }
70
- else
71
- if options[:polymorphic]
72
- can :manage, record_class, "#{get_column(options[:column] || class_name)}": object.id, "#{get_column(options[:column] || class_name, 'type')}": object.class.name
73
- else
74
- can :manage, record_class, "#{get_column(options[:column] || class_name)}": object.id
75
- end
76
- end
77
- end
78
- end
79
-
80
- def belongable_abilities record_class, user, options = {}
81
- defaults = {
82
- scope: nil
83
- }
84
- options = defaults.merge options
85
-
86
- if options[:scope].nil?
87
- user.belongable_belongings.each do |belonging|
88
- belongable_belonging belonging
89
- end
90
- else
91
- user.belongable_belongings.where(scope: options[:scope].to_s).each do |belonging|
92
- belongable_belonging belonging
93
- end
94
- end
95
- end
96
- def belongable_belonging belonging
97
- if belonging.belonger_type == record_class.name
98
- ability = ability belonging
99
- can ability, record_class, id: belonging.belonger_id if ability
100
- end
101
- end
102
-
103
- def belonger_abilities record_class, user, options = {}
104
- defaults = {
105
- scope: nil
106
- }
107
- options = defaults.merge options
108
-
109
- if options[:scope].nil?
110
- user.belonger_belongings.each do |belonging|
111
- belonger_belonging belonging
112
- end
113
- else
114
- user.belonger_belongings.where(scope: options[:scope].to_s).each do |belonging|
115
- belonger_belonging belonging
116
- end
117
- end
118
- end
119
- def belonger_belonging belonging
120
- if belonging.belongable_type == record_class.name
121
- ability = ability belonging
122
- can ability, record_class, id: belonging.belongable_id if ability
123
- end
124
- end
125
-
126
- def public_abilities record_class
127
- can :manage, record_class, ability: 'admin', visibility: 'public'
128
- can :modify, record_class, ability: 'user', visibility: 'public'
129
- can :read, record_class, ability: 'guest', visibility: 'public'
130
- end
131
-
132
- def ability object
133
- case object.ability
134
- when 'admin'
135
- :manage
136
- when 'user'
137
- :modify
138
- when 'guest'
139
- :read
140
- else
141
- object.ability&.to_sym
142
- end
143
- end
144
-
145
-
146
- def get_column column, name = 'id'
147
- if column.nil? || column == ''
148
- name
149
- else
150
- "#{column}_#{name}"
151
- end
152
- end
153
-
154
- end
155
- end
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
@@ -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
- module System
7
-
8
- require 'cancancan-system/engine'
9
-
10
- end
8
+ module System
9
+ require 'cancancan-system/engine'
10
+ end
11
11
  end
@@ -1,8 +1,10 @@
1
- require 'rails/railtie'
2
-
3
- module CanCanCan
4
- module System
5
- class Engine < Rails::Engine
6
- end
7
- end
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
- module CanCanCan
2
- module System
3
-
4
- VERSION = '1.1.2'
5
-
6
- end
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
- include Rails::Generators::Migration
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
- def self.next_migration_number dirname
12
- if ActiveRecord::Base.timestamped_migrations
13
- Time.now.utc.strftime '%Y%m%d%H%M%S'
14
- else
15
- "%.3d" % ( current_migration_number(dirname) + 1 )
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
- def create_migration_file
20
- migration_template 'migration.rb.erb', 'db/migrate/cancancan_system_migration.rb', migration_version: migration_version
21
- end
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
- def show_readme
24
- readme 'README.md'
25
- end
26
+ def show_readme
27
+ readme 'README.md'
28
+ end
26
29
 
27
- private
30
+ private
28
31
 
29
- def migration_version
30
- if Rails.version >= '5.0.0'
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
- class CancancanSystemMigration < ActiveRecord::Migration<%= migration_version %>
2
- def change
3
- add_column :belongings, :ability, :string, default: 'guest'
4
- end
5
- end
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.2
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-05-24 00:00:00.000000000 Z
11
+ date: 2018-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: railties
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
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.0'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
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: '5.0'
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: '5.0'
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: acts_as_belongable
56
+ name: railties
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.1'
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.1'
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: '3.7'
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: '3.7'
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.52'
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.52'
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.4
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