Add posts and comments to admin panel

This commit is contained in:
riggraz
2019-09-24 21:16:51 +02:00
parent 7d50454d14
commit e06d95a7bb
17 changed files with 329 additions and 40 deletions

View File

@@ -0,0 +1,15 @@
module Admin
class CommentsController < Admin::ApplicationController
before_action :default_order
def default_order
@order ||= Administrate::Order.new(
params.fetch(resource_name, {}).fetch(:order, 'updated_at'),
params.fetch(resource_name, {}).fetch(:direction, 'desc'),
)
end
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
end
end

View File

@@ -0,0 +1,15 @@
module Admin
class PostsController < Admin::ApplicationController
before_action :default_order
def default_order
@order ||= Administrate::Order.new(
params.fetch(resource_name, {}).fetch(:order, 'updated_at'),
params.fetch(resource_name, {}).fetch(:direction, 'desc'),
)
end
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
end
end

View File

@@ -1,35 +1,13 @@
module Admin
class UsersController < Admin::ApplicationController
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# foo = Foo.find(params[:id])
# foo.update(params[:foo])
# send_foo_updated_email
# end
before_action :default_order
# Override this method to specify custom lookup behavior.
# This will be used to set the resource for the `show`, `edit`, and `update`
# actions.
#
# def find_resource(param)
# Foo.find_by!(slug: param)
# end
# Override this if you have certain roles that require a subset
# this will be used to set the records shown on the `index` action.
#
# def scoped_resource
# if current_user.super_admin?
# resource_class
# else
# resource_class.with_less_stuff
# end
# end
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
def default_order
@order ||= Administrate::Order.new(
params.fetch(resource_name, {}).fetch(:order, 'updated_at'),
params.fetch(resource_name, {}).fetch(:direction, 'desc'),
)
end
def authenticate_admin
unless user_signed_in?

View File

@@ -12,6 +12,7 @@ class BoardDashboard < Administrate::BaseDashboard
name: Field::String,
description: Field::Text,
order: Field::Number,
posts: Field::HasMany,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
@@ -25,6 +26,7 @@ class BoardDashboard < Administrate::BaseDashboard
name
description
order
posts
].freeze
# SHOW_PAGE_ATTRIBUTES
@@ -34,6 +36,7 @@ class BoardDashboard < Administrate::BaseDashboard
name
description
order
posts
created_at
updated_at
].freeze
@@ -62,7 +65,7 @@ class BoardDashboard < Administrate::BaseDashboard
# Overwrite this method to customize how boards are displayed
# across all pages of the admin dashboard.
#
# def display_resource(board)
# "Board ##{board.id}"
# end
def display_resource(board)
"Board \"#{board.name}\""
end
end

View File

@@ -0,0 +1,76 @@
require "administrate/base_dashboard"
class CommentDashboard < 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 = {
id: Field::Number,
body: Field::Text,
parent_id: Field::Number,
parent: Field::BelongsTo.with_options(class_name: "Comment"),
children: Field::HasMany.with_options(class_name: "Comment"),
user: Field::BelongsTo,
post: Field::BelongsTo,
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 = %i[
body
user
post
children
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
body
parent_id
parent
children
user
post
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 = %i[
body
parent
user
post
].freeze
# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze
# Overwrite this method to customize how comments are displayed
# across all pages of the admin dashboard.
#
# def display_resource(comment)
# "Comment ##{comment.id}"
# end
end

View File

@@ -0,0 +1,79 @@
require "administrate/base_dashboard"
class PostDashboard < 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 = {
id: Field::Number,
title: Field::String,
description: Field::Text,
comments: Field::HasMany,
user: Field::BelongsTo,
board: Field::BelongsTo,
post_status: Field::BelongsTo,
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 = %i[
title
description
user
board
post_status
comments
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
title
description
user
board
post_status
comments
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 = %i[
title
description
user
board
post_status
].freeze
# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze
# Overwrite this method to customize how posts are displayed
# across all pages of the admin dashboard.
#
# def display_resource(post)
# "Post ##{post.id}"
# end
end

View File

@@ -13,6 +13,7 @@ class PostStatusDashboard < Administrate::BaseDashboard
color: ColorField,
order: Field::Number,
show_in_roadmap: Field::Boolean,
posts: Field::HasMany,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
@@ -27,6 +28,7 @@ class PostStatusDashboard < Administrate::BaseDashboard
color
order
show_in_roadmap
posts
].freeze
# SHOW_PAGE_ATTRIBUTES
@@ -37,6 +39,7 @@ class PostStatusDashboard < Administrate::BaseDashboard
color
order
show_in_roadmap
posts
created_at
updated_at
].freeze
@@ -66,7 +69,7 @@ class PostStatusDashboard < Administrate::BaseDashboard
# Overwrite this method to customize how post statuses are displayed
# across all pages of the admin dashboard.
#
# def display_resource(post_status)
# "PostStatus ##{post_status.id}"
# end
def display_resource(post_status)
"PostStatus \"#{post_status.name}\""
end
end

View File

@@ -23,6 +23,8 @@ class UserDashboard < Administrate::BaseDashboard
updated_at: Field::DateTime,
role: RoleField,
full_name: Field::String,
posts: Field::HasMany,
comments: Field::HasMany,
}.freeze
# COLLECTION_ATTRIBUTES
@@ -34,6 +36,8 @@ class UserDashboard < Administrate::BaseDashboard
full_name
email
role
posts
comments
].freeze
# SHOW_PAGE_ATTRIBUTES
@@ -44,6 +48,8 @@ class UserDashboard < Administrate::BaseDashboard
email
role
password
posts
comments
created_at
updated_at
confirmed_at
@@ -76,7 +82,7 @@ class UserDashboard < Administrate::BaseDashboard
# Overwrite this method to customize how users are displayed
# across all pages of the admin dashboard.
#
# def display_resource(user)
# "User ##{user.id}"
# end
def display_resource(user)
"#{user.role.capitalize} \"#{user.full_name}\""
end
end

