devise_can 0.1.1 → 1.0.0.pre.beta

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.
Files changed (24) hide show
  1. checksums.yaml +4 -4
  2. data/devise_can.gemspec +2 -1
  3. data/lib/devise_can/version.rb +1 -1
  4. data/lib/generators/association/association_generator.rb +87 -0
  5. data/lib/generators/association/templates/role_migration.rb +14 -0
  6. data/lib/generators/association/templates/role_migration_existing.rb +11 -0
  7. data/lib/generators/association/templates/role_model.rb +6 -0
  8. data/lib/generators/association/templates/role_model_existing.rb +3 -0
  9. data/lib/generators/association/templates/user_role_migration.rb +14 -0
  10. data/lib/generators/association/templates/user_role_migration_existing.rb +12 -0
  11. data/lib/generators/association/templates/user_role_model.rb +7 -0
  12. data/lib/generators/association/templates/user_role_model_existing.rb +5 -0
  13. data/lib/generators/permission/permission_generator.rb +102 -0
  14. data/lib/generators/permission/templates/ability_model.rb +46 -0
  15. data/lib/generators/permission/templates/module_action_migration.rb +14 -0
  16. data/lib/generators/permission/templates/module_action_migration_existing.rb +11 -0
  17. data/lib/generators/permission/templates/module_action_model.rb +6 -0
  18. data/lib/generators/permission/templates/module_group_migration.rb +14 -0
  19. data/lib/generators/permission/templates/module_group_migration_existing.rb +11 -0
  20. data/lib/generators/permission/templates/module_group_model.rb +6 -0
  21. data/lib/generators/permission/templates/module_permission_migration.rb +14 -0
  22. data/lib/generators/permission/templates/module_permission_migration_existing.rb +13 -0
  23. data/lib/generators/permission/templates/module_permission_model.rb +7 -0
  24. metadata +38 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8b7796b966429c7831fc8e2334f9b4b06918129
4
- data.tar.gz: e8d6b04b3cdc2963fc88ca27c423289932a57531
3
+ metadata.gz: 5915c881a06176374a7e1583507eefeab4df7d32
4
+ data.tar.gz: 01827a46927d990174d08a1a19c3b6644cf08bf3
5
5
  SHA512:
6
- metadata.gz: 5645847bee5f96ec8951505dea3cd6868d2060a0a81445065eff50545a83d6f6c264a10ec11f693f72f8a0f66af35925d12ea412e9245e4e485efd317c7f8240
7
- data.tar.gz: 15d807483d8d494b354f13ff8985b6911aac5aa5eccb566a163ef1ff4ab27c9574081da06af550bcec9f2861f9dd3aa85a10b088eb360217cb8e546cc40f33cd
6
+ metadata.gz: cff298fcd9b0b3d32cf808560af7006a28a66155ca98254eddac26e69dbd365bccb5a8785ad11910c7fabb4bd0e34ca47961dd70d2cdc757999ee590a4acd51a
7
+ data.tar.gz: 5647a07ac614cbc1b43671cd9881f88d917d2c245899363d416a5ab5a8ee2c3a831d79b13b127e1f5432c1dd6480045430845dcb95d175e0845000a43ff4dddd
data/devise_can.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["Raghav.Singh@lptpl.com"]
11
11
 
12
12
  spec.summary = %q{This gem is to use for devise,cancancan and user role association.}
13
- spec.description = %q{Enjoy the finctionality of this gem.}
13
+ spec.description = %q{Enjoy the functionality of this gem.}
14
14
  spec.homepage = "https://github.com/RaghavVishnoi/devise_can.git"
15
15
  spec.license = "MIT"
16
16
 
@@ -33,5 +33,6 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "bundler", "~> 1.13"
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_dependency "devise"
36
+ spec.add_dependency "cancancan"
36
37
 
37
38
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseCan
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0-beta"
3
3
  end
