How to correctly make a bundle in Gema the_role?

Good day, try to make a bundle heme the_role + devise + the_role_dashboard and, in particular, are faced with a problem in the bundle single user - multiple roles. Role as would be exhibited, but to the dashboard do not have access (ruby learn on the fly, do not scold severely)
routes.rb
Rails.application.routes.draw do
TheRoleManagementPanel::Routes.mixin(self)
 devise_for :users
 resources :dashboard, only: [:index]
 root 'home#index'
end


models/user.rb
class User < ApplicationRecord
 include TheRole::Api::User
 devise :database_authenticatable, :registerable,
 :recoverable, :rememberable, :trackable, :validatable
 has_many :user_roles
 has_many :roles, :through => :user_roles

 def role?(role)
 roles.any? { |r| r.name.underscore.to_sym == role }
end

end


models/user_role.rb
class UserRole < ApplicationRecord
 belongs_to :user
 belongs_to :role
end


modes/role rb
class Role < ActiveRecord::Base
 include TheRole::Api::Role
 has_many :user_roles
 has_many :users, :through => :user_roles
end


he the_role config which is in initial

config.login_required_method = :authenticate_user!
config.layout = :the_role_management_panel
config.layout_title = 'M2 Role Controller'
June 27th 19 at 15:08
1 answer
June 27th 19 at 15:10
Solution
I must say that this gem is not used, but I'll write how to do it, suddenly it is useful...

The example below describes how you can implement simple role, or a role in a "company"

Model Role
class Role < ApplicationRecord
 belongs_to :user
 belongs_to :company, optional: true

 validates :user, :name, presence: true
 enum name: %i[admin manager]
end


For example and excerpt from the model User
has_many :roles, dependent: :destroy

def create_role(role_name, company = nil)
 roles.create(name: Role.names[role_name], company: company)
end

def role?(role_name, company = nil)
 if company.present?
 roles.where(name: Role.names[role_name], company: company).any?
else
 roles.where(name: Role.names[role_name]).any?
end
end

def admin?
 role? 'admin'
end

def manager?(company)
 role? 'manager'
end


Further all is simple

# Fake
user = User.first
company = Company.first


To create an administrator role:
user.create_role :admin

To create the role of company owner:
user.create_role :manager, company

To verify the role of administrator:
user.admin?

To verify the role to:
user.manager? company

And to restrict access "on roles" by gem pundit
Perhaps somewhere sealed, wrote by hand

Find more questions by tags RubyRuby on Rails