From bf44988a302499cb85d070ace355d3915ecd284b Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 10 Jun 2015 03:59:39 -0400 Subject: Add RootController This controller is now the target for `root_url`. It sub-classes DashboardController so we can render the old default without a redirect if the user hasn't customized their dashboard location. cherry-picked --- app/controllers/root_controller.rb | 18 +++++++++++++++++ app/views/layouts/nav/_dashboard.html.haml | 2 +- config/routes.rb | 2 +- spec/controllers/root_controller_spec.rb | 32 ++++++++++++++++++++++++++++++ spec/routing/routing_spec.rb | 10 ++++++++-- 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 app/controllers/root_controller.rb create mode 100644 spec/controllers/root_controller_spec.rb diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb new file mode 100644 index 00000000000..7606d2d0fbe --- /dev/null +++ b/app/controllers/root_controller.rb @@ -0,0 +1,18 @@ +# RootController +# +# This controller exists solely to handle requests to `root_url`. When a user is +# logged in and has customized their `dashboard` setting, they will be +# redirected to their preferred location. +# +# For users who haven't customized the setting, we simply delegate to +# `DashboardController#show`, which is the default. +class RootController < DashboardController + def show + case current_user.try(:dashboard) + when 'stars' + redirect_to starred_dashboard_projects_path + else + super + end + end +end diff --git a/app/views/layouts/nav/_dashboard.html.haml b/app/views/layouts/nav/_dashboard.html.haml index d46dba4a240..83e6fe863f9 100644 --- a/app/views/layouts/nav/_dashboard.html.haml +++ b/app/views/layouts/nav/_dashboard.html.haml @@ -1,6 +1,6 @@ %ul.nav.nav-sidebar = nav_link(path: 'dashboard#show', html_options: {class: 'home'}) do - = link_to root_path, title: 'Home', class: 'shortcuts-activity', data: {placement: 'right'} do + = link_to dashboard_path, title: 'Home', class: 'shortcuts-activity', data: {placement: 'right'} do = icon('dashboard fw') %span Your Projects diff --git a/config/routes.rb b/config/routes.rb index 52c98541daf..d60bc796fdb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -293,7 +293,7 @@ Gitlab::Application.routes.draw do get '/users/auth/:provider/omniauth_error' => 'omniauth_callbacks#omniauth_error', as: :omniauth_error end - root to: "dashboard#show" + root to: "root#show" # # Project Area diff --git a/spec/controllers/root_controller_spec.rb b/spec/controllers/root_controller_spec.rb new file mode 100644 index 00000000000..abbbf6855fc --- /dev/null +++ b/spec/controllers/root_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe RootController do + describe 'GET show' do + context 'with a user' do + let(:user) { create(:user) } + + before do + sign_in(user) + allow(subject).to receive(:current_user).and_return(user) + end + + context 'who has customized their dashboard setting' do + before do + user.update_attribute(:dashboard, 'stars') + end + + it 'redirects to their specified dashboard' do + get :show + expect(response).to redirect_to starred_dashboard_projects_path + end + end + + context 'who uses the default dashboard setting' do + it 'renders the default dashboard' do + get :show + expect(response).to render_template 'dashboard/show' + end + end + end + end +end diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb index 199851be482..f268e4755d1 100644 --- a/spec/routing/routing_spec.rb +++ b/spec/routing/routing_spec.rb @@ -204,11 +204,9 @@ end # dashboard GET /dashboard(.:format) dashboard#show # dashboard_issues GET /dashboard/issues(.:format) dashboard#issues # dashboard_merge_requests GET /dashboard/merge_requests(.:format) dashboard#merge_requests -# root / dashboard#show describe DashboardController, "routing" do it "to #index" do expect(get("/dashboard")).to route_to('dashboard#show') - expect(get("/")).to route_to('dashboard#show') end it "to #issues" do @@ -220,6 +218,14 @@ describe DashboardController, "routing" do end end +# root / root#show +describe RootController, 'routing' do + it 'to #show' do + expect(get('/')).to route_to('root#show') + end +end + + # new_user_session GET /users/sign_in(.:format) devise/sessions#new # user_session POST /users/sign_in(.:format) devise/sessions#create # destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy -- cgit v1.2.1