From 3300017d98eff74ec467431576b0e80471731121 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sun, 1 Oct 2023 15:05:10 +0100 Subject: [PATCH] Add administrate --- Gemfile | 1 + Gemfile.lock | 33 ++++++++ .../admin/application_controller.rb | 21 +++++ .../admin/attendances_controller.rb | 46 +++++++++++ app/controllers/admin/courses_controller.rb | 46 +++++++++++ app/controllers/admin/lectures_controller.rb | 46 +++++++++++ app/dashboards/attendance_dashboard.rb | 69 ++++++++++++++++ app/dashboards/course_dashboard.rb | 75 ++++++++++++++++++ app/dashboards/lecture_dashboard.rb | 78 +++++++++++++++++++ config/routes.rb | 7 ++ 10 files changed, 422 insertions(+) create mode 100644 app/controllers/admin/application_controller.rb create mode 100644 app/controllers/admin/attendances_controller.rb create mode 100644 app/controllers/admin/courses_controller.rb create mode 100644 app/controllers/admin/lectures_controller.rb create mode 100644 app/dashboards/attendance_dashboard.rb create mode 100644 app/dashboards/course_dashboard.rb create mode 100644 app/dashboards/lecture_dashboard.rb diff --git a/Gemfile b/Gemfile index 64ceb73..d45492c 100644 --- a/Gemfile +++ b/Gemfile @@ -76,3 +76,4 @@ gem "font-awesome-sass" gem "httparty" gem "sidekiq" +gem "administrate" diff --git a/Gemfile.lock b/Gemfile.lock index 6e12f9c..d358d06 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,14 @@ GEM tzinfo (~> 2.0) addressable (2.8.5) public_suffix (>= 2.0.2, < 6.0) + administrate (0.19.0) + actionpack (>= 5.0) + actionview (>= 5.0) + activerecord (>= 5.0) + jquery-rails (>= 4.0) + kaminari (>= 1.0) + sassc-rails (~> 2.1) + selectize-rails (~> 0.6) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -109,6 +117,22 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) + jquery-rails (4.6.0) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + kaminari (1.2.2) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.2) + kaminari-activerecord (= 1.2.2) + kaminari-core (= 1.2.2) + kaminari-actionview (1.2.2) + actionview + kaminari-core (= 1.2.2) + kaminari-activerecord (1.2.2) + activerecord + kaminari-core (= 1.2.2) + kaminari-core (1.2.2) loofah (2.21.3) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -187,6 +211,13 @@ GEM rubyzip (2.3.2) sassc (2.4.0) ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt + selectize-rails (0.12.6) selenium-webdriver (4.10.0) rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) @@ -209,6 +240,7 @@ GEM tailwindcss-rails (2.0.30-arm64-darwin) railties (>= 6.0.0) thor (1.2.2) + tilt (2.2.0) timeout (0.4.0) turbo-rails (1.4.0) actionpack (>= 6.0.0) @@ -237,6 +269,7 @@ PLATFORMS arm64-darwin-22 DEPENDENCIES + administrate bootsnap capybara debug diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb new file mode 100644 index 0000000..f2cffea --- /dev/null +++ b/app/controllers/admin/application_controller.rb @@ -0,0 +1,21 @@ +# All Administrate controllers inherit from this +# `Administrate::ApplicationController`, making it the ideal place to put +# authentication logic or other before_actions. +# +# If you want to add pagination or other controller-level concerns, +# you're free to overwrite the RESTful controller actions. +module Admin + class ApplicationController < Administrate::ApplicationController + before_action :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 +end diff --git a/app/controllers/admin/attendances_controller.rb b/app/controllers/admin/attendances_controller.rb new file mode 100644 index 0000000..987e805 --- /dev/null +++ b/app/controllers/admin/attendances_controller.rb @@ -0,0 +1,46 @@ +module Admin + class AttendancesController < 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 + # super + # send_foo_updated_email(requested_resource) + # 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 + + # The result of this lookup will be available as `requested_resource` + + # 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 + + # Override `resource_params` if you want to transform the submitted + # data before it's persisted. For example, the following would turn all + # empty values into nil values. It uses other APIs such as `resource_class` + # and `dashboard`: + # + # def resource_params + # params.require(resource_class.model_name.param_key). + # permit(dashboard.permitted_attributes(action_name)). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-demo.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/courses_controller.rb b/app/controllers/admin/courses_controller.rb new file mode 100644 index 0000000..d7bea0e --- /dev/null +++ b/app/controllers/admin/courses_controller.rb @@ -0,0 +1,46 @@ +module Admin + class CoursesController < 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 + # super + # send_foo_updated_email(requested_resource) + # 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 + + # The result of this lookup will be available as `requested_resource` + + # 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 + + # Override `resource_params` if you want to transform the submitted + # data before it's persisted. For example, the following would turn all + # empty values into nil values. It uses other APIs such as `resource_class` + # and `dashboard`: + # + # def resource_params + # params.require(resource_class.model_name.param_key). + # permit(dashboard.permitted_attributes(action_name)). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-demo.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/lectures_controller.rb b/app/controllers/admin/lectures_controller.rb new file mode 100644 index 0000000..1fda180 --- /dev/null +++ b/app/controllers/admin/lectures_controller.rb @@ -0,0 +1,46 @@ +module Admin + class LecturesController < 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 + # super + # send_foo_updated_email(requested_resource) + # 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 + + # The result of this lookup will be available as `requested_resource` + + # 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 + + # Override `resource_params` if you want to transform the submitted + # data before it's persisted. For example, the following would turn all + # empty values into nil values. It uses other APIs such as `resource_class` + # and `dashboard`: + # + # def resource_params + # params.require(resource_class.model_name.param_key). + # permit(dashboard.permitted_attributes(action_name)). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-demo.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/dashboards/attendance_dashboard.rb b/app/dashboards/attendance_dashboard.rb new file mode 100644 index 0000000..b5a0d5b --- /dev/null +++ b/app/dashboards/attendance_dashboard.rb @@ -0,0 +1,69 @@ +require "administrate/base_dashboard" + +class AttendanceDashboard < 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, + associated_toggl_entry: Field::String, + kind: Field::Select.with_options(searchable: false, collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys }), + lecture: 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[ + id + associated_toggl_entry + kind + lecture + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + associated_toggl_entry + kind + lecture + 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[ + associated_toggl_entry + kind + lecture + ].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) { resources.where(open: true) } + # }.freeze + COLLECTION_FILTERS = {}.freeze + + # Overwrite this method to customize how attendances are displayed + # across all pages of the admin dashboard. + # + # def display_resource(attendance) + # "Attendance ##{attendance.id}" + # end +end diff --git a/app/dashboards/course_dashboard.rb b/app/dashboards/course_dashboard.rb new file mode 100644 index 0000000..cab4f24 --- /dev/null +++ b/app/dashboards/course_dashboard.rb @@ -0,0 +1,75 @@ +require "administrate/base_dashboard" + +class CourseDashboard < 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, + lectures: Field::HasMany, + panopto_folders: Field::String.with_options(searchable: false), + title: Field::String, + toggl_project: Field::Number, + unit_code: Field::String, + 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[ + id + lectures + panopto_folders + title + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + lectures + panopto_folders + title + toggl_project + unit_code + 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[ + lectures + panopto_folders + title + toggl_project + unit_code + ].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) { resources.where(open: true) } + # }.freeze + COLLECTION_FILTERS = {}.freeze + + # Overwrite this method to customize how courses are displayed + # across all pages of the admin dashboard. + # + # def display_resource(course) + # "Course ##{course.id}" + # end +end diff --git a/app/dashboards/lecture_dashboard.rb b/app/dashboards/lecture_dashboard.rb new file mode 100644 index 0000000..c182d23 --- /dev/null +++ b/app/dashboards/lecture_dashboard.rb @@ -0,0 +1,78 @@ +require "administrate/base_dashboard" + +class LectureDashboard < 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, + attendance: Field::HasOne, + course: Field::BelongsTo, + recording_id: Field::String, + start_time: Field::DateTime, + status: Field::Select.with_options(searchable: false, collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys }), + title: Field::String, + 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[ + id + attendance + course + recording_id + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + attendance + course + recording_id + start_time + status + title + 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[ + attendance + course + recording_id + start_time + status + title + ].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) { resources.where(open: true) } + # }.freeze + COLLECTION_FILTERS = {}.freeze + + # Overwrite this method to customize how lectures are displayed + # across all pages of the admin dashboard. + # + # def display_resource(lecture) + # "Lecture ##{lecture.id}" + # end +end diff --git a/config/routes.rb b/config/routes.rb index 4ebb54f..9f3894f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,10 @@ Rails.application.routes.draw do + namespace :admin do + resources :attendances + resources :courses + resources :lectures + + root to: "attendances#index" + end root controller: :attendance_tracker, action: :index end