Skip to content

Commit

Permalink
Allow custom routing for admin backend
Browse files Browse the repository at this point in the history
  • Loading branch information
milj committed Oct 16, 2015
1 parent a6fb572 commit 871e77e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 48 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ Alchemy has very flexible ways to organize and manage content. Please be sure to

Beginning with Alchemy 3.1 we do not patch the `ApplicationController` anymore. If you have controllers that loads Alchemy content or uses Alchemy helpers in the views (i.e. `render_navigation` or `render_elements`) you can either inherit from `Alchemy::BaseController` or you `include Alchemy::ControllerActions` in your controller (**that's the recommended way**).

### Custom admin interface routing

By default, Alchemy Dashboard is accessible at <http://example.com/admin>. You can change this by setting `Alchemy.admin_path` and `Alchemy.admin_constraints`.
For example, these settings:

```ruby
# config/initializers/alchemy.rb

Alchemy.admin_path = '/backend'
Alchemy.admin_constraints = {subdomain: 'hidden'}
```

will move the dashboard to <http://hidden.example.com/backend>.

## Upgrading

Expand Down
4 changes: 2 additions & 2 deletions app/views/alchemy/_menubar.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<% if !@preview_mode && can?(:edit_content, @page) %>
<div id="alchemy_menubar" style="display: none">
<ul>
<li><%= link_to _t(:to_alchemy), alchemy.admin_dashboard_path %></li>
<li><%= link_to _t(:edit_page), alchemy.edit_admin_page_path(@page) %></li>
<li><%= link_to _t(:to_alchemy), alchemy.admin_dashboard_url %></li>
<li><%= link_to _t(:edit_page), alchemy.edit_admin_page_url(@page) %></li>
<li>
<%= form_tag Alchemy.logout_path, method: 'delete' do %>
<%= button_tag _t(:logout) %>
Expand Down
86 changes: 42 additions & 44 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,16 @@

get '/sitemap.xml' => 'pages#sitemap', format: 'xml'

get '/admin' => redirect('admin/dashboard')
get '/admin/dashboard' => 'admin/dashboard#index',
:as => :admin_dashboard
get '/admin/dashboard/info' => 'admin/dashboard#info',
:as => :dashboard_info
get '/admin/help' => 'admin/dashboard#help',
:as => :help
get '/admin/dashboard/update_check' => 'admin/dashboard#update_check',
:as => :update_check

get '/attachment/:id/download(/:name)' => 'attachments#download',
:as => :download_attachment
get '/attachment/:id/show' => 'attachments#show',
:as => :show_attachment

# Picture urls
get "/pictures/:id/show(/:size)(/:crop)(/:crop_from/:crop_size)(/:quality)/:name.:format" => 'pictures#show',
:as => :show_picture
get '/pictures/:id/zoom/:name.:format' => 'pictures#zoom',
:as => :zoom_picture
get "/pictures/:id/thumbnails(/:size)(/:crop)(/:crop_from/:crop_size)/:name.:format" => 'pictures#thumbnail',
:as => :thumbnail, :defaults => {:format => 'png', :name => "thumbnail"}

get '/admin/leave' => 'admin/base#leave', :as => :leave_admin

resources :messages, :only => [:index, :new, :create]
resources :elements, :only => :show

namespace :api, defaults: {format: 'json'} do
resources :contents, only: [:index, :show]

resources :elements, only: [:index, :show] do
get '/contents' => 'contents#index', as: 'contents'
get '/contents/:name' => 'contents#show', as: 'content'
end

resources :pages, only: [:index] do
get 'elements' => 'elements#index', as: 'elements'
get 'elements/:named' => 'elements#index', as: 'named_elements'
end

get '/pages/*urlname(.:format)' => 'pages#show', as: 'page'
get '/admin/pages/:id(.:format)' => 'pages#show', as: 'preview_page'
scope Alchemy.admin_path, {constraints: Alchemy.admin_constraints} do
get '/' => redirect("#{Alchemy.admin_path}/dashboard"), as: :admin
get '/dashboard' => 'admin/dashboard#index', as: :admin_dashboard
get '/dashboard/info' => 'admin/dashboard#info', as: :dashboard_info
get '/help' => 'admin/dashboard#help', as: :help
get '/dashboard/update_check' => 'admin/dashboard#update_check', as: :update_check
get '/leave' => 'admin/base#leave', as: :leave_admin
end

namespace :admin do
namespace :admin, {path: Alchemy.admin_path, constraints: Alchemy.admin_constraints} do
resources :contents do
collection do
post :order
Expand Down Expand Up @@ -154,6 +118,40 @@
resources :sites
end

get '/attachment/:id/download(/:name)' => 'attachments#download',
as: :download_attachment
get '/attachment/:id/show' => 'attachments#show',
as: :show_attachment

# Picture urls
get "/pictures/:id/show(/:size)(/:crop)(/:crop_from/:crop_size)(/:quality)/:name.:format" => 'pictures#show',
as: :show_picture
get '/pictures/:id/zoom/:name.:format' => 'pictures#zoom',
as: :zoom_picture
get "/pictures/:id/thumbnails(/:size)(/:crop)(/:crop_from/:crop_size)/:name.:format" => 'pictures#thumbnail',
as: :thumbnail, :defaults => {:format => 'png', :name => "thumbnail"}

resources :messages, :only => [:index, :new, :create]
resources :elements, :only => :show
resources :contents, :only => :show

This comment has been minimized.

Copy link
@phantomwhale

phantomwhale Feb 13, 2022

Contributor

What is this route? There doesn't seem to be a matching Alchemy::ContentsController?


namespace :api, defaults: {format: 'json'} do
resources :contents, only: [:index, :show]

resources :elements, only: [:index, :show] do
get '/contents' => 'contents#index', as: 'contents'
get '/contents/:name' => 'contents#show', as: 'content'
end

resources :pages, only: [:index] do
get 'elements' => 'elements#index', as: 'elements'
get 'elements/:named' => 'elements#index', as: 'named_elements'
end

get '/pages/*urlname(.:format)' => 'pages#show', as: 'page'
get '/admin/pages/:id(.:format)' => 'pages#show', as: 'preview_page'
end

get '/:locale' => 'pages#show',
constraints: {locale: Alchemy::RoutingConstraints::LOCALE_REGEXP},
as: :show_language_root
Expand Down
1 change: 1 addition & 0 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
require_relative './on_page_layout'
require_relative './on_page_layout/callbacks_runner'
require_relative './page_layout'
require_relative './paths'
require_relative './permissions'
require_relative './picture_attributes'
require_relative './ssl_protection'
Expand Down
32 changes: 32 additions & 0 deletions lib/alchemy/paths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Provides admin interface routing configuration accessors.
#
# Alchemy has some defaults for admin path and admin constraints:
#
# +Alchemy.admin_path defaults to +'/admin'+
# +Alchemy.admin_constraints defaults to +{}+
#
# Anyway, you can tell Alchemy about your routing configuration:
#
# 1. The path to the admin panel - @see: Alchemy.admin_path
# 2. The constraints for the admin panel (like subdomain) - @see: Alchemy.admin_constraints
#
# A word of caution: you need to know what you are doing if you set admin_path to ''. This can cause
# routing name clashes, e.g. a page named 'dashboard' will clash with the Alchemy dashboard.
#
# == Example
#
# If you do not wish to use the default admin interface routing ('example.com/admin/')
# and prefer e.g. 'hidden.example.com/backend/', those are the settings you need:
#
# # config/initializers/alchemy.rb
# Alchemy.admin_path = '/backend'
# Alchemy.admin_constraints = {subdomain: 'hidden'}
#
module Alchemy
mattr_accessor :admin_path, :admin_constraints

# Defaults
#
@@admin_path = '/admin'
@@admin_constraints = {}
end
4 changes: 2 additions & 2 deletions spec/features/page_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ module Alchemy

it "a link to the admin area" do
within('#alchemy_menubar') do
expect(page).to have_selector("li a[href='#{alchemy.admin_dashboard_path}']")
expect(page).to have_selector("li a[href='#{alchemy.admin_dashboard_url}']")
end
end

it "a link to edit the current page" do
within('#alchemy_menubar') do
expect(page).to have_selector("li a[href='#{alchemy.edit_admin_page_path(public_page_1)}']")
expect(page).to have_selector("li a[href='#{alchemy.edit_admin_page_url(public_page_1)}']")
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/libraries/paths_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

module Alchemy
describe 'Paths' do
describe 'defaults' do
it 'has default value for Alchemy.admin_path' do
expect(Alchemy.admin_path).to eq('/admin')
end

it 'has default value for Alchemy.admin_constraints' do
expect(Alchemy.admin_constraints).to eq({})
end
end
end
end

0 comments on commit 871e77e

Please sign in to comment.