Create pace plan if primary does not exist

This also includes a controller action change from show to index. This
better follows other route endpoints where index loads the collection
page and show loads an individual pace plan. It's a little confusing
because a pace plan is loaded but the endpoint URL suggests it should
go to index.

fixes LS-2625
flag=pace_plans

test plan
- Enable pace plans feature flag and pace plans on a course
- Click Pace Plans from the course menu on a course with no pace plans
- Verify the page to load without an error
- Verify the course to have a created pace plan in the database

Change-Id: Ia2a1a7ba803f37274701c97c85d01b7f8e1d3aa3
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/273670
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Jeff Largent <jeff.largent@instructure.com>
QA-Review: Jeff Largent <jeff.largent@instructure.com>
Product-Review: Eric Saupe <eric.saupe@instructure.com>
This commit is contained in:
Eric Saupe 2021-09-15 15:18:16 -07:00
parent d6e1cf581c
commit 0aa88e6fa5
5 changed files with 26 additions and 11 deletions

View File

@ -26,11 +26,16 @@ class PacePlansController < ApplicationController
include Api::V1::Course
include K5Mode
def show
not_found unless @context.account.feature_enabled?(:pace_plans) && @context.settings[:enable_pace_plans]
return unless authorized_action(@context, @current_user, :manage_content)
def index
@pace_plan = @context.pace_plans.primary.first
if @pace_plan.nil?
@pace_plan = @context.pace_plans.create!
@context.context_module_tags.each do |module_item|
@pace_plan.pace_plan_module_items.create module_item: module_item, duration: 0
end
end
js_env({
BLACKOUT_DATES: [],
COURSE: course_json(@context, @current_user, session, [], nil),

View File

@ -37,7 +37,7 @@ class PacePlan < ActiveRecord::Base
validates :course_id, presence: true
validate :valid_secondary_context
scope :primary, -> { where(course_section_id: nil, user_id: nil) }
scope :primary, -> { not_deleted.where(course_section_id: nil, user_id: nil) }
scope :for_section, ->(section) { where(course_section_id: section) }
scope :for_user, ->(user) { where(user_id: user) }
scope :not_deleted, -> { where.not(workflow_state: 'deleted') }

View File

@ -456,7 +456,7 @@ CanvasRails::Application.routes.draw do
end
end
get 'pace_plans' => 'pace_plans#show'
get 'pace_plans' => 'pace_plans#index'
post 'collapse_all_modules' => 'context_modules#toggle_collapse_all'
resources :content_exports, only: [:create, :index, :destroy, :show]

View File

@ -86,11 +86,11 @@ describe PacePlansController, type: :controller do
user_session(@teacher)
end
describe "GET #show" do
describe "GET #index" do
it "populates js_env with course, enrollment, sections, and pace_plan details" do
@section = @course.course_sections.first
@student_enrollment = @course.enrollments.find_by(user_id: @student.id)
get :show, { params: { course_id: @course.id } }
get :index, { params: { course_id: @course.id } }
expect(response).to be_successful
expect(assigns[:js_bundles].flatten).to include(:pace_plans)
@ -138,10 +138,20 @@ describe PacePlansController, type: :controller do
}))
end
it "creates a pace plan if no primary pace plans are available" do
@pace_plan.update(user_id: @student)
expect(@course.pace_plans.count).to eq(1)
expect(@course.pace_plans.primary).to be_empty
get :index, params: { course_id: @course.id }
expect(@course.pace_plans.count).to eq(2)
expect(@course.pace_plans.primary.count).to eq(1)
expect(@course.pace_plans.primary.first.pace_plan_module_items.count).to eq(3)
end
it "responds with not found if the pace_plans feature is disabled" do
@course.account.disable_feature!(:pace_plans)
assert_page_not_found do
get :show, params: { course_id: @course.id }
get :index, params: { course_id: @course.id }
end
end
@ -149,13 +159,13 @@ describe PacePlansController, type: :controller do
@course.enable_pace_plans = false
@course.save!
assert_page_not_found do
get :show, params: { course_id: @course.id }
get :index, params: { course_id: @course.id }
end
end
it "responds with forbidden if the user doesn't have authorization" do
user_session(@student)
get :show, params: { course_id: @course.id }
get :index, params: { course_id: @course.id }
assert_unauthorized
end
end