diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb new file mode 100644 index 00000000..879a37b0 --- /dev/null +++ b/app/controllers/admin/comments_controller.rb @@ -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 diff --git a/app/controllers/admin/posts_controller.rb b/app/controllers/admin/posts_controller.rb new file mode 100644 index 00000000..655059d5 --- /dev/null +++ b/app/controllers/admin/posts_controller.rb @@ -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 diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 8f532c42..18ab215b 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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? diff --git a/app/dashboards/board_dashboard.rb b/app/dashboards/board_dashboard.rb index ec17d0dd..f241360e 100644 --- a/app/dashboards/board_dashboard.rb +++ b/app/dashboards/board_dashboard.rb @@ -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 diff --git a/app/dashboards/comment_dashboard.rb b/app/dashboards/comment_dashboard.rb new file mode 100644 index 00000000..82a6ff4c --- /dev/null +++ b/app/dashboards/comment_dashboard.rb @@ -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 diff --git a/app/dashboards/post_dashboard.rb b/app/dashboards/post_dashboard.rb new file mode 100644 index 00000000..869d6d31 --- /dev/null +++ b/app/dashboards/post_dashboard.rb @@ -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 diff --git a/app/dashboards/post_status_dashboard.rb b/app/dashboards/post_status_dashboard.rb index 10cf1f31..478cd90b 100644 --- a/app/dashboards/post_status_dashboard.rb +++ b/app/dashboards/post_status_dashboard.rb @@ -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 diff --git a/app/dashboards/user_dashboard.rb b/app/dashboards/user_dashboard.rb index 22d025f8..e0e142c9 100644 --- a/app/dashboards/user_dashboard.rb +++ b/app/dashboards/user_dashboard.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 09f70f66..8f61f1cc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 } diff --git a/app/models/post_status.rb b/app/models/post_status.rb index cbb421a7..1eaa9ef0 100644 --- a/app/models/post_status.rb +++ b/app/models/post_status.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 707f013b..1b6c7406 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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? diff --git a/app/views/admin/application/_flashes.html.erb b/app/views/admin/application/_flashes.html.erb new file mode 100644 index 00000000..61de2711 --- /dev/null +++ b/app/views/admin/application/_flashes.html.erb @@ -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? %> +