forked from fixin.me/fixin.me
Resolves conflicts between upstream/master and upstream/css-cleanup: - CSS: take css-cleanup versions (button/link style unification, comment cleanup, table .button styles, [name=cancel]/.auxiliary styles) - application_helper.rb: use SVG icon for flash message close button (css-cleanup) - users_test.rb: use single-quote style (css-cleanup), keep sole-admin test (master) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
2.9 KiB
Ruby
87 lines
2.9 KiB
Ruby
class ApplicationController < ActionController::Base
|
|
# Turbo-rails disables layout rendering for turbo_frame requests (i.e.
|
|
# requests that specify 'turbo-frame:' header).
|
|
# As a side effect, this also disables layout for turbo_stream requests that
|
|
# happen to originate from within turbo frame (e.g. turbo_frame_tag or tag
|
|
# with 'is="turbo-frame"' attribute). To fix this, either frame tags must not be
|
|
# used, or custom layout method needs to be defined.
|
|
|
|
helper_method :current_user_disguised?
|
|
helper_method :current_tab
|
|
|
|
before_action :redirect_to_setup_if_needed
|
|
before_action :authenticate_user!
|
|
|
|
class AccessForbidden < StandardError; end
|
|
class ParameterInvalid < StandardError; end
|
|
|
|
# Exceptions are handled depending on request format:
|
|
# * HTML is handled by PublicExceptions, resulting in display of
|
|
# 'public/<status-code>.html' template.
|
|
# * TURBO_STREAM is handled by method specified below, which writes flash
|
|
# message and forces redirect to referer - to display flash and make page
|
|
# content consistent with database (which may or may not have been
|
|
# modified before exception).
|
|
# This requires referer to be available in TURBO_STREAM format. Otherwise
|
|
# Turbo will reload 2nd time with HTML format and flashes will be lost.
|
|
rescue_from *ActionDispatch::ExceptionWrapper.rescue_responses.keys, with: :rescue_turbo
|
|
|
|
# Required by #respond_with (gem `responders`) used by Devise controllers.
|
|
respond_to :html, :turbo_stream
|
|
|
|
def after_sign_in_path_for(resource)
|
|
# TODO: allow setting path per-user or save last path in session and restore
|
|
units_path
|
|
end
|
|
|
|
def after_sign_out_path_for(resource)
|
|
new_user_session_path
|
|
end
|
|
|
|
protected
|
|
|
|
def current_user_disguised?
|
|
session[:revert_to_id].present?
|
|
end
|
|
|
|
class << self
|
|
attr_reader :navigation_menu_tab
|
|
def navigation_tab(name)
|
|
@navigation_menu_tab = name.to_s
|
|
end
|
|
end
|
|
def current_tab
|
|
self.class.navigation_menu_tab || controller_name
|
|
end
|
|
|
|
private
|
|
|
|
# Redirect to the web setup wizard when the application has not yet been
|
|
# initialised (i.e. no admin account exists in the database).
|
|
def redirect_to_setup_if_needed
|
|
return if User.exists?(status: :admin)
|
|
redirect_to new_setup_path
|
|
rescue ActiveRecord::StatementInvalid
|
|
# Tables may not exist yet (migrations not run). Fall through and let the
|
|
# normal request handling surface a meaningful error.
|
|
end
|
|
|
|
def render_no_content(record)
|
|
helpers.render_errors(record)
|
|
render html: nil, layout: true
|
|
end
|
|
|
|
def rescue_turbo(exception)
|
|
raise unless request.format.to_sym == :turbo_stream
|
|
|
|
message_id = ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.to_s]
|
|
flash.alert = t("actioncontroller.exceptions.status.#{message_id}")
|
|
redirect_to request.referer
|
|
end
|
|
|
|
def run_and_render(action)
|
|
send action
|
|
render action
|
|
end
|
|
end
|