Skip to content

Instantly share code, notes, and snippets.

@anon987654321
Created May 3, 2020 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anon987654321/2d1c13310e51369460d880f56c939def to your computer and use it in GitHub Desktop.
Save anon987654321/2d1c13310e51369460d880f56c939def to your computer and use it in GitHub Desktop.
commit 33f71feec0bfa11a108d7adebd5e7581cfb26b29
Author: dev <dev@dev.my.domain>
Date: Mon Mar 30 12:20:46 2020 +0200
Make first user to register admin
diff --git a/app/models/user.rb b/app/models/user.rb
index 9c72148..5339e94 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -2,4 +2,15 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable
has_many :incidents
+
+ after_create :set_admin
+
+ private
+ def set_admin
+ if User.count == 1
+ User.first.update_attribute(:admin, true)
+ else
+ return true
+ end
+ end
end
commit d8217f8214e2fdc00f1954d16071cc89a031b09f
Author: dev <dev@dev.my.domain>
Date: Mon Mar 30 11:07:23 2020 +0200
Add admin database field to user
rails generate migration AddAdminToUser admin:boolean
diff --git a/db/migrate/20200401221550_add_admin_to_user.rb b/db/migrate/20200401221550_add_admin_to_user.rb
new file mode 100644
index 0000000..ceeee17
--- /dev/null
+++ b/db/migrate/20200401221550_add_admin_to_user.rb
@@ -0,0 +1,5 @@
+class AddAdminToUser < ActiveRecord::Migration[6.0]
+ def change
+ add_column :users, :admin, :boolean
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 925e84e..6aeada5 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: 2020_03_30_072748) do
+ActiveRecord::Schema.define(version: 2020_04_01_221550) do
create_table "incidents", force: :cascade do |t|
t.integer "status"
@@ -38,6 +38,7 @@ ActiveRecord::Schema.define(version: 2020_03_30_072748) do
t.string "hospital"
t.string "department"
t.string "role"
+ t.boolean "admin"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment