From f0b3fe7a605d6beaeab94a7e97eec144254378be Mon Sep 17 00:00:00 2001 From: riggraz Date: Sat, 24 Aug 2019 12:28:59 +0200 Subject: [PATCH] Add post statuses admin panel --- .../admin/post_statuses_controller.rb | 34 ++++++++++ app/dashboards/post_status_dashboard.rb | 64 +++++++++++++++++++ app/fields/color_field.rb | 7 ++ app/views/fields/color_field/_form.html.erb | 6 ++ app/views/fields/color_field/_index.html.erb | 1 + app/views/fields/color_field/_show.html.erb | 2 + config/routes.rb | 1 + spec/requests/admin_panel_spec.rb | 17 +++++ 8 files changed, 132 insertions(+) create mode 100644 app/controllers/admin/post_statuses_controller.rb create mode 100644 app/dashboards/post_status_dashboard.rb create mode 100644 app/fields/color_field.rb create mode 100644 app/views/fields/color_field/_form.html.erb create mode 100644 app/views/fields/color_field/_index.html.erb create mode 100644 app/views/fields/color_field/_show.html.erb diff --git a/app/controllers/admin/post_statuses_controller.rb b/app/controllers/admin/post_statuses_controller.rb new file mode 100644 index 00000000..9d3109d6 --- /dev/null +++ b/app/controllers/admin/post_statuses_controller.rb @@ -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 diff --git a/app/dashboards/post_status_dashboard.rb b/app/dashboards/post_status_dashboard.rb new file mode 100644 index 00000000..d64b9eba --- /dev/null +++ b/app/dashboards/post_status_dashboard.rb @@ -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 diff --git a/app/fields/color_field.rb b/app/fields/color_field.rb new file mode 100644 index 00000000..1ba760c5 --- /dev/null +++ b/app/fields/color_field.rb @@ -0,0 +1,7 @@ +require "administrate/field/base" + +class ColorField < Administrate::Field::Base + def to_s + data.to_s + end +end diff --git a/app/views/fields/color_field/_form.html.erb b/app/views/fields/color_field/_form.html.erb new file mode 100644 index 00000000..b0955f5b --- /dev/null +++ b/app/views/fields/color_field/_form.html.erb @@ -0,0 +1,6 @@ +
+ <%= f.label field.attribute %> +
+
+ <%= f.color_field field.attribute, style: "height: 40px" %> +
diff --git a/app/views/fields/color_field/_index.html.erb b/app/views/fields/color_field/_index.html.erb new file mode 100644 index 00000000..01c73697 --- /dev/null +++ b/app/views/fields/color_field/_index.html.erb @@ -0,0 +1 @@ +
diff --git a/app/views/fields/color_field/_show.html.erb b/app/views/fields/color_field/_show.html.erb new file mode 100644 index 00000000..00d85674 --- /dev/null +++ b/app/views/fields/color_field/_show.html.erb @@ -0,0 +1,2 @@ +
+(<%= field.to_s %>) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7e85a05e..f9c77e1f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Rails.application.routes.draw do root to: "boards#index" resources :boards + resources :post_statuses resources :users end diff --git a/spec/requests/admin_panel_spec.rb b/spec/requests/admin_panel_spec.rb index ff1242ef..69a3a3f7 100644 --- a/spec/requests/admin_panel_spec.rb +++ b/spec/requests/admin_panel_spec.rb @@ -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 \ No newline at end of file