Skip to content

Commit

Permalink
added role column on User and seed AdminUser
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwong committed Oct 6, 2018
1 parent 71a027b commit b8ee635
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 0 additions & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def home
end

def members_only

end

def admin_only
Expand Down
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class User < ApplicationRecord
enum role: [:user, :admin]
after_initialize :set_default_role, :if => :new_record?

def set_default_role
self.role ||= :user
end

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
Expand Down
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<% end %>
</ul>
</nav>
<% if user_signed_in? %>
<p> you are signed in as <%=current_user.email%> </p>
<p> with role <%=current_user.role%> </p>
<% end %>
<%= yield %>
</body>
</html>
5 changes: 5 additions & 0 deletions db/migrate/20181006022523_add_role_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRoleToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :role, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_10_05_222150) do
ActiveRecord::Schema.define(version: 2018_10_06_022523) do

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
Expand All @@ -20,6 +20,7 @@
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
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
Expand Down
14 changes: 7 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
#Seeding admin
User.create(
:email => "[email protected]",
:password => "admin123",
:password_confirmation => "admin123",
:role => :admin
)

0 comments on commit b8ee635

Please sign in to comment.