From 84df9e2722aecf960451198125cb5844fc333f59 Mon Sep 17 00:00:00 2001 From: Jonathanlau92 Date: Tue, 28 Apr 2020 21:51:30 +0800 Subject: [PATCH] Add admin page to view all data upon login --- Gemfile | 2 + Gemfile.lock | 36 +++++++ app/assets/config/manifest.js | 2 + .../admin/application_controller.rb | 21 ++++ app/controllers/admin/students_controller.rb | 46 ++++++++ app/controllers/admin/tutors_controller.rb | 46 ++++++++ app/controllers/admin/users_controller.rb | 46 ++++++++ app/dashboards/student_dashboard.rb | 102 ++++++++++++++++++ app/dashboards/tutor_dashboard.rb | 102 ++++++++++++++++++ app/dashboards/user_dashboard.rb | 75 +++++++++++++ .../admin/application/_navigation.html.erb | 20 ++++ config/routes.rb | 7 ++ 12 files changed, 505 insertions(+) create mode 100644 app/controllers/admin/application_controller.rb create mode 100644 app/controllers/admin/students_controller.rb create mode 100644 app/controllers/admin/tutors_controller.rb create mode 100644 app/controllers/admin/users_controller.rb create mode 100644 app/dashboards/student_dashboard.rb create mode 100644 app/dashboards/tutor_dashboard.rb create mode 100644 app/dashboards/user_dashboard.rb create mode 100644 app/views/admin/application/_navigation.html.erb diff --git a/Gemfile b/Gemfile index 006f899..5b09be9 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,8 @@ gem 'jbuilder', '~> 2.7' # User registration, login,logout etc gem 'devise' +# Admin page +gem "administrate" # Multi Pages form gem 'wicked' diff --git a/Gemfile.lock b/Gemfile.lock index 206ff92..c131deb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -58,6 +58,19 @@ GEM zeitwerk (~> 2.2) addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) + administrate (0.13.0) + actionpack (>= 4.2) + actionview (>= 4.2) + activerecord (>= 4.2) + autoprefixer-rails (>= 6.0) + datetime_picker_rails (~> 0.0.7) + jquery-rails (>= 4.0) + kaminari (>= 1.0) + momentjs-rails (~> 2.8) + sassc-rails (~> 2.1) + selectize-rails (~> 0.6) + autoprefixer-rails (9.7.6) + execjs bcrypt (3.1.13) bindex (0.8.1) bootsnap (1.4.6) @@ -75,6 +88,8 @@ GEM childprocess (3.0.0) concurrent-ruby (1.1.6) crass (1.0.6) + datetime_picker_rails (0.0.7) + momentjs-rails (>= 2.8.1) devise (4.7.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -86,6 +101,7 @@ GEM dotenv (= 2.7.5) railties (>= 3.2, < 6.1) erubi (1.9.0) + execjs (2.7.0) ffi (1.12.2) globalid (0.4.2) activesupport (>= 4.2.0) @@ -93,6 +109,22 @@ GEM concurrent-ruby (~> 1.0) jbuilder (2.10.0) activesupport (>= 5.0.0) + jquery-rails (4.3.5) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + kaminari (1.2.0) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.0) + kaminari-activerecord (= 1.2.0) + kaminari-core (= 1.2.0) + kaminari-actionview (1.2.0) + actionview + kaminari-core (= 1.2.0) + kaminari-activerecord (1.2.0) + activerecord + kaminari-core (= 1.2.0) + kaminari-core (1.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -109,6 +141,8 @@ GEM mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.0) + momentjs-rails (2.20.1) + railties (>= 3.1) msgpack (1.3.3) nio4r (2.5.2) nokogiri (1.10.9) @@ -170,6 +204,7 @@ GEM sprockets (> 3.0) sprockets-rails tilt + selectize-rails (0.12.6) selenium-webdriver (3.142.7) childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) @@ -220,6 +255,7 @@ PLATFORMS ruby DEPENDENCIES + administrate bootsnap (>= 1.4.2) byebug capybara (>= 2.15) diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index ac907b3..9b82681 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -1 +1,3 @@ //= link_tree ../images +//= link administrate/application.css +//= link administrate/application.js diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb new file mode 100644 index 0000000..a01e3d0 --- /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_user! + + # 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/students_controller.rb b/app/controllers/admin/students_controller.rb new file mode 100644 index 0000000..27fd7aa --- /dev/null +++ b/app/controllers/admin/students_controller.rb @@ -0,0 +1,46 @@ +module Admin + class StudentsController < 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). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-prototype.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/controllers/admin/tutors_controller.rb b/app/controllers/admin/tutors_controller.rb new file mode 100644 index 0000000..2334456 --- /dev/null +++ b/app/controllers/admin/tutors_controller.rb @@ -0,0 +1,46 @@ +module Admin + class TutorsController < 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). + # transform_values { |value| value == "" ? nil : value } + # 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 new file mode 100644 index 0000000..9f0b829 --- /dev/null +++ b/app/controllers/admin/users_controller.rb @@ -0,0 +1,46 @@ +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 + # 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). + # transform_values { |value| value == "" ? nil : value } + # end + + # See https://administrate-prototype.herokuapp.com/customizing_controller_actions + # for more information + end +end diff --git a/app/dashboards/student_dashboard.rb b/app/dashboards/student_dashboard.rb new file mode 100644 index 0000000..396b065 --- /dev/null +++ b/app/dashboards/student_dashboard.rb @@ -0,0 +1,102 @@ +require "administrate/base_dashboard" + +class StudentDashboard < 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, + personal_consent: Field::Boolean, + full_name: Field::String, + school_email: Field::String, + alternate_email: Field::String, + sex: Field::String.with_options(searchable: false), + subject_preferences: Field::String.with_options(searchable: false), + education_level: Field::String.with_options(searchable: false), + parental_consent: Field::Boolean, + match_count: Field::Number, + created_at: Field::DateTime, + updated_at: Field::DateTime, + subject_1: Field::String, + subject_2: Field::String, + subject_3: Field::String, + others_subject: Field::String, + contact_number: Field::String, + }.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 + personal_consent + full_name + school_email + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + personal_consent + full_name + school_email + alternate_email + sex + subject_preferences + education_level + parental_consent + match_count + created_at + updated_at + subject_1 + subject_2 + subject_3 + others_subject + contact_number + ].freeze + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = %i[ + personal_consent + full_name + school_email + alternate_email + sex + subject_preferences + education_level + parental_consent + match_count + subject_1 + subject_2 + subject_3 + others_subject + contact_number + ].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 students are displayed + # across all pages of the admin dashboard. + # + # def display_resource(student) + # "Student ##{student.id}" + # end +end diff --git a/app/dashboards/tutor_dashboard.rb b/app/dashboards/tutor_dashboard.rb new file mode 100644 index 0000000..88e902a --- /dev/null +++ b/app/dashboards/tutor_dashboard.rb @@ -0,0 +1,102 @@ +require "administrate/base_dashboard" + +class TutorDashboard < 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, + personal_consent: Field::Boolean, + full_name: Field::String, + school_email: Field::String, + alternate_email: Field::String, + sex: Field::Number, + subjects: Field::String.with_options(searchable: false), + hours_to_teach: Field::Number, + match_count: Field::Number, + created_at: Field::DateTime, + updated_at: Field::DateTime, + qualification: Field::String, + past_experiences: Field::Text, + reason_for_volunteering: Field::Text, + level_to_teach: Field::String, + code_of_conduct: Field::Boolean, + contact_number: Field::String, + }.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 + personal_consent + full_name + school_email + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + personal_consent + full_name + school_email + alternate_email + sex + subjects + hours_to_teach + match_count + created_at + updated_at + qualification + past_experiences + reason_for_volunteering + level_to_teach + code_of_conduct + contact_number + ].freeze + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = %i[ + personal_consent + full_name + school_email + alternate_email + sex + subjects + hours_to_teach + match_count + qualification + past_experiences + reason_for_volunteering + level_to_teach + code_of_conduct + contact_number + ].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 tutors are displayed + # across all pages of the admin dashboard. + # + # def display_resource(tutor) + # "Tutor ##{tutor.id}" + # end +end diff --git a/app/dashboards/user_dashboard.rb b/app/dashboards/user_dashboard.rb new file mode 100644 index 0000000..7614f24 --- /dev/null +++ b/app/dashboards/user_dashboard.rb @@ -0,0 +1,75 @@ +require "administrate/base_dashboard" + +class UserDashboard < 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, + email: Field::String, + encrypted_password: Field::String, + reset_password_token: Field::String, + reset_password_sent_at: Field::DateTime, + remember_created_at: Field::DateTime, + 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 + email + encrypted_password + reset_password_token + ].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = %i[ + id + email + encrypted_password + reset_password_token + reset_password_sent_at + remember_created_at + 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[ + email + encrypted_password + reset_password_token + reset_password_sent_at + remember_created_at + ].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 users are displayed + # across all pages of the admin dashboard. + # + # def display_resource(user) + # "User ##{user.id}" + # end +end diff --git a/app/views/admin/application/_navigation.html.erb b/app/views/admin/application/_navigation.html.erb new file mode 100644 index 0000000..3491e60 --- /dev/null +++ b/app/views/admin/application/_navigation.html.erb @@ -0,0 +1,20 @@ +<%# +# Navigation + +This partial is used to display the navigation in Administrate. +By default, the navigation contains navigation links +for all resources in the admin dashboard, +as defined by the routes in the `admin/` namespace +%> + + diff --git a/config/routes.rb b/config/routes.rb index 6ef68e1..2297bc2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,11 @@ Rails.application.routes.draw do + namespace :admin do + resources :users + resources :tutors + resources :students + + root to: "users#index" + end devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html resources :students