Skip to content

Commit

Permalink
omit blank objectives when updating lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsbailey committed Nov 19, 2021
1 parent 5ab4ebf commit c0f3e32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dashboard/app/models/lesson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,12 @@ def update_objectives(objectives)
return unless objectives

self.objectives = objectives.map do |objective|
next nil unless objective['description'].present?
persisted_objective = objective['id'].blank? ? Objective.new(key: SecureRandom.uuid) : Objective.find(objective['id'])
persisted_objective.description = objective['description']
persisted_objective.save!
persisted_objective
end
end.compact
end

# Used for seeding from JSON. Returns the full set of information needed to
Expand Down
19 changes: 19 additions & 0 deletions dashboard/test/controllers/lessons_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,25 @@ class LessonsControllerTest < ActionController::TestCase
assert_equal 'edited description', objective.description
end

test 'objectives with empty description are removed' do
sign_in @levelbuilder
objective_to_keep = create :objective, description: 'to keep', lesson: @lesson
objective_to_remove = create :objective, description: 'to remove', lesson: @lesson
assert_equal 2, @lesson.objectives.count

objectives_data = @lesson.summarize_for_lesson_edit[:objectives]
objectives_data[1][:description] = ''
objectives_data.push(id: nil, description: '')

new_update_params = @update_params.merge({objectives: [objective_to_keep.summarize_for_edit].to_json})
put :update, params: new_update_params
@lesson.reload

assert_equal 1, @lesson.objectives.count
assert_nil Objective.find_by_id(objective_to_remove.id)
assert_not_nil objective_to_keep.reload
end

test 'add script level via lesson update' do
sign_in @levelbuilder
activity = create :lesson_activity, lesson: @lesson
Expand Down

0 comments on commit c0f3e32

Please sign in to comment.