Skip to content
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

Require at least one correct submission and a valid config before publishing #5330

Merged
merged 11 commits into from
Feb 2, 2024
16 changes: 16 additions & 0 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Activity < ApplicationRecord
has_many :labels, through: :activity_labels

validates :path, uniqueness: { scope: :repository_id, case_sensitive: false }, allow_nil: true
validate :require_correct_submission_before_publish, on: :update

token_generator :repository_token, length: 64
token_generator :access_token
Expand Down Expand Up @@ -510,4 +511,19 @@ def unique_labels(hash)
hash['labels'] = hash['labels'].uniq if hash.key? 'labels'
hash
end

def correct_submission?
return true if content_page?

submissions.any?(&:accepted?)
end

def require_correct_submission_before_publish
return if draft?
return unless draft_was
return if correct_submission?

errors.add(:base, I18n.t('activerecord.errors.models.activity.no_correct_submission'))
self.draft = true
jorg-vr marked this conversation as resolved.
Show resolved Hide resolved
end
end
2 changes: 2 additions & 0 deletions config/locales/models/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ en:
activerecord:
errors:
models:
activity:
no_correct_submission: "At least one correct submission is required, before the exercise can be published."
course_membership:
at_least_one_admin: The course should always have at least one course administrator.
api_token:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/models/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ nl:
activerecord:
errors:
models:
activity:
no_correct_submission: "Er moet minstens één correcte oplossing zijn, vooraleer de oefening kan worden gepubliceerd."
course_membership:
at_least_one_admin: Een cursus moet altijd minstens één cursusbeheerder hebben.
api_token:
Expand Down
1 change: 1 addition & 0 deletions test/controllers/activities_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ def create_exercises_return_valid
repo = create :repository, :git_stubbed
repo.admins << user
exercise = create :exercise, repository: repo, draft: true
create :correct_submission, exercise: exercise

put activity_url(exercise), params: { activity: { draft: false } }

Expand Down
16 changes: 16 additions & 0 deletions test/models/activity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,20 @@ class ActivityTest < ActiveSupport::TestCase
assert_empty Activity.repository_scope(scope: :my_institution, user: nil)
assert_empty Activity.repository_scope(scope: :my_institution, user: create(:user, institution_id: nil))
end

test 'should have at least one valid submission before publishing' do
activity = create :exercise, draft: true
activity.update(draft: false)

assert_not_empty activity.errors
assert_predicate activity.reload, :draft?

# reset errors
activity = Activity.find(activity.id)
create :correct_submission, exercise: activity
activity.update(draft: false)

assert_empty activity.errors
assert_not activity.reload.draft?
end
end
Loading