Skip to content

Create a dashboard page

An Easy page is a dashboard whose layout and modules users can edit. A dashboard consists of an EasyPage record, a controller registered with EasyExtensions::EasyPageHandler, routes and two views.

Paths on this page are relative to your plugin directory, for example plugins/easyproject/easy_plugins/modification_acme/.

Data migration

Create the EasyPage record in a data migration (db/data/). Its available zones are created for you from the layout.

db/data/20260101120000_create_my_dashboard.rb
class CreateMyDashboard < EasyExtensions::EasyDataMigration

  def up
    EasyPage.create!(page_name: "my-dashboard", layout_path: "easy_page_layouts/two_column_header_three_rows_right_sidebar")
  end

  def down
    EasyPage.where(page_name: "my-dashboard").destroy_all
  end

end

Available layouts are two_column_header_three_rows_right_sidebar, two_column_header_first_wider and two_column_header_and_footer (EasyPage::PAGE_LAYOUTS).

Controller

Register the page in a controller. The handler defines the show and edit actions and the page authorisation.

app/controllers/my_dashboard_controller.rb
1
2
3
4
5
6
7
8
class MyDashboardController < ApplicationController
  EasyExtensions::EasyPageHandler.register_for(self, {
    page_name: "my-dashboard",
    path: proc { my_dashboard_index_path(t: params[:t]) },
    show_action: :index,
    edit_action: :layout
  })
end

Arguments:

  • page_name: the page_name of the EasyPage record. The record must exist in the database.
  • path: a proc returning the path of the dashboard.
  • show_action: the action that displays the dashboard.
  • edit_action: the action where users add and arrange modules.

Optional page_context and entity arguments are described in EasyExtensions::EasyPageHandler.generate_module.

Routes

config/routes.rb
1
2
3
resources :my_dashboard, only: [:index], path: "/super-dashboard" do
  match :layout, action: :layout, via: %i[get post], on: :collection
end

Views

Create app/views/my_dashboard/index.html.erb and app/views/my_dashboard/layout.html.erb. render_aria_headings is required; the button opens the layout editor for users who can edit the page:

app/views/my_dashboard/index.html.erb
<%= render_aria_headings %>

<%= render_easy_page_bottom_buttons(layout_my_dashboard_index_path(t: params[:t])) if @page_editable %>
app/views/my_dashboard/layout.html.erb
<%= render_aria_headings %>

Tests

Test the routes with a request spec in test/spec/requests/my_dashboard_controller_spec.rb:

request spec
require "easy_extensions/spec_helper"

RSpec.describe "My Dashboard", logged: true do
  subject { response }

  before do
    EasyPage.create!(page_name: "my-dashboard", layout_path: "easy_page_layouts/two_column_header_three_rows_right_sidebar")
  end

  describe "GET /super-dashboard" do
    let!(:request) { get "/super-dashboard" }

    it { is_expected.to have_http_status(:forbidden) }

    context "with admin", logged: :admin do
      it { is_expected.to have_http_status(:ok) }
    end
  end

  describe "GET /super-dashboard/layout" do
    let!(:request) { get "/super-dashboard/layout" }

    it { is_expected.to have_http_status(:forbidden) }

    context "with admin", logged: :admin do
      it { is_expected.to have_http_status(:ok) }
    end
  end
end