Search code examples
ruby-on-railsactiveadminmulti-tenant

Active Admin Dynamic Site Title


Active Admin says you can change the site title this way:

Site Title Options You can update the title or use an optional image in the initializer also. In addition you can set the link. By default there is no link and the title is set to the name of your Rails.application class name.

# config/initializers/active_admin.rb
config.site_title = "My Admin Site"
config.site_title_link = "/"    ## Rails url helpers do not work here
config.site_title_image = "site_log_image.png"

Also, they say you can configure titles by namespace as follows:

ActiveAdmin.setup do |config|
  config.site_title = "My Default Site Title"

  config.namespace :admin do |admin|
    admin.site_title = "Admin Site"
  end

  config.namespace :super_admin do |super_admin|
    super_admin.site_title = "Super Admin Site"
  end
end

I'm building a multi tenant site and I want the site_title to be based on the current tenant name. Is there a way to put a dynamic field in there that changes based on the tenant?

Thanks


Solution

  • I have achieved this by monkey-patching the SiteTitle class.

    Put this in an initializer and change it according to your user model:

    ActiveAdmin::Views::SiteTitle.class_eval do
        private
        def title_text
            if current_admin_user
                if current_admin_user.has_role? :admin
                    "Site Admin"
                elsif current_admin_user.has_role? :guest
                    "Site Guest"
                else
                    helpers.render_or_call_method_or_proc_on(self, @namespace.site_title)
                end
            else
                helpers.render_or_call_method_or_proc_on(self, @namespace.site_title)
            end
        end
    end