diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1f1890d..90af2ed 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,19 @@ class ApplicationController < ActionController::Base helper_method :current_user helper_method :logged_in? + def current_user User.find_by(id: session[:user_id]) end + def logged_in? !current_user.nil? end + + def notices_from_errors(record) + messages = record.errors.messages.map do |attribute, messages| + messages.map { |message| "#{attribute} #{message}".capitalize } + end + messages.flatten + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fe97096..e25d5f1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,8 +5,12 @@ class UsersController < ApplicationController def create @user = User.create(params.require(:user).permit(:email, :password)) - session[:user_id] = @user.id - redirect_to '/welcome', notice: 'Account has been created' + if @user.invalid? + redirect_to '/welcome', notice: notices_from_errors(@user) + else + session[:user_id] = @user.id + redirect_to '/welcome', notice: 'Account has been created' + end end def password_recovery_request diff --git a/app/models/user.rb b/app/models/user.rb index 36e30d8..174b4bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,9 @@ class User < ApplicationRecord has_secure_password has_secure_password :recovery_password, validations: false + enum role: [:customer, :admin], _default: :customer + + validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } + validates :role, presence: true + validates :password, presence: true, length: { minimum: 8 } end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e3cec58..2a12733 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,9 +12,15 @@ <%= link_to 'Home', '/welcome', method: :get%> - <% flash.each do |type, msg| %> + <% flash.each do |type, notice| %>
- <%= msg %> + <% if notice.is_a? String %> + <%= notice %> + <% else %> + <% notice.each do |msg| %> +
<%= msg %>
+ <% end %> + <% end %>
<% end %> <%= yield %> diff --git a/app/views/sessions/welcome.html.erb b/app/views/sessions/welcome.html.erb index 2f18736..dc85f35 100644 --- a/app/views/sessions/welcome.html.erb +++ b/app/views/sessions/welcome.html.erb @@ -1,8 +1,8 @@

Welcome

<% if logged_in? %> You are Logged In, <%= current_user.email %> - <%= button_to "Logout", '/logout', method: :get%> + <%= button_to "Logout", '/logout', method: :get, class: 'btn' %> <% else %> - <%= button_to "Login", '/login', method: :get, class: 'btn'%> - <%= button_to "Sign Up", '/users/new', method: :get, class: 'btn'%> + <%= button_to "Login", '/login', method: :get, class: 'btn' %> + <%= button_to "Sign Up", '/users/new', method: :get, class: 'btn' %> <% end %> diff --git a/db/migrate/20210321135711_add_role_to_users.rb b/db/migrate/20210321135711_add_role_to_users.rb new file mode 100644 index 0000000..a7a966f --- /dev/null +++ b/db/migrate/20210321135711_add_role_to_users.rb @@ -0,0 +1,5 @@ +class AddRoleToUsers < ActiveRecord::Migration[6.1] + def change + add_column :users, :role, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 25f3a5f..fb945c3 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: 2021_03_21_093857) do +ActiveRecord::Schema.define(version: 2021_03_21_135711) do create_table "authors", force: :cascade do |t| t.string "first_name" @@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 2021_03_21_093857) do t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "recovery_password_digest" + t.integer "role" end end