diff options
author | blackst0ne <blackst0ne.ru@gmail.com> | 2017-04-12 08:56:33 +1100 |
---|---|---|
committer | blackst0ne <blackst0ne.ru@gmail.com> | 2017-04-12 08:56:33 +1100 |
commit | 9a048a6ea2f0c5aeea0d366ebf715b54664a5f70 (patch) | |
tree | 3a2c85708568acdf56ee766d852e73f430bbaaa1 | |
parent | 01adf920c4da977328c38836327681eb4c52cbee (diff) | |
download | gitlab-ce-9a048a6ea2f0c5aeea0d366ebf715b54664a5f70.tar.gz |
Remove heading and trailing spaces from label's color and title
-rw-r--r-- | app/models/label.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/fix_spaces_in_label_title.yml | 4 | ||||
-rw-r--r-- | spec/models/label_spec.rb | 16 |
3 files changed, 26 insertions, 0 deletions
diff --git a/app/models/label.rb b/app/models/label.rb index 568fa6d44f5..d8b0e250732 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -21,6 +21,8 @@ class Label < ActiveRecord::Base has_many :issues, through: :label_links, source: :target, source_type: 'Issue' has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest' + before_validation :strip_whitespace_from_title_and_color + validates :color, color: true, allow_blank: false # Don't allow ',' for label titles @@ -193,4 +195,8 @@ class Label < ActiveRecord::Base def sanitize_title(value) CGI.unescapeHTML(Sanitize.clean(value.to_s)) end + + def strip_whitespace_from_title_and_color + %w(color title).each { |attr| self[attr] = self[attr]&.strip } + end end diff --git a/changelogs/unreleased/fix_spaces_in_label_title.yml b/changelogs/unreleased/fix_spaces_in_label_title.yml new file mode 100644 index 00000000000..51f07438edb --- /dev/null +++ b/changelogs/unreleased/fix_spaces_in_label_title.yml @@ -0,0 +1,4 @@ +--- +title: Remove heading and trailing spaces from label's color and title +merge_request: 10603 +author: blackst0ne diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb index a9139f7d4ab..80ca19acdda 100644 --- a/spec/models/label_spec.rb +++ b/spec/models/label_spec.rb @@ -42,11 +42,27 @@ describe Label, models: true do end end + describe '#color' do + it 'strips color' do + label = described_class.new(color: ' #abcdef ') + label.valid? + + expect(label.color).to eq('#abcdef') + end + end + describe '#title' do it 'sanitizes title' do label = described_class.new(title: '<b>foo & bar?</b>') expect(label.title).to eq('foo & bar?') end + + it 'strips title' do + label = described_class.new(title: ' label ') + label.valid? + + expect(label.title).to eq('label') + end end describe 'priorization' do |