Skip to content

Commit

Permalink
feature: configure home path (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev authored Apr 8, 2021
1 parent e34e783 commit 635b329
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/controllers/avo/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class HomeController < ApplicationController
def index
@page_title = "Get started"

unless Rails.env.development?
if Avo.configuration.home_path.present?
redirect_to Avo.configuration.home_path
elsif !Rails.env.development?
redirect_to resources_path Avo::App.get_resources.min_by { |resource| resource.route_key }.model_class
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/views/avo/sidebar/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<div class="flex-1 flex flex-col justify-between">
<div class="tools py-4">
<%= render Avo::NavigationLinkComponent.new label: 'Get started', path: Avo.configuration.root_path, active: :exclusive if Rails.env.development? %>
<%= render Avo::NavigationLinkComponent.new label: 'Get started', path: Avo.configuration.root_path, active: :exclusive if Rails.env.development? && Avo.configuration.home_path.nil? %>

<%= render Avo::NavigationHeadingComponent.new label: t('avo.resources') %>

<div class="w-full">
Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ coverage:
patch:
default:
enabled: false
project:
default:
target: 90%
6 changes: 6 additions & 0 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Configuration
attr_accessor :context
attr_accessor :display_breadcrumbs
attr_accessor :initial_breadcrumbs
attr_accessor :home_path

def initialize
@root_path = "/avo"
Expand Down Expand Up @@ -54,6 +55,7 @@ def initialize
add_breadcrumb I18n.t("avo.home").humanize, avo.root_path
}
@display_breadcrumbs = true
@home_path = nil
end

def locale_tag
Expand Down Expand Up @@ -81,6 +83,10 @@ def set_context(&block)
def set_initial_breadcrumbs(&block)
@initial_breadcrumbs = block if block.present?
end

def namespace
root_path.delete "/"
end
end

def self.configuration
Expand Down
11 changes: 10 additions & 1 deletion lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Avo.configure do |config|
# }

## == Localization ==
# config.locale = 'en-US'

## == Customization ==
# config.app_name = 'Avocadelicious'
# config.locale = 'en-US'
# config.timezone = 'UTC'
# config.currency = 'USD'
# config.per_page = 24
Expand All @@ -43,4 +43,13 @@ Avo.configure do |config|
# config.full_width_container = false
# config.full_width_index_view = false
# config.cache_resources_on_index_view = true

# Where should the user be redirected when he hits the `/<%= options[:path] %>` url
# config.home_path = nil

## == Breadcrumbs ==
# config.display_breadcrumbs = true
# config.set_initial_breadcrumbs do
# add_breadcrumb "Home", '/<%= options[:path] %>'
# end
end
2 changes: 1 addition & 1 deletion lib/generators/avo/tool_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def #{file_name}
template "tool/view.tt", "app/views/avo/tools/#{file_name}.html.erb"

route <<-ROUTE
scope :#{::Avo.configuration.root_path.gsub('/', '')} do
scope :#{::Avo.configuration.namespace} do
get "#{file_name}", to: "avo/tools##{file_name}"
end
ROUTE
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/app/controllers/avo/tools_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Avo::ToolsController < Avo::ApplicationController
def custom_tool
@page_title = "Custom tool page title"
add_breadcrumb "Custom tool"
def dashboard
@page_title = "Dashboard"
add_breadcrumb "Dashboard"
end
end

This file was deleted.

1 change: 1 addition & 0 deletions spec/dummy/app/views/avo/sidebar/items/_dashboard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render Avo::NavigationLinkComponent.new label: 'Dashboard', path: '/avo/dashboard' %>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex flex-col">
<%= render Avo::PanelComponent.new title: 'Custom tool' do |c| %>
<%= render Avo::PanelComponent.new title: 'Dashboard' do |c| %>
<% c.tools do %>
<div class="text-sm italic">This is the panels tools section.</div>
<% end %>
Expand All @@ -10,15 +10,15 @@
<h3>What a nice new tool 👋</h3>