@@ -0,0 +1,87 @@
1
+ require 'rails/generators/active_record'
2
+ class AssociationGenerator < ActiveRecord::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
6
+
7
+ def copy_role_migration
8
+ if (behavior == :invoke && model_exists?("role")) || (behavior == :revoke && role_migration_exists?)
9
+ migration_template "role_migration_existing.rb", "db/migrate/add_name_to_roles.rb", migration_version: migration_version
10
+ else
11
+ migration_template "role_migration.rb", "db/migrate/create_roles.rb", migration_version: migration_version
12
+ end
13
+ end
14
+
15
+ def copy_user_roles_migration
16
+ if (behavior == :invoke && model_exists?("user_role")) || (behavior == :revoke && user_role_migration_exists?)
17
+ migration_template "user_role_migration_existing.rb", "db/migrate/add_user_roles_association_to_user_roles.rb", migration_version: migration_version
18
+ else
19
+ migration_template "user_role_migration.rb", "db/migrate/create_user_roles.rb", migration_version: migration_version
20
+ end
21
+ end
22
+
23
+ def copy_role_model
24
+ if model_exists?("role")
25
+ copy_file "role_model_existing.rb","app/models/role.rb"
26
+ else
27
+ copy_file "role_model.rb","app/models/role.rb"
28
+ end
29
+ end
30
+
31
+ def copy_user_role_model
32
+ if model_exists?("user_role")
33
+ copy_file "user_role_model_existing.rb","app/models/user_role.rb"
34
+ else
35
+ copy_file "user_role_model.rb","app/models/user_role.rb"
36
+ end
37
+ end
38
+
39
+
40
+ private
41
+ def model_exists?(model_name)
42
+ File.exist?(File.join(destination_root, model_path(model_name)))
43
+ end
44
+
45
+ def role_migration_exists?
46
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_name_to_roles.rb$/).first
47
+ end
48
+
49
+ def user_role_migration_exists?
50
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_user_roles_association_to_user_roles.rb$/).first
51
+ end
52
+
53
+ def migration_path
54
+ @migration_path ||= File.join("db", "migrate")
55
+ end
56
+
57
+ def model_path(model_name)
58
+ File.join("app", "models", "#{model_name}.rb")
59
+ end
60
+
61
+ def migration_version
62
+ if rails5?
63
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
64
+ end
65
+ end
66
+
67
+ def rails5?
68
+ Rails.version.start_with? '5'
69
+ end
70
+
71
+ def role_migration_data
72
+ <<RUBY
73
+ t.string :name
74
+
75
+ RUBY
76
+ end
77
+
78
+ def user_role_migration_data
79
+ <<RUBY
80
+ t.belongs_to :user
81
+ t.belongs_to :role
82
+ RUBY
83
+ end
84
+
85
+
86
+
87
+ end
@@ -0,0 +1,14 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table :roles do |t|
4
+ <%= role_migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class AddNameToRoles < ActiveRecord::Migration
2
+ def self.up
3
+ change_column :roles,:name,:string
4
+ end
5
+
6
+ def self.down
7
+ # By default, we don't want to make any assumption about how to roll back a migration when your
8
+ # model already existed. Please edit below which fields you would like to remove in this migration.
9
+ raise ActiveRecord::IrreversibleMigration
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ class Role < ActiveRecord::Base
2
+ has_many :user_roles
3
+ has_many :users,through: :user_roles
4
+
5
+ validates :name,presence: true
6
+ end
@@ -0,0 +1,3 @@
1
+ has_many :user_roles
2
+ has_many :users,through: :user_roles
3
+ validates :name,presence: true
@@ -0,0 +1,14 @@
1
+ class CreateUserRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table :user_roles do |t|
4
+ <%= user_role_migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ class AddUserRolesAssociationToUserRoles < ActiveRecord::Migration
2
+ def self.up
3
+ change_column :user_roles,:user_id,:integer,index: true
4
+ change_column :user_roles,:role_id,:integer,index: true
5
+ end
6
+
7
+ def self.down
8
+ # By default, we don't want to make any assumption about how to roll back a migration when your
9
+ # model already existed. Please edit below which fields you would like to remove in this migration.
10
+ raise ActiveRecord::IrreversibleMigration
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class UserRole < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :role
4
+ validates :user,presence: true
5
+ validates :role,presence: true
6
+ validates_uniqueness_of :user_id, scope: :role_id
7
+ end
@@ -0,0 +1,5 @@
1
+ belongs_to :user
2
+ belongs_to :role
3
+ validates :user,presence: true
4
+ validates :role,presence: true
5
+ validates_uniqueness_of :user_id, scope: :role_id
@@ -0,0 +1,102 @@
1
+ require 'rails/generators/active_record'
2
+ class PermissionGenerator < ActiveRecord::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
6
+
7
+
8
+ def copy_module_group_migration
9
+ if (behavior == :invoke && model_exists?("module_group")) || (behavior == :revoke && role_migration_exists?)
10
+ migration_template "module_group_migration_existing.rb", "db/migrate/add_name_to_module_group.rb", migration_version: migration_version
11
+ else
12
+ migration_template "module_group_migration.rb", "db/migrate/create_module_group.rb", migration_version: migration_version
13
+ end
14
+ end
15
+
16
+ def copy_module_action_migration
17
+ if (behavior == :invoke && model_exists?("module_action")) || (behavior == :revoke && role_migration_exists?)
18
+ migration_template "module_action_migration_existing.rb", "db/migrate/add_name_to_module_action.rb", migration_version: migration_version
19
+ else
20
+ migration_template "module_action_migration.rb", "db/migrate/create_module_action.rb", migration_version: migration_version
21
+ end
22
+ end
23
+
24
+ def copy_module_permission_migration
25
+ if (behavior == :invoke && model_exists?("module_permission")) || (behavior == :revoke && role_migration_exists?)
26
+ migration_template "module_permission_migration_existing.rb", "db/migrate/add_group_action_to_module_permission.rb", migration_version: migration_version
27
+ else
28
+ migration_template "module_permission_migration.rb", "db/migrate/create_module_permission.rb", migration_version: migration_version
29
+ end
30
+ end
31
+
32
+ def copy_module_action_model
33
+ copy_file "module_action_model.rb","app/models/module_action.rb"
34
+ end
35
+
36
+ def copy_module_group_model
37
+ copy_file "module_group_model.rb","app/models/module_group.rb"
38
+ end
39
+
40
+ def copy_module_permission_model
41
+ copy_file "module_permission_model.rb","app/models/module_permission.rb"
42
+ end
43
+
44
+ def copy_ability
45
+ copy_file "ability_model.rb","app/models/ability.rb"
46
+ end
47
+
48
+ private
49
+ def model_exists?(model_name)
50
+ File.exist?(File.join(destination_root, model_path(model_name)))
51
+ end
52
+
53
+ def role_migration_exists?
54
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_name_to_roles.rb$/).first
55
+ end
56
+
57
+ def user_role_migration_exists?
58
+ Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_user_roles_association_to_user_roles.rb$/).first
59
+ end
60
+
61
+ def migration_path
62
+ @migration_path ||= File.join("db", "migrate")
63
+ end
64
+
65
+ def model_path(model_name)
66
+ File.join("app", "models", "#{model_name}.rb")
67
+ end
68
+
69
+ def migration_version
70
+ if rails5?
71
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
72
+ end
73
+ end
74
+
75
+ def rails5?
76
+ Rails.version.start_with? '5'
77
+ end
78
+
79
+ def module_group_migration_data
80
+ <<RUBY
81
+ t.string :name
82
+
83
+ RUBY
84
+ end
85
+
86
+ def module_action_migration_data
87
+ <<RUBY
88
+ t.string :name
89
+
90
+ RUBY
91
+ end
92
+
93
+ def module_permission_migration_data
94
+ <<RUBY
95
+ t.belongs_to :module_group
96
+ t.belongs_to :module_action
97
+ t.belongs_to :role
98
+ RUBY
99
+ end
100
+
101
+
102
+ end
@@ -0,0 +1,46 @@
1
+ class Ability
2
+ include CanCan::Ability
3
+
4
+ def initialize(user)
5
+ # Define abilities for the passed in user here. For example:
6
+ #
7
+ # user ||= User.new # guest user (not logged in)
8
+ # if user.admin?
9
+ # can :manage, :all
10
+ # else
11
+ # can :read, :all
12
+ # end
13
+ #
14
+ # The first argument to `can` is the action you are giving the user
15
+ # permission to do.
16
+ # If you pass :manage it will apply to every action. Other common actions
17
+ # here are :read, :create, :update and :destroy.
18
+ #
19
+ # The second argument is the resource the user can perform the action on.
20
+ # If you pass :all it will apply to every resource. Otherwise pass a Ruby
21
+ # class of the resource.
22
+ #
23
+ # The third argument is an optional hash of conditions to further filter the
24
+ # objects.
25
+ # For example, here the user can only update published articles.
26
+ #
27
+ # can :update, Article, :published => true
28
+ #
29
+ # See the wiki for details:
30
+ # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
31
+
32
+ if user.roles.pluck(:name).include?('superadmin')
33
+ can :manage, :all
34
+ else
35
+ user.roles.map{|role| role.module_permissions}.each do |permissions|
36
+ permissions.each do |permission|
37
+ can permission.module_action.name.to_sym,permission.module_group.name.constantize
38
+ end
39
+ end
40
+ end
41
+
42
+
43
+
44
+
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ class CreateModuleAction < ActiveRecord::Migration
2
+ def change
3
+ create_table :module_actions do |t|
4
+ <%= module_action_migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class AddNameToModuleAction < ActiveRecord::Migration
2
+ def self.up
3
+ change_column :module_actions,:name,:string
4
+ end
5
+
6
+ def self.down
7
+ # By default, we don't want to make any assumption about how to roll back a migration when your
8
+ # model already existed. Please edit below which fields you would like to remove in this migration.
9
+ raise ActiveRecord::IrreversibleMigration
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ class ModuleAction < ApplicationRecord
2
+
3
+ has_many :module_permissions
4
+ has_many :module_groups,through: :module_permissions
5
+
6
+ end
@@ -0,0 +1,14 @@
1
+ class CreateModuleGroup < ActiveRecord::Migration
2
+ def change
3
+ create_table :module_groups do |t|
4
+ <%= module_group_migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class AddNameToModuleGroup < ActiveRecord::Migration
2
+ def self.up
3
+ change_column :module_groups,:name,:string
4
+ end
5
+
6
+ def self.down
7
+ # By default, we don't want to make any assumption about how to roll back a migration when your
8
+ # model already existed. Please edit below which fields you would like to remove in this migration.
9
+ raise ActiveRecord::IrreversibleMigration
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ class ModuleGroup < ApplicationRecord
2
+
3
+ has_many :module_permissions
4
+ has_many :module_actions,through: :module_permissions
5
+
6
+ end
@@ -0,0 +1,14 @@
1
+ class CreateModulePermission < ActiveRecord::Migration
2
+ def change
3
+ create_table :module_permissions do |t|
4
+ <%= module_permission_migration_data -%>
5
+
6
+ <% attributes.each do |attribute| -%>
7
+ t.<%= attribute.type %> :<%= attribute.name %>
8
+ <% end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class AddGroupActionToModulePermission < ActiveRecord::Migration
2
+ def self.up
3
+ change_column :module_permissions,:module_group_id,:integer,index: true
4
+ change_column :module_permissions,:module_action_id,:integer,index: true
5
+ change_column :module_permissions,:role_id,:integer,index: true
6
+ end
7
+
8
+ def self.down
9
+ # By default, we don't want to make any assumption about how to roll back a migration when your
10
+ # model already existed. Please edit below which fields you would like to remove in this migration.
11
+ raise ActiveRecord::IrreversibleMigration
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class ModulePermission < ApplicationRecord
2
+
3
+ belongs_to :module_group
4
+ belongs_to :module_action
5
+ belongs_to :role
6
+
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_can
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0.pre.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - RaghavSingh
@@ -52,7 +52,21 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Enjoy the finctionality of this gem.
55
+ - !ruby/object:Gem::Dependency
56
+ name: cancancan
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Enjoy the functionality of this gem.
56
70
  email:
