Class: RivetCms::DashboardController

Inherits:
ApplicationController show all
Includes:
InertiaProps
Defined in:
app/controllers/rivet_cms/dashboard_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject

The dashboard stays reachable by any authenticated user, but every section's data is filtered through the same can? checks that gate the section's own pages, so it never leaks what those pages would refuse.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/rivet_cms/dashboard_controller.rb', line 8

def show
  read_content = can?(:read, :content)
  read_schema = can?(:read, :schema)

  organization = Current.organization
  content_types = read_content || read_schema ? organization.content_types.order(:name).to_a : []
  # Type cards link to entry lists, so they filter through whichever
  # record-phase read the user holds for that surface
  content_types = content_types.select { |ct|
    read_content ? can?(:read, :content, record: ct) : can?(:read, :schema, record: ct)
  }
  documents = Document.where(organization: organization).in_visible_types
  entry_counts = read_content ? documents.group(:content_type_id).count : {}

  stats = {}
  stats[:entries] = documents.count if read_content
  if read_schema
    stats[:content_types] = content_types.size
    stats[:components] = organization.components.count
  end
  read_api = can?(:read, :api)
  stats[:media_assets] = organization.media_assets.count if can?(:read, :media)
  stats[:api_tokens] = organization.api_tokens.count if read_api

  render inertia: "Dashboard/Show", props: {
    stats: stats,
    has_fields: read_schema && Field.where(content_type_id: content_types.map(&:id)).exists?,
    recent_documents: recent_documents(documents, read_content),
    content_types: content_types.map { |content_type|
      content_type_ref_props(content_type).merge(entry_count: entry_counts.fetch(content_type.id, 0))
    },
    api: read_api && read_schema ? { base_path: File.join(root_path, "api") } : nil,
    permissions: {
      write_content: can?(:write, :content),
      write_schema: can?(:write, :schema)
    }
  }
end