Check polymorphic type in validations

* Previously, some models had "duplicated" `belongs_to` associations, which were now removed (and replaced by the `include Creation`).
This commit is contained in:
Sebastian Serth
2023-08-20 18:01:18 +02:00
committed by Sebastian Serth
parent be4f2b790d
commit 977fa4539e
16 changed files with 26 additions and 13 deletions

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class AnomalyNotification < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :exercise
belongs_to :exercise_collection
end

View File

@ -12,6 +12,7 @@ module CodeOcean
TEACHER_DEFINED_ROLES = ROLES - %w[user_defined_file]
OWNER_READ_PERMISSION = 0o400
OTHER_READ_PERMISSION = 0o004
ALLOWED_CONTEXT_TYPES = %w[Exercise Submission CommunitySolution CommunitySolutionContribution].freeze
after_initialize :set_default_values
before_validation :clear_weight, unless: :teacher_defined_assessment?
@ -56,6 +57,7 @@ module CodeOcean
validates :weight, if: :teacher_defined_assessment?, numericality: true, presence: true
validates :weight, absence: true, unless: :teacher_defined_assessment?
validates :file, presence: true if :context.is_a?(Submission)
validates :context_type, inclusion: {in: ALLOWED_CONTEXT_TYPES}
validates_with FileNameValidator, fields: %i[name path file_type_id]

View File

@ -8,7 +8,6 @@ class Comment < ApplicationRecord
attr_accessor :username, :date, :updated, :editable
belongs_to :file, class_name: 'CodeOcean::File'
belongs_to :user, polymorphic: true
# after_save :trigger_rfc_action_cable_from_comment
def request_for_comment

View File

@ -1,12 +1,15 @@
# frozen_string_literal: true
class CommunitySolution < ApplicationRecord
ALLOWED_USER_TYPES = [InternalUser, ExternalUser].map(&:to_s).freeze
belongs_to :exercise
has_many :community_solution_locks
has_many :community_solution_contributions
has_and_belongs_to_many :users, polymorphic: true, through: :community_solution_contributions
has_many :files, class_name: 'CodeOcean::File', through: :community_solution_contributions
validates :user_type, inclusion: {in: ALLOWED_USER_TYPES}
def to_s
"Gemeinschaftslösung für #{exercise}"
end

View File

@ -4,11 +4,15 @@ module ContributorCreation
extend ActiveSupport::Concern
include Contributor
ALLOWED_CONTRIBUTOR_TYPES = [InternalUser, ExternalUser, ProgrammingGroup].map(&:to_s).freeze
included do
belongs_to :contributor, polymorphic: true
alias_method :user, :contributor
alias_method :user=, :contributor=
alias_method :author, :user
alias_method :creator, :user
validates :contributor_type, inclusion: {in: ALLOWED_CONTRIBUTOR_TYPES}
end
end

View File

@ -3,9 +3,13 @@
module Creation
extend ActiveSupport::Concern
ALLOWED_USER_TYPES = [InternalUser, ExternalUser].map(&:to_s).freeze
included do
belongs_to :user, polymorphic: true
alias_method :author, :user
alias_method :creator, :user
validates :user_type, inclusion: {in: ALLOWED_USER_TYPES}
end
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Event < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :exercise
belongs_to :file, class_name: 'CodeOcean::File'

View File

@ -1,12 +1,12 @@
# frozen_string_literal: true
class ExerciseCollection < ApplicationRecord
include Creation
include TimeHelper
has_many :exercise_collection_items, dependent: :delete_all
alias items exercise_collection_items
has_many :exercises, through: :exercise_collection_items, inverse_of: :exercise_collections
belongs_to :user, polymorphic: true
def collection_statistics
statistics = {}

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class ProgrammingGroupMembership < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :programming_group
validate :unique_membership_for_exercise

View File

@ -2,9 +2,9 @@
# TODO: reference to lti_param_model
class RemoteEvaluationMapping < ApplicationRecord
include Creation
before_create :generate_token, unless: :validation_token?
belongs_to :exercise
belongs_to :user, polymorphic: true
belongs_to :study_group, optional: true
def generate_token

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true
class Runner < ApplicationRecord
include Creation
belongs_to :execution_environment
belongs_to :user, polymorphic: true
before_validation :request_id

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
class StudyGroupMembership < ApplicationRecord
ROLES = %w[learner teacher].freeze
belongs_to :user, polymorphic: true
include Creation
belongs_to :study_group
before_save :destroy_if_empty_study_group_or_user
ROLES = %w[learner teacher].freeze
def destroy_if_empty_study_group_or_user
destroy if study_group.blank? || user.blank?
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Subscription < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :request_for_comment
belongs_to :study_group, optional: true
end

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true
class Testrun < ApplicationRecord
include Creation
belongs_to :file, class_name: 'CodeOcean::File', optional: true
belongs_to :submission
belongs_to :testrun_execution_environment, optional: true, dependent: :destroy

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class UserExerciseIntervention < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :intervention
belongs_to :exercise
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class UserProxyExerciseExercise < ApplicationRecord
belongs_to :user, polymorphic: true
include Creation
belongs_to :exercise
belongs_to :proxy_exercise