57
71
  - Raghav.Singh@lptpl.com
58
72
  executables: []
@@ -70,6 +84,26 @@ files:
70
84
  - devise_can.gemspec
71
85
  - lib/devise_can.rb
72
86
  - lib/devise_can/version.rb
87
+ - lib/generators/association/association_generator.rb
88
+ - lib/generators/association/templates/role_migration.rb
89
+ - lib/generators/association/templates/role_migration_existing.rb
90
+ - lib/generators/association/templates/role_model.rb
91
+ - lib/generators/association/templates/role_model_existing.rb
92
+ - lib/generators/association/templates/user_role_migration.rb
93
+ - lib/generators/association/templates/user_role_migration_existing.rb
94
+ - lib/generators/association/templates/user_role_model.rb
95
+ - lib/generators/association/templates/user_role_model_existing.rb
96
+ - lib/generators/permission/permission_generator.rb
97
+ - lib/generators/permission/templates/ability_model.rb
98
+ - lib/generators/permission/templates/module_action_migration.rb
99
+ - lib/generators/permission/templates/module_action_migration_existing.rb
100
+ - lib/generators/permission/templates/module_action_model.rb
101
+ - lib/generators/permission/templates/module_group_migration.rb
102
+ - lib/generators/permission/templates/module_group_migration_existing.rb
103
+ - lib/generators/permission/templates/module_group_model.rb
104
+ - lib/generators/permission/templates/module_permission_migration.rb
105
+ - lib/generators/permission/templates/module_permission_migration_existing.rb
106
+ - lib/generators/permission/templates/module_permission_model.rb
73
107
  homepage: https://github.com/RaghavVishnoi/devise_can.git
74
108
  licenses:
75
109
  - MIT
@@ -85,9 +119,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
119
  version: '0'
86
120
  required_rubygems_version: !ruby/object:Gem::Requirement
87
121
  requirements:
88
- - - ">="
122
+ - - ">"
89
123
  - !ruby/object:Gem::Version
90
- version: '0'
124
+ version: 1.3.1
91
125
  requirements: []
92
126
  rubyforge_project:
93
127
  rubygems_version: 2.5.1