55#
66# id :integer not null, primary key
77# description :string
8- # name :string not null
8+ # name :string(30) not null
99# created_at :datetime
1010# updated_at :datetime
1111# assessment_id :bigint
2323#
2424# rubocop:enable Layout/LineLength, Lint/RedundantCopDisableDirective
2525class Tag < ApplicationRecord
26+ # Constants
27+ NUM_CSV_FIELDS = 3
28+ NAME_MAX_LENGTH = 30
29+
2630 has_and_belongs_to_many :groupings
2731 belongs_to :role
2832 belongs_to :assessment , optional : true
2933
3034 has_one :course , through : :role
3135 validate :courses_should_match
32- validates :name , presence : true , length : { maximum : 30 } , uniqueness : { scope : :assessment_id , conditions : -> {
33- where . not ( assessment_id : nil )
34- } }
35-
36- # Constants
37- NUM_CSV_FIELDS = 3
36+ validates :name ,
37+ presence : true ,
38+ length : { maximum : NAME_MAX_LENGTH } ,
39+ uniqueness : { scope : :assessment_id , conditions : -> {
40+ where . not ( assessment_id : nil )
41+ } }
3842
3943 def ==( other )
4044 description == other . description &&
@@ -47,7 +51,7 @@ def self.from_csv(data, course, assignment_id)
4751 result = MarkusCsv . parse ( data ) do |row |
4852 raise CsvInvalidLineError if row . length < NUM_CSV_FIELDS
4953 name , description , role_id = row [ 0 ] , row [ 1 ] , instructors [ row [ 2 ] ]
50- if name . nil? || name . strip . blank? || role_id . nil?
54+ if name . nil? || name . strip . blank? || role_id . nil? || name . length > NAME_MAX_LENGTH
5155 raise CsvInvalidLineError
5256 end
5357
@@ -74,7 +78,7 @@ def self.from_yml(data, course, assignment_id, allow_ta_upload: false)
7478 author = ta_author . id unless ta_author . nil? || !ta_author . grader_permission . manage_assessments
7579 end
7680 name , description , role_id = row [ :name ] , row [ :description ] , author
77- if name . nil? || name . strip . blank? || role_id . nil?
81+ if name . nil? || name . strip . blank? || role_id . nil? || name . strip . length > NAME_MAX_LENGTH
7882 raise ArgumentError , I18n . t ( 'tags.invalid_tag_data' , item : row )
7983 end
8084
0 commit comments