Skip to content

Commit 27da723

Browse files
authored
605: Add user origin to school dashboard, fix relationships (#556)
## Status - Closes RaspberryPiFoundation/digital-editor-issues#605 ## Points for consideration: - Security - Performance ## What's changed? - Adds the user_origin to the school dashboard - Fixes the relationships between models since class members were split out ## Steps to perform after deploying to production N/A
1 parent f397e87 commit 27da723

File tree

11 files changed

+171
-39
lines changed

11 files changed

+171
-39
lines changed

app/controllers/admin/projects_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ProjectsController < Admin::ApplicationController
55
before_action :set_host_for_local_storage
66

77
def scoped_resource
8-
resource_class.internal_projects
8+
action_name == 'index' ? resource_class.internal_projects : resource_class.all
99
end
1010

1111
def destroy_image
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require 'administrate/base_dashboard'
4+
5+
class ClassStudentDashboard < Administrate::BaseDashboard
6+
# ATTRIBUTE_TYPES
7+
# a hash that describes the type of each of the model's fields.
8+
#
9+
# Each different type represents an Administrate::Field object,
10+
# which determines how the attribute is displayed
11+
# on pages throughout the dashboard.
12+
ATTRIBUTE_TYPES = {
13+
school_class: Field::HasOne,
14+
student_id: Field::String,
15+
created_at: Field::DateTime,
16+
updated_at: Field::DateTime
17+
}.freeze
18+
19+
# COLLECTION_ATTRIBUTES
20+
# an array of attributes that will be displayed on the model's index page.
21+
#
22+
# By default, it's limited to four items to reduce clutter on index pages.
23+
# Feel free to add, remove, or rearrange items.
24+
COLLECTION_ATTRIBUTES = %i[
25+
student_id
26+
created_at
27+
updated_at
28+
].freeze
29+
30+
# SHOW_PAGE_ATTRIBUTES
31+
# an array of attributes that will be displayed on the model's show page.
32+
SHOW_PAGE_ATTRIBUTES = %i[
33+
student_id_changed
34+
created_at
35+
updated_at
36+
].freeze
37+
38+
# FORM_ATTRIBUTES
39+
# an array of attributes that will be displayed
40+
# on the model's form (`new` and `edit`) pages.
41+
FORM_ATTRIBUTES = %i[].freeze
42+
43+
# COLLECTION_FILTERS
44+
# a hash that defines filters that can be used while searching via the search
45+
# field of the dashboard.
46+
#
47+
# For example to add an option to search for open resources by typing "open:"
48+
# in the search field:
49+
#
50+
# COLLECTION_FILTERS = {
51+
# open: ->(resources) { resources.where(open: true) }
52+
# }.freeze
53+
COLLECTION_FILTERS = {}.freeze
54+
55+
# Overwrite this method to customize how school classes are displayed
56+
# across all pages of the admin dashboard.
57+
#
58+
# def display_resource(school_class)
59+
# "SchoolClass ##{school_class.id}"
60+
# end
61+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require 'administrate/base_dashboard'
4+
5+
class ClassTeacherDashboard < Administrate::BaseDashboard
6+
# ATTRIBUTE_TYPES
7+
# a hash that describes the type of each of the model's fields.
8+
#
9+
# Each different type represents an Administrate::Field object,
10+
# which determines how the attribute is displayed
11+
# on pages throughout the dashboard.
12+
ATTRIBUTE_TYPES = {
13+
school_class: Field::HasOne,
14+
teacher_id: Field::String,
15+
created_at: Field::DateTime,
16+
updated_at: Field::DateTime
17+
}.freeze
18+
19+
# COLLECTION_ATTRIBUTES
20+
# an array of attributes that will be displayed on the model's index page.
21+
#
22+
# By default, it's limited to four items to reduce clutter on index pages.
23+
# Feel free to add, remove, or rearrange items.
24+
COLLECTION_ATTRIBUTES = %i[
25+
teacher_id
26+
created_at
27+
updated_at
28+
].freeze
29+
30+
# SHOW_PAGE_ATTRIBUTES
31+
# an array of attributes that will be displayed on the model's show page.
32+
SHOW_PAGE_ATTRIBUTES = %i[
33+
teacher_id
34+
created_at
35+
updated_at
36+
].freeze
37+
38+
# FORM_ATTRIBUTES
39+
# an array of attributes that will be displayed
40+
# on the model's form (`new` and `edit`) pages.
41+
FORM_ATTRIBUTES = %i[].freeze
42+
43+
# COLLECTION_FILTERS
44+
# a hash that defines filters that can be used while searching via the search
45+
# field of the dashboard.
46+
#
47+
# For example to add an option to search for open resources by typing "open:"
48+
# in the search field:
49+
#
50+
# COLLECTION_FILTERS = {
51+
# open: ->(resources) { resources.where(open: true) }
52+
# }.freeze
53+
COLLECTION_FILTERS = {}.freeze
54+
55+
# Overwrite this method to customize how school classes are displayed
56+
# across all pages of the admin dashboard.
57+
#
58+
# def display_resource(class_teacher)
59+
# class_teacher.teacher.name
60+
# end
61+
end

app/dashboards/lesson_dashboard.rb

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LessonDashboard < Administrate::BaseDashboard
1414
school_class: Field::BelongsTo,
1515
parent: Field::BelongsTo,
1616
copies: Field::HasMany,
17-
projects: Field::HasMany,
17+
project: Field::HasOne,
1818
id: Field::String,
1919
copied_from_id: Field::String,
2020
user_id: Field::String,
@@ -33,9 +33,8 @@ class LessonDashboard < Administrate::BaseDashboard
3333
# By default, it's limited to four items to reduce clutter on index pages.
3434
# Feel free to add, remove, or rearrange items.
3535
COLLECTION_ATTRIBUTES = %i[
36-
school
36+
name
3737
school_class
38-
parent
3938
copies
4039
].freeze
4140

@@ -44,9 +43,9 @@ class LessonDashboard < Administrate::BaseDashboard
4443
SHOW_PAGE_ATTRIBUTES = %i[
4544
school
4645
school_class
46+
project
4747
parent
4848
copies
49-
projects
5049
id
5150
copied_from_id
5251
user_id
@@ -62,20 +61,7 @@ class LessonDashboard < Administrate::BaseDashboard
6261
# FORM_ATTRIBUTES
6362
# an array of attributes that will be displayed
6463
# on the model's form (`new` and `edit`) pages.
65-
FORM_ATTRIBUTES = %i[
66-
school
67-
school_class
68-
parent
69-
copies
70-
projects
71-
copied_from_id
72-
user_id
73-
name
74-
description
75-
visibility
76-
due_date
77-
archived_at
78-
].freeze
64+
FORM_ATTRIBUTES = %i[].freeze
7965

8066
# COLLECTION_FILTERS
8167
# a hash that defines filters that can be used while searching via the search
@@ -92,7 +78,7 @@ class LessonDashboard < Administrate::BaseDashboard
9278
# Overwrite this method to customize how lessons are displayed
9379
# across all pages of the admin dashboard.
9480
#
95-
# def display_resource(lesson)
96-
# "Lesson ##{lesson.id}"
97-
# end
81+
def display_resource(lesson)
82+
lesson.name
83+
end
9884
end

app/dashboards/school_class_dashboard.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class SchoolClassDashboard < Administrate::BaseDashboard
1111
# on pages throughout the dashboard.
1212
ATTRIBUTE_TYPES = {
1313
school: Field::BelongsTo,
14-
members: Field::HasMany,
14+
teachers: Field::HasMany,
15+
students: Field::HasMany,
1516
lessons: Field::HasMany,
1617
id: Field::String,
17-
teacher_id: Field::String,
1818
name: Field::String,
1919
created_at: Field::DateTime,
2020
updated_at: Field::DateTime
@@ -26,33 +26,27 @@ class SchoolClassDashboard < Administrate::BaseDashboard
2626
# By default, it's limited to four items to reduce clutter on index pages.
2727
# Feel free to add, remove, or rearrange items.
2828
COLLECTION_ATTRIBUTES = %i[
29-
school
30-
members
29+
name
3130
lessons
32-
id
3331
].freeze
3432

3533
# SHOW_PAGE_ATTRIBUTES
3634
# an array of attributes that will be displayed on the model's show page.
3735
SHOW_PAGE_ATTRIBUTES = %i[
3836
school
39-
members
4037
lessons
41-
id
42-
teacher_id
38+
teachers
39+
students
4340
name
41+
id
4442
created_at
4543
updated_at
4644
].freeze
4745

4846
# FORM_ATTRIBUTES
4947
# an array of attributes that will be displayed
5048
# on the model's form (`new` and `edit`) pages.
51-
FORM_ATTRIBUTES = %i[
52-
school
53-
teacher_id
54-
name
55-
].freeze
49+
FORM_ATTRIBUTES = %i[].freeze
5650

5751
# COLLECTION_FILTERS
5852
# a hash that defines filters that can be used while searching via the search
@@ -69,7 +63,7 @@ class SchoolClassDashboard < Administrate::BaseDashboard
6963
# Overwrite this method to customize how school classes are displayed
7064
# across all pages of the admin dashboard.
7165
#
72-
# def display_resource(school_class)
73-
# "SchoolClass ##{school_class.id}"
74-
# end
66+
def display_resource(school_class)
67+
school_class.name
68+
end
7569
end

app/dashboards/school_dashboard.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class SchoolDashboard < Administrate::BaseDashboard
2929
verified_at: Field::DateTime,
3030
rejected_at: Field::DateTime,
3131
created_at: Field::DateTime,
32-
updated_at: Field::DateTime
32+
updated_at: Field::DateTime,
33+
user_origin: EnumField
3334
}.freeze
3435

3536
# COLLECTION_ATTRIBUTES
@@ -39,6 +40,7 @@ class SchoolDashboard < Administrate::BaseDashboard
3940
# Feel free to add, remove, or rearrange items.
4041
COLLECTION_ATTRIBUTES = %i[
4142
name
43+
user_origin
4244
reference
4345
country_code
4446
created_at
@@ -50,6 +52,7 @@ class SchoolDashboard < Administrate::BaseDashboard
5052
# an array of attributes that will be displayed on the model's show page.
5153
SHOW_PAGE_ATTRIBUTES = %i[
5254
name
55+
user_origin
5356
creator
5457
creator_role
5558
creator_department

app/fields/enum_field.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
class EnumField < Administrate::Field::Select
4+
def to_s
5+
# Use Rails' i18n for enums
6+
return if data.blank?
7+
8+
I18n.t(
9+
"activerecord.attributes.#{resource.class.model_name.i18n_key}.#{attribute}_values.#{data}",
10+
default: data.humanize
11+
)
12+
end
13+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="field-unit__label">
2+
<%= f.label field.attribute %>
3+
</div>
4+
<div class="field-unit__field">
5+
<%= f.text_field field.attribute %>
6+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= field.to_s %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= field.to_s %>

0 commit comments

Comments
 (0)