<p>
You can edit this file here <code class='p-1 rounded bg-gray-500 text-white text-sm'>app/views/avo/tools/custom_tool.html.erb</code>.
You can edit this file here <code class='p-1 rounded bg-gray-500 text-white text-sm'>app/views/avo/tools/dashboard.html.erb</code>.
</p>

<p>
For this tool we created the <code class='p-1 rounded bg-gray-500 text-white text-sm'>custom_tool</code> method in the <code class='p-1 rounded bg-gray-500 text-white text-sm'>Avo::ToolsController</code>. You may delete it or move it to your own controller.
For this tool we created the <code class='p-1 rounded bg-gray-500 text-white text-sm'>dashboard</code> method in the <code class='p-1 rounded bg-gray-500 text-white text-sm'>Avo::ToolsController</code>. You may delete it or move it to your own controller.
</p>

<p>
A new <code class='p-1 rounded bg-gray-500 text-white text-sm'>Custom tool</code> item should have been added to the sidebar. Edit it in <code class='p-1 rounded bg-gray-500 text-white text-sm'>app/views/avo/sidebar/items/_custom_tool.html.erb</code>
A new <code class='p-1 rounded bg-gray-500 text-white text-sm'>Dashboard</code> item should have been added to the sidebar. Edit it in <code class='p-1 rounded bg-gray-500 text-white text-sm'>app/views/avo/sidebar/items/_dashboard.html.erb</code>
</p>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
params: request.params
}
end
config.home_path = "/avo/dashboard"
config.set_initial_breadcrumbs do
add_breadcrumb "Dashboard", "/avo/dashboard"
end
end
4 changes: 2 additions & 2 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
authenticate :user, ->(user) { user.is_admin? } do
mount Avo::Engine => Avo.configuration.root_path

namespace :avo do
get "custom_tool", to: "tools#custom_tool"
scope :avo do
get "dashboard", to: "avo/tools#dashboard"
end
end
end
6 changes: 3 additions & 3 deletions spec/features/avo/breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

describe "with breadcrumbs" do
it { is_expected.to have_css ".breadcrumbs" }
it { is_expected.to have_text "Home\n \n\nProjects\n \n\n#{project.name}\n \n\nEdit\n" }
it { is_expected.to have_text "Dashboard\n \n\nProjects\n \n\n#{project.name}\n \n\nEdit\n" }
end

describe "on a custom tool" do
let!(:url) { "/avo/custom_tool" }
let!(:url) { "/avo/dashboard" }

it { is_expected.to have_css ".breadcrumbs" }
it { is_expected.to have_text "Home\n \n\nCustom tool\n" }
it { is_expected.to have_text "Dashboard\n \n\nDashboard\n" }
end
end
13 changes: 13 additions & 0 deletions spec/features/avo/configuration_home_path_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"

RSpec.feature "Avo.configuration", type: :feature do
describe ".home_path" do
context "when set" do
it "redirects to that path" do
visit "/avo"

expect(current_path).to eql "/avo/dashboard"
end
end
end
end
8 changes: 6 additions & 2 deletions spec/features/avo/custom_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

subject { page.body }

it { is_expected.to have_link "Custom tool", href: "/avo/custom_tool" }
it { is_expected.to have_link "Dashboard", href: "/avo/dashboard" }

it "navigates to the custom tool page" do
subject

click_on "Custom tool"
click_on "Dashboard"

expect(page.body).to have_text "What a nice new tool"
expect(page.body).to have_text "app/views/avo/tools/dashboard.html.erb"
expect(page.body).to have_text "app/views/avo/sidebar/items/_dashboard.html.erb"
expect(find(".text-2xl.tracking-normal.font-bold.text-gray-800.truncate")).to have_text "Dashboard"
expect(find(".breadcrumbs")).to have_text "Dashboard"
end
end
end

0 comments on commit 635b329

Please sign in to comment.