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:

committed by
Sebastian Serth

parent
be4f2b790d
commit
977fa4539e
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class AnomalyNotification < ApplicationRecord
|
class AnomalyNotification < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
belongs_to :exercise_collection
|
belongs_to :exercise_collection
|
||||||
end
|
end
|
||||||
|
@ -12,6 +12,7 @@ module CodeOcean
|
|||||||
TEACHER_DEFINED_ROLES = ROLES - %w[user_defined_file]
|
TEACHER_DEFINED_ROLES = ROLES - %w[user_defined_file]
|
||||||
OWNER_READ_PERMISSION = 0o400
|
OWNER_READ_PERMISSION = 0o400
|
||||||
OTHER_READ_PERMISSION = 0o004
|
OTHER_READ_PERMISSION = 0o004
|
||||||
|
ALLOWED_CONTEXT_TYPES = %w[Exercise Submission CommunitySolution CommunitySolutionContribution].freeze
|
||||||
|
|
||||||
after_initialize :set_default_values
|
after_initialize :set_default_values
|
||||||
before_validation :clear_weight, unless: :teacher_defined_assessment?
|
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, if: :teacher_defined_assessment?, numericality: true, presence: true
|
||||||
validates :weight, absence: true, unless: :teacher_defined_assessment?
|
validates :weight, absence: true, unless: :teacher_defined_assessment?
|
||||||
validates :file, presence: true if :context.is_a?(Submission)
|
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]
|
validates_with FileNameValidator, fields: %i[name path file_type_id]
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ class Comment < ApplicationRecord
|
|||||||
attr_accessor :username, :date, :updated, :editable
|
attr_accessor :username, :date, :updated, :editable
|
||||||
|
|
||||||
belongs_to :file, class_name: 'CodeOcean::File'
|
belongs_to :file, class_name: 'CodeOcean::File'
|
||||||
belongs_to :user, polymorphic: true
|
|
||||||
# after_save :trigger_rfc_action_cable_from_comment
|
# after_save :trigger_rfc_action_cable_from_comment
|
||||||
|
|
||||||
def request_for_comment
|
def request_for_comment
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class CommunitySolution < ApplicationRecord
|
class CommunitySolution < ApplicationRecord
|
||||||
|
ALLOWED_USER_TYPES = [InternalUser, ExternalUser].map(&:to_s).freeze
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
has_many :community_solution_locks
|
has_many :community_solution_locks
|
||||||
has_many :community_solution_contributions
|
has_many :community_solution_contributions
|
||||||
has_and_belongs_to_many :users, polymorphic: true, through: :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
|
has_many :files, class_name: 'CodeOcean::File', through: :community_solution_contributions
|
||||||
|
|
||||||
|
validates :user_type, inclusion: {in: ALLOWED_USER_TYPES}
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"Gemeinschaftslösung für #{exercise}"
|
"Gemeinschaftslösung für #{exercise}"
|
||||||
end
|
end
|
||||||
|
@ -4,11 +4,15 @@ module ContributorCreation
|
|||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
include Contributor
|
include Contributor
|
||||||
|
|
||||||
|
ALLOWED_CONTRIBUTOR_TYPES = [InternalUser, ExternalUser, ProgrammingGroup].map(&:to_s).freeze
|
||||||
|
|
||||||
included do
|
included do
|
||||||
belongs_to :contributor, polymorphic: true
|
belongs_to :contributor, polymorphic: true
|
||||||
alias_method :user, :contributor
|
alias_method :user, :contributor
|
||||||
alias_method :user=, :contributor=
|
alias_method :user=, :contributor=
|
||||||
alias_method :author, :user
|
alias_method :author, :user
|
||||||
alias_method :creator, :user
|
alias_method :creator, :user
|
||||||
|
|
||||||
|
validates :contributor_type, inclusion: {in: ALLOWED_CONTRIBUTOR_TYPES}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,9 +3,13 @@
|
|||||||
module Creation
|
module Creation
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
ALLOWED_USER_TYPES = [InternalUser, ExternalUser].map(&:to_s).freeze
|
||||||
|
|
||||||
included do
|
included do
|
||||||
belongs_to :user, polymorphic: true
|
belongs_to :user, polymorphic: true
|
||||||
alias_method :author, :user
|
alias_method :author, :user
|
||||||
alias_method :creator, :user
|
alias_method :creator, :user
|
||||||
|
|
||||||
|
validates :user_type, inclusion: {in: ALLOWED_USER_TYPES}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Event < ApplicationRecord
|
class Event < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
belongs_to :file, class_name: 'CodeOcean::File'
|
belongs_to :file, class_name: 'CodeOcean::File'
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ExerciseCollection < ApplicationRecord
|
class ExerciseCollection < ApplicationRecord
|
||||||
|
include Creation
|
||||||
include TimeHelper
|
include TimeHelper
|
||||||
|
|
||||||
has_many :exercise_collection_items, dependent: :delete_all
|
has_many :exercise_collection_items, dependent: :delete_all
|
||||||
alias items exercise_collection_items
|
alias items exercise_collection_items
|
||||||
has_many :exercises, through: :exercise_collection_items, inverse_of: :exercise_collections
|
has_many :exercises, through: :exercise_collection_items, inverse_of: :exercise_collections
|
||||||
belongs_to :user, polymorphic: true
|
|
||||||
|
|
||||||
def collection_statistics
|
def collection_statistics
|
||||||
statistics = {}
|
statistics = {}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ProgrammingGroupMembership < ApplicationRecord
|
class ProgrammingGroupMembership < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :programming_group
|
belongs_to :programming_group
|
||||||
|
|
||||||
validate :unique_membership_for_exercise
|
validate :unique_membership_for_exercise
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
# TODO: reference to lti_param_model
|
# TODO: reference to lti_param_model
|
||||||
class RemoteEvaluationMapping < ApplicationRecord
|
class RemoteEvaluationMapping < ApplicationRecord
|
||||||
|
include Creation
|
||||||
before_create :generate_token, unless: :validation_token?
|
before_create :generate_token, unless: :validation_token?
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
belongs_to :user, polymorphic: true
|
|
||||||
belongs_to :study_group, optional: true
|
belongs_to :study_group, optional: true
|
||||||
|
|
||||||
def generate_token
|
def generate_token
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Runner < ApplicationRecord
|
class Runner < ApplicationRecord
|
||||||
|
include Creation
|
||||||
belongs_to :execution_environment
|
belongs_to :execution_environment
|
||||||
belongs_to :user, polymorphic: true
|
|
||||||
|
|
||||||
before_validation :request_id
|
before_validation :request_id
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class StudyGroupMembership < ApplicationRecord
|
class StudyGroupMembership < ApplicationRecord
|
||||||
ROLES = %w[learner teacher].freeze
|
include Creation
|
||||||
|
|
||||||
belongs_to :user, polymorphic: true
|
|
||||||
belongs_to :study_group
|
belongs_to :study_group
|
||||||
|
|
||||||
before_save :destroy_if_empty_study_group_or_user
|
before_save :destroy_if_empty_study_group_or_user
|
||||||
|
|
||||||
|
ROLES = %w[learner teacher].freeze
|
||||||
|
|
||||||
def destroy_if_empty_study_group_or_user
|
def destroy_if_empty_study_group_or_user
|
||||||
destroy if study_group.blank? || user.blank?
|
destroy if study_group.blank? || user.blank?
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Subscription < ApplicationRecord
|
class Subscription < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :request_for_comment
|
belongs_to :request_for_comment
|
||||||
belongs_to :study_group, optional: true
|
belongs_to :study_group, optional: true
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Testrun < ApplicationRecord
|
class Testrun < ApplicationRecord
|
||||||
|
include Creation
|
||||||
belongs_to :file, class_name: 'CodeOcean::File', optional: true
|
belongs_to :file, class_name: 'CodeOcean::File', optional: true
|
||||||
belongs_to :submission
|
belongs_to :submission
|
||||||
belongs_to :testrun_execution_environment, optional: true, dependent: :destroy
|
belongs_to :testrun_execution_environment, optional: true, dependent: :destroy
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class UserExerciseIntervention < ApplicationRecord
|
class UserExerciseIntervention < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :intervention
|
belongs_to :intervention
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class UserProxyExerciseExercise < ApplicationRecord
|
class UserProxyExerciseExercise < ApplicationRecord
|
||||||
belongs_to :user, polymorphic: true
|
include Creation
|
||||||
belongs_to :exercise
|
belongs_to :exercise
|
||||||
belongs_to :proxy_exercise
|
belongs_to :proxy_exercise
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user