46 lines
903 B
Ruby
46 lines
903 B
Ruby
class ApplicationController < ActionController::Base
|
|
before_action :set_locale
|
|
before_action :require_login
|
|
|
|
private
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def set_company
|
|
return unless logged_in?
|
|
|
|
@company = current_user.company
|
|
|
|
return if @company
|
|
|
|
redirect_to companies_path, alert: 'No company found. Please create a company first.'
|
|
end
|
|
|
|
def current_company
|
|
@company
|
|
end
|
|
helper_method :current_user, :logged_in?, :current_company
|
|
end
|