diff --git a/app/models/user.rb b/app/models/user.rb index e875da7e..62e9587a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,11 @@ class User < ApplicationRecord + enum role: [:user, :moderator, :admin] + after_initialize :set_default_role, if: :new_record? + + def set_default_role + self.role ||= :user + end + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable diff --git a/db/migrate/20190818191513_add_role_to_users.rb b/db/migrate/20190818191513_add_role_to_users.rb new file mode 100644 index 00000000..5519f4f4 --- /dev/null +++ b/db/migrate/20190818191513_add_role_to_users.rb @@ -0,0 +1,5 @@ +class AddRoleToUsers < ActiveRecord::Migration[6.0] + def change + add_column :users, :role, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 3f91b52d..60acf722 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_18_162602) do +ActiveRecord::Schema.define(version: 2019_08_18_191513) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,6 +27,7 @@ ActiveRecord::Schema.define(version: 2019_08_18_162602) do t.string "unconfirmed_email" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "role" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true