View File

@@ -2,7 +2,7 @@ class Post < ApplicationRecord
belongs_to :board
belongs_to :user
belongs_to :post_status, optional: true
has_many :comments
has_many :comments, dependent: :destroy
validates :title, presence: true, length: { in: 4..64 }

View File

@@ -1,4 +1,6 @@
class PostStatus < ApplicationRecord
has_many :posts, dependent: :nullify
after_initialize :set_random_color, :set_order_to_last
validates :name, presence: true, uniqueness: true

View File

@@ -3,7 +3,8 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable,
:confirmable
has_many :comments
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
enum role: [:user, :moderator, :admin]
after_initialize :set_default_role, if: :new_record?

View File

@@ -0,0 +1,20 @@
<%#
# Flash Partial
This partial renders flash messages on every page.
## Relevant Helpers:
- `flash`:
Returns a hash,
where the keys are the type of flash (alert, error, notice, etc)
and the values are the message to be displayed.
%>
<% if flash.any? %>
<div class="flashes">
<% flash.each do |key, value| -%>
<div class="flash flash-<%= key %>"><%= value.html_safe %></div>
<% end -%>
</div>
<% end %>

View File

@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="icon-cancel" viewBox="0 0 48 48">
<path fill-rule="evenodd" d="M24 19.757l-8.485-8.485c-.784-.783-2.047-.782-2.827 0l-1.417 1.416c-.777.777-.78 2.046.002 2.827L19.757 24l-8.485 8.485c-.783.784-.782 2.047 0 2.827l1.416 1.417c.777.777 2.046.78 2.827-.002L24 28.243l8.485 8.485c.784.783 2.047.782 2.827 0l1.417-1.416c.777-.777.78-2.046-.002-2.827L28.243 24l8.485-8.485c.783-.784.782-2.047 0-2.827l-1.416-1.417c-.777-.777-2.046-.78-2.827.002L24 19.757zM24 47c12.703 0 23-10.297 23-23S36.703 1 24 1 1 11.297 1 24s10.297 23 23 23z" />
</symbol>
<symbol id="icon-eyeglass" viewBox="0 0 48 48">
<path d="M27.885 32.515c-2.864 1.966-6.333 3.116-10.07 3.116C7.976 35.63 0 27.656 0 17.817 0 7.976 7.976 0 17.816 0S35.63 7.976 35.63 17.816c0 3.736-1.15 7.205-3.115 10.07l14.53 14.53c1.278 1.277 1.275 3.352 0 4.628-1.28 1.278-3.353 1.278-4.63 0l-14.53-14.53zm-10.07-3.736c6.056 0 10.964-4.91 10.964-10.964 0-6.055-4.91-10.964-10.964-10.964-6.055 0-10.964 4.91-10.964 10.964 0 6.055 4.91 10.963 10.964 10.963z" />
</symbol>
<symbol id="icon-up-caret" viewBox="0 0 48 48">
<path d="M2.988 33.02c-1.66 0-1.943-.81-.618-1.824l20-15.28c.878-.672 2.31-.67 3.188 0l20.075 15.288c1.316 1.003 1.048 1.816-.62 1.816H2.987z" />
</symbol>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -0,0 +1,21 @@
<%#
# Javascript Partial
This partial imports the necessary javascript on each page.
By default, it includes the application JS,
but each page can define additional JS sources
by providing a `content_for(:javascript)` block.
%>
<% Administrate::Engine.javascripts.each do |js_path| %>
<%= javascript_include_tag js_path %>
<% end %>
<%= yield :javascript %>
<% if Rails.env.test? %>
<%= javascript_tag do %>
$.fx.off = true;
$.ajaxSetup({ async: false });
<% end %>
<% end %>

View File

@@ -0,0 +1,14 @@
<%#
# Stylesheet Partial
This partial imports the necessary stylesheets on each page.
By default, it includes the application CSS,
but each page can define additional CSS sources
by providing a `content_for(:stylesheet)` block.
%>
<% Administrate::Engine.stylesheets.each do |css_path| %>
<%= stylesheet_link_tag css_path %>
<% end %>
<%= yield :stylesheet %>

View File

@@ -0,0 +1,41 @@
<%#
# Application Layout
This view template is used as the layout
for every page that Administrate generates.
By default, it renders:
- Navigation
- Content for a search bar
(if provided by a `content_for` block in a nested page)
- Flashes
- Links to stylesheets and JavaScripts
%>
<!DOCTYPE html>
<html lang="<%= I18n.locale %>">
<head>
<meta charset="utf-8">
<meta name="ROBOTS" content="NOODP">
<meta name="viewport" content="initial-scale=1">
<title>
<%= content_for(:title) %> - Admin Panel - <%= Rails.application.name %>
</title>
<%= render "stylesheet" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render "icons" %>
<div class="app-container">
<%= render "navigation" -%>
<main class="main-content" role="main">
<%= render "flashes" -%>
<%= yield %>
</main>
</div>
<%= render "javascript" %>
</body>
</html>

View File

@@ -6,6 +6,8 @@ Rails.application.routes.draw do
root to: 'boards#index'
resources :boards
resources :comments
resources :posts
resources :post_statuses
resources :users
end