Skip to content

Commit

Permalink
Add administrate
Browse files Browse the repository at this point in the history
```
$ rails generate administrate:install

Running via Spring preloader in process 30795
       route  namespace :admin do
    resources :blogs

    root to: "blogs#index"
  end
WARNING: Unable to generate a dashboard for PaperTrail::Version.
         Administrate does not yet support namespaced models.
      create  app/controllers/admin/application_controller.rb
      create  app/dashboards/blog_dashboard.rb
      create  app/controllers/admin/blogs_controller.rb
```
  • Loading branch information
coffeejunk committed Apr 2, 2019
1 parent 56df062 commit b546a2b
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "paper_trail", "~> 10.2"

gem "administrate", "~> 0.11.0"
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ GEM
tzinfo (~> 1.1)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
administrate (0.11.0)
actionpack (>= 4.2, < 6.0)
actionview (>= 4.2, < 6.0)
activerecord (>= 4.2, < 6.0)
autoprefixer-rails (>= 6.0)
datetime_picker_rails (~> 0.0.7)
jquery-rails (>= 4.0)
kaminari (>= 1.0)
momentjs-rails (~> 2.8)
sass-rails (~> 5.0)
selectize-rails (~> 0.6)
archive-zip (0.12.0)
io-like (~> 0.3.0)
arel (9.0.0)
autoprefixer-rails (9.5.0)
execjs
bindex (0.6.0)
bootsnap (1.4.2)
msgpack (~> 1.0)
Expand Down Expand Up @@ -74,6 +87,8 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
datetime_picker_rails (0.0.7)
momentjs-rails (>= 2.8.1)
erubi (1.8.0)
execjs (2.7.0)
ffi (1.10.0)
Expand All @@ -85,6 +100,22 @@ GEM
jbuilder (2.8.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -101,6 +132,8 @@ GEM
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
momentjs-rails (2.20.1)
railties (>= 3.1)
msgpack (1.2.9)
multi_json (1.13.1)
nio4r (2.3.1)
Expand Down Expand Up @@ -158,6 +191,7 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selectize-rails (0.12.6)
selenium-webdriver (3.141.0)
childprocess (~> 0.5)
rubyzip (~> 1.2, >= 1.2.2)
Expand Down Expand Up @@ -199,6 +233,7 @@ PLATFORMS
ruby

DEPENDENCIES
administrate (~> 0.11.0)
bootsnap (>= 1.1.0)
byebug
capybara (>= 2.15)
Expand Down
21 changes: 21 additions & 0 deletions app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# All Administrate controllers inherit from this `Admin::ApplicationController`,
# making it the ideal place to put authentication logic or other
# before_actions.
#
# If you want to add pagination or other controller-level concerns,
# you're free to overwrite the RESTful controller actions.
module Admin
class ApplicationController < Administrate::ApplicationController
before_action :authenticate_admin

def authenticate_admin
# TODO Add authentication logic here.
end

# Override this value to specify the number of elements to display at a time
# on index pages. Defaults to 20.
# def records_per_page
# params[:per_page] || 20
# end
end
end
21 changes: 21 additions & 0 deletions app/controllers/admin/blogs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Admin
class BlogsController < Admin::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Blog.
# page(params[:page]).
# per(10)
# end

# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Blog.find_by!(slug: param)
# end

# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
end
end
57 changes: 57 additions & 0 deletions app/dashboards/blog_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "administrate/base_dashboard"

class BlogDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
versions: Field::HasMany.with_options(class_name: "PaperTrail::Version"),
id: Field::Number,
title: Field::String,
body: Field::Text,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [
:versions,
:id,
:title,
:body,
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = [
:versions,
:id,
:title,
:body,
:created_at,
:updated_at,
].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [
:versions,
:title,
:body,
].freeze

# Overwrite this method to customize how blogs are displayed
# across all pages of the admin dashboard.
#
# def display_resource(blog)
# "Blog ##{blog.id}"
# end
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Rails.application.routes.draw do
namespace :admin do
resources :blogs

root to: "blogs#index"
end
resources :blogs
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

0 comments on commit b546a2b

Please sign in to comment.