bookstore/app/controllers/application_controller.rb

29 lines
645 B
Ruby
Raw Normal View History

2021-03-22 03:16:29 +01:00
# frozen_string_literal: true
# Base for application controllers
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-22 03:16:29 +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)
2021-03-22 03:16:29 +01:00
errors = record.errors.messages.map do |attribute, messages|
2021-03-21 17:02:04 +01:00
messages.map { |message| "#{attribute} #{message}".capitalize }
end
2021-03-22 03:16:29 +01:00
errors.flatten
2021-03-21 17:02:04 +01:00
end
2021-03-21 22:31:58 +01:00
def ensure_admin
2021-03-22 03:16:29 +01:00
redirect_to '/welcome', notice: 'You are not allowed to perform this action' unless current_user&.admin?
2021-03-21 22:31:58 +01:00
end
2021-03-19 17:31:38 +02:00
end