Add roles permission to seed
askareija opened this issue · comments
nice plugins, i'm using this for my project.
i created a admin seed which is look like this:
# Creating Super Admin role
puts "Generating admin role"
role = Role.create(name: "Super Admin")
# Creating user & profile
puts "Generating admin user"
user = User.create(
email: "superadmin@mailinator.com",
username: "admin",
role_id: role.id,
password: "123123",
active: true
)
But the problem is when i try to reset all the database, and seed it. Of course i need to manually do rake voltex
to generate the roles permissions. So how can i add all permissions to admin using seeds without doing some roles edit in voltex/roles/1/edit/
?
i've done this in seeds.rb
but it still giving me access denied
puts "Generating permissions"
`rake voltex`
puts "Generating super admin permissions"
Permission.all.each do |permission|
sql = "INSERT INTO permissions_roles (permission_id, role_id) VALUES (#{permission.id},#{role.id})"
ActiveRecord::Base.connection.execute(sql)
end
Thank you.
You can do the following:
First create voltex permissions:
rake voltex
And after that, create default roles in your application with a rake task and assign permissions:
namespace :roles do
desc 'Creates roles'
task create: :environment do
ROLES = [
{ name: 'Super Admin', deletable: false, key: :superadmin },
{ name: 'Administrador', deletable: false, key: :administrator }
]
ROLES.each do |attrs|
role = Role.where(name: attrs[:name]).first_or_initialize
role.permissions = Permission.all
role.update(attrs)
end
end
end
Then, you can create your super admin user with your seed:
# Creating Super Admin role
puts "Generating admin role"
role = Role.create(name: "Super Admin")
# Creating user & profile
puts "Generating admin user"
user = User.create(
email: "superadmin@mailinator.com",
username: "admin",
role_id: role.id,
password: "123123",
active: true
)
I hope this can help you!
I have a rake task that seed my entire environment like this:
namespace :app do
desc 'Initialize app'
task init: :environment do
Rake::Task['db:drop'].execute
Rake::Task['db:create'].execute
Rake::Task['db:migrate'].execute
Rake::Task['voltex'].execute
Rake::Task['roles:create'].execute
Rake::Task['users:create'].execute
end
end
hmm, ok i understand now, i have to use rake task to generate the roles & permissions instead of putting it all on seeds.rb
thanks for explaining it to me, this helps a lot! <3