Skip to content

Commit

Permalink
feat: Added subject and grade lists to projects [PT-188116989]
Browse files Browse the repository at this point in the history
This adds the grade_levels and subject_areas taggable lists to the project model and adds the UI to set the lists in the new/edit project form.
  • Loading branch information
dougmartin committed Aug 28, 2024
1 parent a0d38c3 commit 1f65572
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
17 changes: 17 additions & 0 deletions rails/app/controllers/admin/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def create
authorize Admin::Project
@project = Admin::Project.new(admin_project_strong_params(params[:admin_project]))

if params[:update_grade_levels]
@project.grade_level_list = (params[:grade_levels] || [])
end

if params[:update_subject_areas]
@project.subject_area_list = (params[:subject_areas] || [])
end

if @project.save
redirect_to admin_projects_url, notice: 'Project was successfully created.'
else
Expand All @@ -92,6 +100,15 @@ def create
def update
@project = Admin::Project.find(params[:id])
authorize @project

if params[:update_grade_levels]
@project.grade_level_list = (params[:grade_levels] || [])
end

if params[:update_subject_areas]
@project.subject_area_list = (params[:subject_areas] || [])
end

if @project.update(admin_project_strong_params(params[:admin_project]))
redirect_to @project, notice: 'Project was successfully updated.'
else
Expand Down
3 changes: 3 additions & 0 deletions rails/app/models/admin/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class Admin::Project < ApplicationRecord

self.table_name = 'admin_projects'

acts_as_taggable_on :grade_levels
acts_as_taggable_on :subject_areas

def changeable?(user)
# project admins can change the projects they are admins of
if user.is_project_admin? && user.admin_for_projects.include?(self)
Expand Down
8 changes: 8 additions & 0 deletions rails/app/policies/admin/project_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ def api_show?
def research_classes?
update_or_edit? || user && user.is_project_researcher?(record)
end

def edit_grade_levels?
new_or_create? || update_or_edit?
end

def edit_subject_areas?
new_or_create? || update_or_edit?
end
end
2 changes: 2 additions & 0 deletions rails/app/views/admin/projects/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
%li
Project card description:
= f.text_area :project_card_description
= render :partial => 'shared/grade_levels_edit', :locals => { :object => @project }
= render :partial => 'shared/subject_areas_edit', :locals => { :object => @project }
%hr
-# If we don't have an ID yet (new record), we can't display associations
- if @project.id
Expand Down
8 changes: 6 additions & 2 deletions rails/spec/controllers/admin/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@
end

it "assigns a newly created project as @project" do
post :create, params: { :admin_project => valid_attributes }
post :create, params: { :admin_project => valid_attributes, update_grade_levels: true, grade_levels: ["1", "2"], update_subject_areas: true, subject_areas: ["Math", "Physical Science"] }
expect(assigns(:project)).to be_a(Admin::Project)
expect(assigns(:project)).to be_persisted
expect(assigns(:project).grade_level_list).to match_array(["1", "2"])
expect(assigns(:project).subject_area_list).to match_array(["Math", "Physical Science"])
end

it "redirects to the projects index" do
Expand Down Expand Up @@ -119,8 +121,10 @@
end

it "assigns the requested project as @project" do
put :update, params: { :id => project.to_param, :admin_project => valid_attributes }
put :update, params: { :id => project.to_param, :admin_project => valid_attributes, update_grade_levels: true, grade_levels: ["3", "4"], update_subject_areas: true, subject_areas: ["Physics"] }
expect(assigns(:project)).to eq(project)
expect(assigns(:project).grade_level_list).to match_array(["3", "4"])
expect(assigns(:project).subject_area_list).to match_array(["Physics"])
end

it "redirects to the project" do
Expand Down

0 comments on commit 1f65572

Please sign in to comment.