From 760bca6ff06ead6986eef8dd689aa2955276937b Mon Sep 17 00:00:00 2001 From: Stella Rouzi Date: Thu, 4 Sep 2014 15:53:47 +0300 Subject: [PATCH] task to make a user an admin. Alter migration to make admins users with old role of Admin or Organizer --- ...0140730104658_migrate_roles_for_cancancan.rb | 3 ++- lib/tasks/user.rake | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/user.rake diff --git a/db/migrate/20140730104658_migrate_roles_for_cancancan.rb b/db/migrate/20140730104658_migrate_roles_for_cancancan.rb index 89efebcd..d4259863 100644 --- a/db/migrate/20140730104658_migrate_roles_for_cancancan.rb +++ b/db/migrate/20140730104658_migrate_roles_for_cancancan.rb @@ -8,6 +8,7 @@ class MigrateRolesForCancancan < ActiveRecord::Migration Conference.all.each do |conference| if role.name == 'Admin' || role.name == 'Organizer' user.add_role :organizer, conference + user.update_columns(is_admin: true) else user.add_role role.name.parameterize.underscore.to_sym, conference end @@ -22,6 +23,6 @@ class MigrateRolesForCancancan < ActiveRecord::Migration end def down - raise ActiveRecord::IrreversibleMigration.new('Cannot reverse migration. Deleted events cannot be re-created') + raise ActiveRecord::IrreversibleMigration.new('Cannot reverse migration.') end end diff --git a/lib/tasks/user.rake b/lib/tasks/user.rake new file mode 100644 index 00000000..0e199d72 --- /dev/null +++ b/lib/tasks/user.rake @@ -0,0 +1,17 @@ +namespace :user do + desc "Makes is_admin attribute true for user based on the supplied email address." + task :admin, [:email] => :environment do |t, args| + + # Check if an email address was supplied + fail 'You need to define the email address of the user you want to make an admin.' unless args.email + + user = User.find_by(email: args.email) + # Check if a user is found based on the supplied email address + fail "There is no user with email #{args.email}!" unless user + + if user.update_columns(is_admin: true) + puts "User with email #{args.email} is now an admin!" + end + end + +end