Add post statuses admin panel

This commit is contained in:
riggraz
2019-08-24 12:28:59 +02:00
parent 726236b8aa
commit f0b3fe7a60
8 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
module Admin
class PostStatusesController < 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
# 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
end
end

View File

@@ -0,0 +1,64 @@
require "administrate/base_dashboard"
class PostStatusDashboard < 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,
name: Field::String,
color: ColorField,
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[
name
color
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
name
color
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[
name
color
].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 post statuses are displayed
# across all pages of the admin dashboard.
#
# def display_resource(post_status)
# "PostStatus ##{post_status.id}"
# end
end

View File

@@ -0,0 +1,7 @@
require "administrate/field/base"
class ColorField < Administrate::Field::Base
def to_s
data.to_s
end
end

View File

@@ -0,0 +1,6 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.color_field field.attribute, style: "height: 40px" %>
</div>

View File

@@ -0,0 +1 @@
<div style="background-color: <%= field.to_s %>; width: 32px; height: 32px; border-radius: 32px;"></div>

View File

@@ -0,0 +1,2 @@
<div style="background-color: <%= field.to_s %>; width: 32px; height: 32px; border-radius: 32px;"></div>
(<%= field.to_s %>)

View File

@@ -5,6 +5,7 @@ Rails.application.routes.draw do
root to: "boards#index"
resources :boards
resources :post_statuses
resources :users
end

View File

@@ -44,4 +44,21 @@ RSpec.describe 'Requests to the admin panel', type: :request do
get admin_boards_path
expect(response).to have_http_status(:success)
end
it 'requires at least a logged-in moderator to view PostStatuses admin panel' do
get admin_post_statuses_path
expect(response).to redirect_to(new_user_session_path)
sign_in user
get admin_post_statuses_path
expect(response).to redirect_to(root_path)
sign_in moderator
get admin_post_statuses_path
expect(response).to have_http_status(:success)
sign_in admin
get admin_post_statuses_path
expect(response).to have_http_status(:success)
end
end