Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace DashboardManifest with explicit routes #464

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

### Upcoming Release

* [#464] [CHANGE] Replace the DashboardManifest with explicit Rails routes.
* Run `rails generate administrate:routes` to generate the default routes.

### 0.1.3 (January 22, 2016)

* [#269] [FEATURE] Add a generator for copying default layout files
Expand Down
4 changes: 2 additions & 2 deletions app/views/administrate/application/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
This partial is used to display the sidebar in Administrate.
By default, the sidebar contains navigation links
for all resources in the admin dashboard,
as defined by the DashboardManifest.
as defined by the routes in the `admin/` namespace
%>

<ul class="sidebar__list">
<% DashboardManifest::DASHBOARDS.each do |resource| %>
<% Administrate::Namespace.new(namespace).resources.each do |resource| %>
<li>
<%= link_to(
display_resource_name(resource),
Expand Down
23 changes: 5 additions & 18 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ Re-bundle, then run the installer:
$ rails generate administrate:install
```

The installer creates a few files.
Two of them are standard for any installation:

- `app/controllers/admin/application_controller.rb`
- `app/dashboards/dashboard_manifest.rb`
The installer adds some new routes to your `config/application.rb`,
and creates a controller at `app/controllers/admin/application_controller.rb`

In addition, the generator creates a `Dashboard` and a `Controller` for each of
your ActiveRecord resources:
Expand All @@ -32,23 +29,13 @@ The `Admin::ApplicationController` can be customized to add
authentication logic, authorization, pagination,
or other controller-level concerns.

The `DashboardManifest` can be customized to show or hide
The routes can be customized to show or hide
different models on the dashboard.

Each `FooDashboard` specifies which attributes should be displayed
on the admin dashboard for the `Foo` resource.

Each `Admin::FooController` can be overwritten to specify custom behavior.

The installer will also add a line to your `config/routes.rb` file:

```ruby
DashboardManifest.new.dashboards.each do |dashboard_resource|
resources dashboard_resource
end

root controller: DashboardManifest.new.root_dashboard, action: :index
```

Feel free to customize these routes to your heart's content,
then visit <http://localhost:3000/admin> to see your new dashboard in action.
Once you have Administrate installed,
visit <http://localhost:3000/admin> to see your new dashboard in action.
1 change: 1 addition & 0 deletions lib/administrate/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require "administrate/order"
require "administrate/resource_resolver"
require "administrate/search"
require "administrate/namespace"

module Administrate
class Engine < ::Rails::Engine
Expand Down
29 changes: 29 additions & 0 deletions lib/administrate/namespace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Administrate
class Namespace
def initialize(namespace)
@namespace = namespace
end

def resources
namespace_controller_paths.uniq.map do |controller|
controller.gsub(/^#{namespace}\//, "").to_sym
end
end

private

attr_reader :namespace

def namespace_controller_paths
all_controller_paths.select do |controller|
controller.starts_with?(namespace.to_s)
end
end

def all_controller_paths
Rails.application.routes.routes.map do |route|
route.defaults[:controller].to_s
end
end
end
end
36 changes: 9 additions & 27 deletions lib/generators/administrate/install/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
require "rails/generators/base"
require "administrate/generator_helpers"
require "administrate/namespace"

module Administrate
module Generators
class InstallGenerator < Rails::Generators::Base
include Administrate::GeneratorHelpers
source_root File.expand_path("../templates", __FILE__)

def run_routes_generator
if dashboard_resources.none?
call_generator("administrate:routes")
load Rails.root.join("config/routes.rb")
end
end

def create_dashboard_controller
copy_file(
"application_controller.rb",
"app/controllers/admin/application_controller.rb"
)
end

def insert_dashboard_routes
unless File.read(rails_routes_file_path).include?(dashboard_routes)
route(dashboard_routes)
end
end

def run_dashboard_generators
singular_dashboard_resources.each do |resource|
call_generator("administrate:dashboard", resource)
Expand All @@ -33,27 +35,7 @@ def singular_dashboard_resources
end

def dashboard_resources
manifest::DASHBOARDS
end

def manifest
unless defined?(DashboardManifest)
call_generator("administrate:manifest")
end

DashboardManifest
end

def dashboard_routes
File.read(routes_file_path)
end

def rails_routes_file_path
Rails.root.join("config/routes.rb")
end

def routes_file_path
File.expand_path(find_in_source_paths("routes.rb"))
Administrate::Namespace.new(:admin).resources
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/generators/administrate/install/templates/routes.rb

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Rails.application.eager_load!
require "rails/generators/base"
require "administrate/namespace"

module Administrate
module Generators
class ManifestGenerator < Rails::Generators::Base
class RoutesGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)

def create_dashboard_manifest
template(
"dashboard_manifest.rb.erb",
Rails.root.join("app/dashboards/dashboard_manifest.rb"),
)
def insert_dashboard_routes
unless File.read(rails_routes_file_path).include?(dashboard_routes)
route(dashboard_routes)
end
end

def warn_about_invalid_models
Expand All @@ -22,6 +22,7 @@ def warn_about_invalid_models
models_without_tables.each do |invalid_model|
puts "WARNING: Unable to generate a dashboard for #{invalid_model}."
puts " It is not connected to a database table."
puts " Make sure your database migrations are up to date."
end

unnamed_constants.each do |invalid_model|
Expand Down Expand Up @@ -60,6 +61,18 @@ def namespaced_models
def unnamed_constants
ActiveRecord::Base.descendants.reject { |d| d.name == d.to_s }
end

def dashboard_routes
ERB.new(File.read(routes_file_path)).result(binding)
end

def rails_routes_file_path
Rails.root.join("config/routes.rb")
end

def routes_file_path
File.expand_path(find_in_source_paths("routes.rb.erb"))
end
end
end
end
5 changes: 5 additions & 0 deletions lib/generators/administrate/routes/templates/routes.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace :admin do
<% dashboard_resources.each do |resource| %>resources :<%= resource %>
<% end %>
root to: "<%= dashboard_resources.first %>#index"
end
19 changes: 19 additions & 0 deletions spec/administrate/namespace_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "rails_helper"
require "administrate/namespace"

describe Administrate::Namespace do
describe "#resources" do
it "searches the routes for resources in the namespace" do
begin
namespace = Administrate::Namespace.new(:admin)
Rails.application.routes.draw do
namespace(:admin) { resources :customers }
end

expect(namespace.resources).to eq [:customers]
ensure
reset_routes
end
end
end
end
26 changes: 0 additions & 26 deletions spec/example_app/app/dashboards/dashboard_manifest.rb

This file was deleted.

9 changes: 5 additions & 4 deletions spec/example_app/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Rails.application.routes.draw do
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
end
resources :customers
resources :line_items
resources :orders
resources :products

root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
root to: "customers#index"
end

get "/:page", to: "docs#show"
Expand Down
Loading