DEV: Set limits for text fields in badges

This commit is contained in:
Loïc Guitaut 2023-04-27 15:09:23 +02:00 committed by Loïc Guitaut
parent 552697e194
commit c4c2da83b4
2 changed files with 15 additions and 2 deletions

View File

@ -115,10 +115,12 @@ class Badge < ActiveRecord::Base
has_many :user_badges, dependent: :destroy has_many :user_badges, dependent: :destroy
has_many :upload_references, as: :target, dependent: :destroy has_many :upload_references, as: :target, dependent: :destroy
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :badge_type, presence: true validates :badge_type, presence: true
validates :allow_title, inclusion: [true, false] validates :allow_title, inclusion: [true, false]
validates :multiple_grant, inclusion: [true, false] validates :multiple_grant, inclusion: [true, false]
validates :description, length: { maximum: 250 }
validates :long_description, length: { maximum: 1000 }
scope :enabled, -> { where(enabled: true) } scope :enabled, -> { where(enabled: true) }
@ -220,7 +222,7 @@ class Badge < ActiveRecord::Base
end end
def self.i18n_name(name) def self.i18n_name(name)
name.downcase.tr(" ", "_") name.to_s.downcase.tr(" ", "_")
end end
def self.display_name(name) def self.display_name(name)

View File

@ -1,6 +1,17 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Badge do RSpec.describe Badge do
describe "Validations" do
subject(:badge) { Fabricate.build(:badge) }
it { is_expected.to validate_length_of(:name).is_at_most(100) }
it { is_expected.to validate_length_of(:description).is_at_most(250) }
it { is_expected.to validate_length_of(:long_description).is_at_most(1000) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:badge_type) }
it { is_expected.to validate_uniqueness_of(:name) }
end
it "has a valid system attribute for new badges" do it "has a valid system attribute for new badges" do
expect(Badge.create!(name: "test", badge_type_id: 1).system?).to be false expect(Badge.create!(name: "test", badge_type_id: 1).system?).to be false
end end