Class: Admin::ApplicationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/ruby_cms/templates/controllers/admin/application_controller.rb

Overview

Base for all /admin controllers. Ensures authentication and permission enforcement. Inherits from the host's ApplicationController (or config.admin_base_controller). This layout must not be used for public pages.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cms_page(key) ⇒ Object

Declare which registered page this controller serves. Looks up the permission from the nav_registry entry and sets a before_action. Usage: cms_page :media (requires a matching register_page call with key: :media)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/ruby_cms/templates/controllers/admin/application_controller.rb', line 53

def self.cms_page(key)
  page_key = key.to_sym
  before_action do
    entry = RubyCms.nav_registry.find { |e| e[:key] == page_key }
    if entry.nil?
      if defined?(Rails.logger)
        Rails.logger.warn("[RubyCMS] cms_page :#{page_key} has no matching register_page entry. " \
                          "Only manage_admin is enforced.")
      end
    elsif entry[:permission].present?
      require_permission!(entry[:permission].to_sym)
    end
  end
end

Instance Method Details

#admin_notificationsObject

Lightweight notification feed shown in the topbar bell (Noticed).



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/ruby_cms/templates/controllers/admin/application_controller.rb', line 25

def admin_notifications
  @admin_notifications ||= begin
    user = current_user_cms
    if user.nil?
      []
    else
      Rails.cache.fetch("admin.notifications.user_#{user.id}", expires_in: 30.seconds) do
        user.notifications.unread.includes(:event).order(created_at: :desc).limit(10).map do |n|
          {
            id: "noticed:#{n.id}",
            kind: n.respond_to?(:kind) ? n.kind.to_sym : :system,
            title: n.respond_to?(:title) ? n.title.to_s : n.event&.params&.dig(:title).to_s,
            body: n.respond_to?(:body) ? n.body.to_s : n.event&.params&.dig(:body).to_s,
            ts: n.created_at,
            url: n.respond_to?(:url) && n.url ? n.url : admin_audit_log_entries_path
          }
        end
      end
    end
  end
rescue StandardError => e
  Rails.logger.warn("[Admin] admin_notifications failed: #{e.class}: #{e.message}")
  []
end

#current_user_cmsObject

Public API: dashboard block +data procs and host code may call this on the controller instance.



69
70
71
# File 'lib/generators/ruby_cms/templates/controllers/admin/application_controller.rb', line 69

def current_user_cms
  @current_user_cms ||= resolve_current_user
end