diff --git a/app/models/badge.rb b/app/models/badge.rb index 222855cccd8..d9601a76585 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -115,10 +115,12 @@ class Badge < ActiveRecord::Base has_many :user_badges, 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 :allow_title, 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) } @@ -220,7 +222,7 @@ class Badge < ActiveRecord::Base end def self.i18n_name(name) - name.downcase.tr(" ", "_") + name.to_s.downcase.tr(" ", "_") end def self.display_name(name) diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb index 60bd7a2a7a0..a7e49b5ff25 100644 --- a/spec/models/badge_spec.rb +++ b/spec/models/badge_spec.rb @@ -1,6 +1,17 @@ # frozen_string_literal: true 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 expect(Badge.create!(name: "test", badge_type_id: 1).system?).to be false end