Files
nairobi/app/controllers/management/base_controller.rb
Javi Martín 18611f32f1 Allow rendering different views per tenant
Sometimes it might be convenient to use completely different views for
different tenants. For example, a certain tenant might use a footer that
has nothing to do with the default one.

For these cases, instead of adding `case Tenant.current_schema`
conditions to the view, it might be tidier to use a different file.

For this purpose, we're using Rails variants [1], which means that a
tenant named `mytenant` will use a template ending with
`.html+mytenant.erb` if it's available.

This works with components too, but has a limitation: when using the
`custom/` folder to add ERB files for a tenant, the default tenant ERB
file needs to be added to the `custom/` folder as well; if there aren't
changes to this file, a symbolic link will do.

For example, if we're writing a custom `admin/action_component` view for
the tenant `milky-way` but don't need to change this file for the
default tenant:

1. Create `app/components/custom/admin/action_component.rb` according to
   the components customizations documentation [2]
2. Create the custom view for the `milky-way` tenant and save it under
   `app/components/custom/admin/action_component.html+milky-way.erb`
3. Enter the `app/components/custom/admin/` folder and run `ln -s
   ../../admin/action_component.html.erb`

We're also adding some controller tests. Since Rails doesn't load the
middleware during controller tests, we're stubbing the `current_schema`
method directly instead of changing the subdomain of the request.

[1] https://guides.rubyonrails.org/v6.0/layouts_and_rendering.html#the-variants-option
[2] https://docs.consulproject.org/docs/english-documentation/customization/components
2022-11-29 14:01:22 +01:00

66 lines
1.6 KiB
Ruby

class Management::BaseController < ActionController::Base
include TenantVariants
include GlobalizeFallbacks
layout "management"
default_form_builder ConsulFormBuilder
before_action :verify_manager
around_action :switch_locale
helper_method :managed_user
helper_method :current_user
helper_method :manager_logged_in
private
def verify_manager
raise ActionController::RoutingError, "Not Found" if current_manager.blank?
end
def current_manager
session[:manager]
end
def current_user
managed_user
end
def managed_user
@managed_user ||= Verification::Management::ManagedUser.find(
session[:document_type],
session[:document_number]
)
end
def check_verified_user(alert_msg)
return if managed_user.persisted? && managed_user.level_two_or_three_verified?
message = managed_user.persisted? ? alert_msg : t("management.sessions.need_managed_user")
redirect_to management_document_verifications_path, alert: message
end
def switch_locale(&action)
if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
session[:locale] = params[:locale]
end
session[:locale] ||= I18n.default_locale
I18n.with_locale(session[:locale], &action)
end
def current_budget
Budget.current
end
def clear_password
session[:new_password] = nil
end
def manager_logged_in
if current_manager
@manager_logged_in = User.find_by_manager_login(session[:manager]["login"])
end
end
end