Skip to content

refactor: Files feedback_message without NULL state #3046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/models/code_ocean/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class File < ApplicationRecord

after_initialize :set_default_values
before_validation :clear_weight, unless: :teacher_defined_assessment?
before_validation :clear_feedback_message, unless: :teacher_defined_assessment?
before_validation :hash_content, if: :content_present?
before_validation :set_ancestor_values, if: :incomplete_descendent?

Expand Down Expand Up @@ -49,7 +50,6 @@ class File < ApplicationRecord
default_scope { order(path: :asc, name: :asc) }

validates :feedback_message, if: :teacher_defined_assessment?, presence: true
validates :feedback_message, absence: true, unless: :teacher_defined_assessment?
validates :hashed_content, if: :content_present?, presence: true
validates :hidden, inclusion: [true, false]
validates :hidden_feedback, inclusion: [true, false]
Expand Down Expand Up @@ -153,6 +153,11 @@ def set_default_values
end
private :set_default_values

def clear_feedback_message
self.feedback_message = ''
end
private :clear_feedback_message

def visible?
!hidden
end
Expand Down
2 changes: 0 additions & 2 deletions app/services/proforma_service/convert_task_to_exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def model_solution_files
model_solution.files.map do |task_file|
codeocean_file_from_task_file(task_file, model_solution).tap do |file|
file.role ||= 'reference_implementation'
file.feedback_message = nil
end
end
end
Expand All @@ -99,7 +98,6 @@ def task_files
@task.files.reject {|file| file.id == 'ms-placeholder-file' }.map do |task_file|
codeocean_file_from_task_file(task_file).tap do |file|
file.role ||= 'regular_file'
file.feedback_message = nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class RemoveNullFromFilesFeedbackMessage < ActiveRecord::Migration[8.0]
def up
CodeOcean::File.where(feedback_message: nil).in_batches.update_all(feedback_message: '') # rubocop:disable Rails/SkipsModelValidations

change_column_default :files, :feedback_message, ''
change_column_null :files, :feedback_message, false
end

def down
change_column_null :files, :feedback_message, true
change_column_default :files, :feedback_message, nil
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 26 additions & 10 deletions spec/models/code_ocean/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
require 'rails_helper'

RSpec.describe CodeOcean::File do
let(:file) { described_class.create.tap {|file| file.update(content: nil, hidden: nil, read_only: nil) } }
let(:file) do
described_class.new.tap do |file|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make clear that the create does not create a record here. Therefore, I use assignment and call validate.

file.assign_attributes(content: nil, hidden: nil, read_only: nil)
file.validate
end
end

it 'validates the presence of a file type' do
expect(file.errors[:file_type]).to be_present
end

it 'validates the presence of the hidden flag' do
expect(file.errors[:hidden]).to be_present
file.update(hidden: false)
file.hidden = false
file.validate
expect(file.errors[:hidden]).to be_blank
end

Expand All @@ -21,19 +27,24 @@

it 'validates the presence of the read-only flag' do
expect(file.errors[:read_only]).to be_present
file.update(read_only: false)
file.read_only = false
file.validate
expect(file.errors[:read_only]).to be_blank
end

context 'with a teacher-defined test' do
before { file.update(role: 'teacher_defined_test') }
before do
file.role = 'teacher_defined_test'
file.validate
end

it 'validates the presence of a feedback message' do
expect(file.errors[:feedback_message]).to be_present
end

it 'validates the numericality of a weight' do
file.update(weight: 'heavy')
file.weight = 'heavy'
file.validate
expect(file.errors[:weight]).to be_present
end

Expand All @@ -43,16 +54,21 @@
end

context 'with another file type' do
before { file.update(role: 'regular_file') }
before do
file.role = 'regular_file'
end

it 'validates the absence of a feedback message' do
file.update(feedback_message: 'Your solution is not correct yet.')
expect(file.errors[:feedback_message]).to be_present
it 'removes the feedback message' do
file.feedback_message = 'Your solution is not correct yet.'

expect { file.validate }
.to change(file, :feedback_message).to('')
end

it 'validates the absence of a weight' do
allow(file).to receive(:clear_weight)
file.update(weight: 1)
file.weight = 1
file.validate
expect(file.errors[:weight]).to be_present
end
end
Expand Down