From 5d15bfaf5f0f07c83acfde3051acaa72ebbf7d8c Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Thu, 22 Oct 2015 12:28:26 +0100 Subject: [PATCH] Administrate dashboards live! --- Gemfile | 23 ++-- Gemfile.lock | 126 +++++++++++++----- .../admin/application_controller.rb | 19 +++ .../admin/categories_controller.rb | 19 +++ app/controllers/admin/pages_controller.rb | 19 +++ app/controllers/admin/photos_controller.rb | 19 +++ app/dashboards/category_dashboard.rb | 44 ++++++ app/dashboards/dashboard_manifest.rb | 25 ++++ app/dashboards/page_dashboard.rb | 40 ++++++ app/dashboards/photo_dashboard.rb | 48 +++++++ app/models/category.rb | 4 + app/models/page.rb | 3 + app/models/photo.rb | 2 +- config/routes.rb | 8 ++ 14 files changed, 349 insertions(+), 50 deletions(-) create mode 100644 app/controllers/admin/application_controller.rb create mode 100644 app/controllers/admin/categories_controller.rb create mode 100644 app/controllers/admin/pages_controller.rb create mode 100644 app/controllers/admin/photos_controller.rb create mode 100644 app/dashboards/category_dashboard.rb create mode 100644 app/dashboards/dashboard_manifest.rb create mode 100644 app/dashboards/page_dashboard.rb create mode 100644 app/dashboards/photo_dashboard.rb diff --git a/Gemfile b/Gemfile index fd019b9..ed4bfcc 100644 --- a/Gemfile +++ b/Gemfile @@ -3,13 +3,13 @@ ruby '2.2.3' gem 'dotenv-rails', :groups => [:development, :test] -gem 'rails', '~> 4.1.0' +gem 'rails', '~> 4.2.0' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' -gem 'sass-rails', " ~> 5.0.0" -gem 'coffee-rails', "~> 4.1.0" +gem 'sass-rails' +gem 'coffee-rails' gem 'uglifier' gem 'bourbon' @@ -52,17 +52,16 @@ group :production do gem 'rails_12factor' end -gem 'exception_notification', :git => 'git://github.com/rails/exception_notification', :require => 'exception_notifier' -gem 'pg' -gem 'simple_form' -gem 'squeel' -gem 'will_paginate' -gem 'redcarpet' +gem 'administrate' gem 'dragonfly' gem 'dragonfly-s3_data_store' -gem 'unf' +gem 'exception_notification', :git => 'git://github.com/rails/exception_notification', :require => 'exception_notifier' gem 'haml' - +gem 'kaminari' +gem 'pg' +gem 'redcarpet' +gem 'simple_form' gem 'skylight' - gem 'slugtastic' +gem 'squeel' +gem 'unf' diff --git a/Gemfile.lock b/Gemfile.lock index 6e196a4..5611fe8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,37 +7,60 @@ GIT GEM remote: http://rubygems.org/ specs: - actionmailer (4.1.13) - actionpack (= 4.1.13) - actionview (= 4.1.13) + actionmailer (4.2.4) + actionpack (= 4.2.4) + actionview (= 4.2.4) + activejob (= 4.2.4) mail (~> 2.5, >= 2.5.4) - actionpack (4.1.13) - actionview (= 4.1.13) - activesupport (= 4.1.13) - rack (~> 1.5.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.4) + actionview (= 4.2.4) + activesupport (= 4.2.4) + rack (~> 1.6) rack-test (~> 0.6.2) - actionview (4.1.13) - activesupport (= 4.1.13) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (4.2.4) + activesupport (= 4.2.4) builder (~> 3.1) erubis (~> 2.7.0) - activemodel (4.1.13) - activesupport (= 4.1.13) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + activejob (4.2.4) + activesupport (= 4.2.4) + globalid (>= 0.3.0) + activemodel (4.2.4) + activesupport (= 4.2.4) builder (~> 3.1) - activerecord (4.1.13) - activemodel (= 4.1.13) - activesupport (= 4.1.13) - arel (~> 5.0.0) - activesupport (4.1.13) - i18n (~> 0.6, >= 0.6.9) + activerecord (4.2.4) + activemodel (= 4.2.4) + activesupport (= 4.2.4) + arel (~> 6.0) + activesupport (4.2.4) + i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.1) + thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.3.8) - arel (5.0.1.20140414130214) + administrate (0.0.12) + autoprefixer-rails + datetime_picker_rails (~> 0.0.4) + inline_svg (~> 0.6) + kaminari (~> 0.16) + momentjs-rails (>= 2.9.0) + neat (~> 1.1) + normalize-rails (~> 3.0) + rails (~> 4.2) + sass (~> 3.4) + selectize-rails (~> 0.6) + arel (6.0.3) ast (2.1.0) astrolabe (1.3.1) parser (~> 2.2) + autoprefixer-rails (6.0.3) + execjs + json aws-sdk (2.1.29) aws-sdk-resources (= 2.1.29) aws-sdk-core (2.1.29) @@ -73,6 +96,8 @@ GEM execjs coffee-script-source (1.9.1.1) database_cleaner (1.5.0) + datetime_picker_rails (0.0.4) + momentjs-rails (>= 2.8.1) debug_inspector (0.0.2) diff-lcs (1.2.5) docile (1.1.5) @@ -115,31 +140,48 @@ GEM fog-core nokogiri (~> 1.5, >= 1.5.11) formatador (0.2.5) + globalid (0.3.6) + activesupport (>= 4.1.0) haml (4.0.7) tilt i18n (0.7.0) + inline_svg (0.6.1) + activesupport (>= 4.0.4) + loofah (>= 2.0) + nokogiri (~> 1.6) ipaddress (0.8.0) jmespath (1.1.3) jquery-rails (3.1.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.8.3) + kaminari (0.16.3) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) launchy (2.4.3) addressable (~> 2.3) letter_opener (1.4.1) launchy (~> 2.2) + loofah (2.0.3) + nokogiri (>= 1.5.9) mail (2.6.3) mime-types (>= 1.16, < 3) method_source (0.8.2) mime-types (2.6.2) mini_portile (0.6.2) minitest (5.8.1) + momentjs-rails (2.10.6) + railties (>= 3.1) multi_json (1.11.2) + neat (1.7.2) + bourbon (>= 4.0) + sass (>= 3.3) net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (3.0.1) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + normalize-rails (3.0.3) parser (2.2.3.0) ast (>= 1.1, < 3.0) pg (0.18.3) @@ -158,27 +200,36 @@ GEM method_source (~> 0.8.1) slop (~> 3.4) puma (2.14.0) - rack (1.5.5) + rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) - rails (4.1.13) - actionmailer (= 4.1.13) - actionpack (= 4.1.13) - actionview (= 4.1.13) - activemodel (= 4.1.13) - activerecord (= 4.1.13) - activesupport (= 4.1.13) + rails (4.2.4) + actionmailer (= 4.2.4) + actionpack (= 4.2.4) + actionview (= 4.2.4) + activejob (= 4.2.4) + activemodel (= 4.2.4) + activerecord (= 4.2.4) + activesupport (= 4.2.4) bundler (>= 1.3.0, < 2.0) - railties (= 4.1.13) - sprockets-rails (~> 2.0) + railties (= 4.2.4) + sprockets-rails + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.7) + activesupport (>= 4.2.0.beta, < 5.0) + nokogiri (~> 1.6.0) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.0.2) + loofah (~> 2.0) rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging rails_serve_static_assets (0.0.4) rails_stdout_logging (0.0.4) - railties (4.1.13) - actionpack (= 4.1.13) - activesupport (= 4.1.13) + railties (4.2.4) + actionpack (= 4.2.4) + activesupport (= 4.2.4) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rainbow (2.0.0) @@ -217,6 +268,7 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + selectize-rails (0.12.1) sexp_processor (4.6.0) shoulda (3.5.0) shoulda-context (~> 1.0, >= 1.0.1) @@ -260,7 +312,6 @@ GEM websocket-driver (0.6.2) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) - will_paginate (3.0.7) xpath (2.0.0) nokogiri (~> 1.3) @@ -268,12 +319,13 @@ PLATFORMS ruby DEPENDENCIES + administrate aws-sdk better_errors binding_of_caller bourbon capybara-screenshot - coffee-rails (~> 4.1.0) + coffee-rails database_cleaner dotenv-rails dragonfly @@ -283,19 +335,20 @@ DEPENDENCIES fivemat haml jquery-rails + kaminari letter_opener pg poltergeist powder pry puma - rails (~> 4.1.0) + rails (~> 4.2.0) rails_12factor redcarpet rspec-rails rubocop ruby_parser - sass-rails (~> 5.0.0) + sass-rails shoulda shoulda-context shoulda-matchers @@ -306,7 +359,6 @@ DEPENDENCIES squeel uglifier unf - will_paginate BUNDLED WITH 1.10.6 diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb new file mode 100644 index 0000000..59c4ec9 --- /dev/null +++ b/app/controllers/admin/application_controller.rb @@ -0,0 +1,19 @@ +# All Administrate controllers inherit from this `Admin::ApplicationController`, +# making it the ideal place to put authentication logic or other +# before_filters. +# +# If you want to add pagination or other controller-level concerns, +# you're free to overwrite the RESTful controller actions. +class Admin::ApplicationController < Administrate::ApplicationController + before_filter :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 diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb new file mode 100644 index 0000000..2ed78b7 --- /dev/null +++ b/app/controllers/admin/categories_controller.rb @@ -0,0 +1,19 @@ +module Admin + class CategoriesController < Admin::ApplicationController + # To customize the behavior of this controller, + # simply overwrite any of the RESTful actions. For example: + # + # def index + # super + # @resources = Category.all.paginate(10, params[:page]) + # end + + # Define a custom finder by overriding the `find_resource` method: + # def find_resource(param) + # Category.find_by!(slug: param) + # end + + # See https://administrate-docs.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb new file mode 100644 index 0000000..126f048 --- /dev/null +++ b/app/controllers/admin/pages_controller.rb @@ -0,0 +1,19 @@ +module Admin + class PagesController < Admin::ApplicationController + # To customize the behavior of this controller, + # simply overwrite any of the RESTful actions. For example: + # + # def index + # super + # @resources = Page.all.paginate(10, params[:page]) + # end + + # Define a custom finder by overriding the `find_resource` method: + # def find_resource(param) + # Page.find_by!(slug: param) + # end + + # See https://administrate-docs.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/photos_controller.rb b/app/controllers/admin/photos_controller.rb new file mode 100644 index 0000000..1b181ad --- /dev/null +++ b/app/controllers/admin/photos_controller.rb @@ -0,0 +1,19 @@ +module Admin + class PhotosController < Admin::ApplicationController + # To customize the behavior of this controller, + # simply overwrite any of the RESTful actions. For example: + # + # def index + # super + # @resources = Photo.all.paginate(10, params[:page]) + # end + + # Define a custom finder by overriding the `find_resource` method: + # def find_resource(param) + # Photo.find_by!(slug: param) + # end + + # See https://administrate-docs.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/dashboards/category_dashboard.rb b/app/dashboards/category_dashboard.rb new file mode 100644 index 0000000..2afd8fb --- /dev/null +++ b/app/dashboards/category_dashboard.rb @@ -0,0 +1,44 @@ +require "administrate/base_dashboard" + +class CategoryDashboard < Administrate::BaseDashboard + READ_ONLY_ATTRIBUTES = [ + :id, + :created_at, + :updated_at, + ] + + # 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 = { + photos: Field::HasMany, + id: Field::Number, + name: Field::String, + description: Field::String, + created_at: Field::DateTime, + updated_at: Field::DateTime, + photo_id: Field::Number, + base_colour: Field::String, + sort: Field::Number, + slug: Field::String, + } + + # TABLE_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 remove the limit or customize the returned array. + TABLE_ATTRIBUTES = ATTRIBUTE_TYPES.keys.first(4) + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = ATTRIBUTE_TYPES.keys - READ_ONLY_ATTRIBUTES +end diff --git a/app/dashboards/dashboard_manifest.rb b/app/dashboards/dashboard_manifest.rb new file mode 100644 index 0000000..9811545 --- /dev/null +++ b/app/dashboards/dashboard_manifest.rb @@ -0,0 +1,25 @@ +# DashboardManifest tells Administrate which dashboards to display +class DashboardManifest + # `DASHBOARDS` + # a list of dashboards to display in the side navigation menu + # + # These are all of the rails models that we found in your database + # at the time you installed Administrate. + # + # To show or hide dashboards, add or remove the model name from this list. + # Dashboards returned from this method must be Rails models for Administrate + # to work correctly. + DASHBOARDS = [ + :categories, + :pages, + :photos, + ] + + # `ROOT_DASHBOARD` + # the name of the dashboard that will be displayed + # at "http://your_site.com/admin" + # + # This dashboard will likely be the first page that admins see + # when they log into the dashboard. + ROOT_DASHBOARD = DASHBOARDS.first +end diff --git a/app/dashboards/page_dashboard.rb b/app/dashboards/page_dashboard.rb new file mode 100644 index 0000000..4064a54 --- /dev/null +++ b/app/dashboards/page_dashboard.rb @@ -0,0 +1,40 @@ +require "administrate/base_dashboard" + +class PageDashboard < Administrate::BaseDashboard + READ_ONLY_ATTRIBUTES = [ + :id, + :created_at, + :updated_at, + ] + + # 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 = { + id: Field::Number, + name: Field::String, + title: Field::String, + content: Field::String, + created_at: Field::DateTime, + updated_at: Field::DateTime, + } + + # TABLE_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 remove the limit or customize the returned array. + TABLE_ATTRIBUTES = ATTRIBUTE_TYPES.keys.first(4) + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = ATTRIBUTE_TYPES.keys - READ_ONLY_ATTRIBUTES +end diff --git a/app/dashboards/photo_dashboard.rb b/app/dashboards/photo_dashboard.rb new file mode 100644 index 0000000..6f9eecb --- /dev/null +++ b/app/dashboards/photo_dashboard.rb @@ -0,0 +1,48 @@ +require "administrate/base_dashboard" + +class PhotoDashboard < Administrate::BaseDashboard + READ_ONLY_ATTRIBUTES = [ + :id, + :created_at, + :updated_at, + ] + + # 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 = { + categories: Field::HasMany, + id: Field::Number, + flickr_url: Field::String, + created_at: Field::DateTime, + updated_at: Field::DateTime, + title: Field::String, + description: Field::String, + sort: Field::Number, + featured: Field::Boolean, + enabled: Field::Boolean, + taken_at: Field::DateTime, + views: Field::Number, + image_uid: Field::String, + image_name: Field::String, + } + + # TABLE_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 remove the limit or customize the returned array. + TABLE_ATTRIBUTES = ATTRIBUTE_TYPES.keys.first(4) + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = ATTRIBUTE_TYPES.keys - READ_ONLY_ATTRIBUTES +end diff --git a/app/models/category.rb b/app/models/category.rb index 0973e0b..fd8f864 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -4,4 +4,8 @@ class Category < ActiveRecord::Base validates_uniqueness_of :name, :slug has_slug :slug, from: :name + + def to_s + name + end end diff --git a/app/models/page.rb b/app/models/page.rb index 37ce9e7..77d0d59 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,2 +1,5 @@ class Page < ActiveRecord::Base + def to_s + title + end end diff --git a/app/models/photo.rb b/app/models/photo.rb index ce0b17e..e92c7ce 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -5,7 +5,7 @@ class Photo < ActiveRecord::Base validates :image, presence: true - self.per_page = 11 + paginates_per 11 scope :enabled, -> { where(enabled: true) } diff --git a/config/routes.rb b/config/routes.rb index 9f73ec9..1993957 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,12 @@ DanBarberPhoto::Application.routes.draw do + namespace :admin do + DashboardManifest::DASHBOARDS.each do |dashboard_resource| + resources dashboard_resource + end + + root controller: DashboardManifest::ROOT_DASHBOARD, action: :index + end + resources :contacts resources :categories do