Files
old-zsterminator/app/controllers/application_controller.rb

46 lines
903 B
Ruby
Raw Permalink Normal View History

2024-08-04 07:06:03 +02:00
class ApplicationController < ActionController::Base
2025-04-23 07:45:33 +02:00
before_action :set_locale
2025-08-19 07:24:18 +02:00
before_action :require_login
2025-04-23 07:45:33 +02:00
2024-08-06 14:16:40 +02:00
private
2025-02-17 19:12:40 +01:00
2025-04-23 07:45:33 +02:00
def set_locale
I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
session[:locale] = I18n.locale
end
def default_url_options
{ locale: I18n.locale }
end
2025-08-19 07:24:18 +02:00
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
def require_login
return if logged_in?
flash[:alert] = t('sessions.login_required')
redirect_to login_path
end
2024-08-06 14:16:40 +02:00
def set_company
2025-08-19 07:24:18 +02:00
return unless logged_in?
@company = current_user.company
return if @company
redirect_to companies_path, alert: 'No company found. Please create a company first.'
2024-08-06 14:16:40 +02:00
end
2025-08-19 07:24:18 +02:00
def current_company
@company
end
2025-08-19 07:24:18 +02:00
helper_method :current_user, :logged_in?, :current_company
2024-08-04 07:06:03 +02:00
end