Skip to content

Commit

Permalink
Added Custom pages feature
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Nov 8, 2022
1 parent 5a3008c commit a4c9911
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 1 deletion.
13 changes: 13 additions & 0 deletions apps/dashboard/app/apps/nav_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def self.items(nav_config)
extend_link(matched_apps.first.links.first) if matched_apps.first && matched_apps.first.links.first
elsif nav_item.fetch(:profile, nil)
extend_link(nav_profile(nav_item, nil, nil))
elsif nav_item.fetch(:page, nil)
extend_link(nav_page(nav_item, nil, nil))
end
end
end.flatten.compact
Expand All @@ -47,6 +49,8 @@ def self.nav_menu(hash_item)
nav_apps(item, menu_title, group_title)
elsif item.fetch(:profile, nil)
nav_profile(item, menu_title, group_title)
elsif item.fetch(:page, nil)
nav_page(item, menu_title, group_title)
else
# Update subcategory if title was provided
group_title = item.fetch(:group, group_title)
Expand Down Expand Up @@ -81,6 +85,15 @@ def self.nav_profile(item, category, subcategory)
OodAppLink.new(profile_data).categorize(category: category, subcategory: subcategory)
end

def self.nav_page(item, category, subcategory)
page_code = item.fetch(:page)
page_data = item.clone
page_data[:title] = page_code.titleize unless page_data.fetch(:title, nil)
page_data[:url] = Rails.application.routes.url_helpers.custom_pages_path(page_code)
page_data[:new_tab] = false unless page_data.fetch(:new_tab, nil)
OodAppLink.new(page_data).categorize(category: category, subcategory: subcategory)
end

def self.item_from_token(token)
static_link_template = STATIC_LINKS.fetch(token.to_sym, nil)
if static_link_template
Expand Down
13 changes: 13 additions & 0 deletions apps/dashboard/app/controllers/custom_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# The controller to add new pages with layouts and widgets based on configuration.
#
# It uses a page_code coming from the URL to determine which layout to use.
# If the page_code does not match a configuration object, an error message is displayed.
class CustomPagesController < ApplicationController
def index
page_code = params[:page_code]
@page_layout = @user_configuration.custom_pages.fetch(page_code.to_sym, {})
flash.now[:alert] = t('dashboard.custom_pages.invalid', page: page_code) if @page_layout.blank?
end
end
3 changes: 3 additions & 0 deletions apps/dashboard/app/models/user_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class UserConfiguration
# New navigation definition properties
ConfigurationProperty.property(name: :nav_bar, default_value: []),
ConfigurationProperty.property(name: :help_bar, default_value: []),

# Custom pages configuration property
ConfigurationProperty.property(name: :custom_pages, default_value: {}),
].freeze

def initialize(request_hostname: nil)
Expand Down
13 changes: 13 additions & 0 deletions apps/dashboard/app/views/custom_pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= javascript_include_tag 'dashboard', nonce: true %>

<%- @page_layout.fetch(:rows, []).each do |row| -%>
<div class="row">
<%- row.fetch(:columns, []).each do |col| -%>
<div class='<%= "col-md-#{col.fetch(:width, "12")}" %>'>
<%- Array(col.fetch(:widgets, [])).each do |widget| -%>
<%= render_widget(widget.to_s) %>
<%- end -%>
</div>
<%- end -%>
</div>
<%- end -%>
5 changes: 4 additions & 1 deletion apps/dashboard/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,7 @@ en:
attachments_js: "Max attachment size is %{max}. Selected file size is %{size}"

rt:
creation_success: "Support ticket created in RequestTracker system. TicketId: %{ticket_id}"
creation_success: "Support ticket created in RequestTracker system. TicketId: %{ticket_id}"

custom_pages:
invalid: "Invalid page code: %{page}. This page has not been configured"
3 changes: 3 additions & 0 deletions apps/dashboard/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
post "/support", to: "support_ticket#create"
end

# Custom pages route
get "/custom/:page_code", to: "custom_pages#index", as: :custom_pages

match "/404", :to => "errors#not_found", :via => :all
match "/500", :to => "errors#internal_server_error", :via => :all

Expand Down
13 changes: 13 additions & 0 deletions apps/dashboard/test/apps/nav_bar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ def setup
assert_equal false, result[0].new_tab?
end

test "NavBar.items should return navigation page when nav_item has page property" do
nav_item = {
title: "Page Title",
page: "page_code"
}
result = NavBar.items([nav_item])
assert_equal 1, result.size
assert_equal "layouts/nav/link", result[0].partial_path
assert_equal "Page Title", result[0].title
assert_equal "/custom/page_code", result[0].url
assert_equal false, result[0].new_tab?
end

test "NavBar.items should return navigation link when nav_item has app property" do
nav_item = {
apps: "sys/bc_jupyter"
Expand Down
28 changes: 28 additions & 0 deletions apps/dashboard/test/controllers/custom_pages_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "test_helper"

class CustomPagesControllerTest < ActiveSupport::TestCase

test "index should set flash message if page_code configuration is not found" do
target = CustomPagesController.new
target.instance_variable_set(:@user_configuration, stub("user_configuration", {custom_pages: {}}))
target.stubs(:params).returns({page_code: "not_found_page"})
flash = ActionDispatch::Request.empty.flash
target.stubs(:flash).returns(flash)
target.expects(:t).with("dashboard.custom_pages.invalid", {:page => "not_found_page"})

target.index
end

test "index should not set flash message if page_code configuration is found" do
page_config = {
rows: []
}
target = CustomPagesController.new
target.instance_variable_set(:@user_configuration, stub("user_configuration", {custom_pages: {test_page: page_config}}))
target.stubs(:params).returns({page_code: "test_page"})
target.expects(:flash).times(0)

target.index
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3>Custom Pages Test Widget</h3>
37 changes: 37 additions & 0 deletions apps/dashboard/test/integration/custom_pages_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'test_helper'

class CustomPagesIntegrationTest < ActionDispatch::IntegrationTest

test "should render configured page layout" do
stub_user_configuration({
custom_pages: {
docs: {
rows: [
{columns: [
{
widgets: 'custom_pages_test'
}
]},
{columns: [
{
width: 8,
widgets: 'custom_pages_test'
}
]}
]
}
}
})

get custom_pages_path(page_code: "docs")

assert :success
assert_select 'div.row', 2
assert_select 'div.row > div.col-md-12', 1
assert_select 'div.row > div.col-md-12 > h3', text: "Custom Pages Test Widget"
assert_select 'div.row > div.col-md-8', 1
assert_select 'div.row > div.col-md-8 > h3', text: "Custom Pages Test Widget"
end


end
1 change: 1 addition & 0 deletions apps/dashboard/test/models/user_configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class UserConfigurationTest < ActiveSupport::TestCase
nav_categories: ["Apps", "Files", "Jobs", "Clusters", "Interactive Apps"],
nav_bar: [],
help_bar: [],
custom_pages: {},
}

# ensure all properties are tested
Expand Down

0 comments on commit a4c9911

Please sign in to comment.