bookstore/app/controllers/application_controller.rb

28 lines
599 B
Ruby
Raw Normal View History

2021-03-19 17:31:38 +02:00
class ApplicationController < ActionController::Base
2021-03-20 20:02:37 +01:00
helper_method :current_user
helper_method :logged_in?
2021-03-21 17:02:04 +01:00
2021-03-20 20:02:37 +01:00
def current_user
User.find_by(id: session[:user_id])
end
2021-03-21 17:02:04 +01:00
2021-03-20 20:02:37 +01:00
def logged_in?
!current_user.nil?
end
2021-03-21 17:02:04 +01:00
2021-03-21 22:31:58 +01:00
protected
2021-03-21 17:02:04 +01:00
def notices_from_errors(record)
messages = record.errors.messages.map do |attribute, messages|
messages.map { |message| "#{attribute} #{message}".capitalize }
end
messages.flatten
end
2021-03-21 22:31:58 +01:00
def ensure_admin
unless current_user&.admin?
redirect_to '/welcome', notice: 'You are not allowed to perform this action'
end
end
2021-03-19 17:31:38 +02:00
end