Skip to content

Commit

Permalink
Merge pull request #4709 from sul-dlss/normalize-admin-tag-whitespace…
Browse files Browse the repository at this point in the history
…/argo#4327

Normalize administrative tag labels to remove leading/trailing whitespace
  • Loading branch information
justinlittman authored Mar 11, 2024
2 parents f87fb95 + 1e0dd30 commit dc93a5d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
2 changes: 0 additions & 2 deletions app/models/administrative_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

# Administrative tags for an object
class AdministrativeTag < ApplicationRecord
VALID_TAG_PATTERN = /\A.+( : .+)+\z/

belongs_to :tag_label
end
10 changes: 4 additions & 6 deletions app/models/tag_label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
# This stores the tag text. This text could be applied to more than one object by an AdministrativeTag
class TagLabel < ApplicationRecord
VALID_TAG_PATTERN = /\A.+( : .+)+\z/
has_many :administrative_tags, dependent: :destroy

# rubocop:disable Rails/I18nLocaleTexts
has_many :administrative_tags, dependent: :destroy
normalizes :tag, with: ->(tag) { tag.squish }
scope :project, -> { where('tag like ?', 'Project : %') }
validates :tag, format: {
with: VALID_TAG_PATTERN,
message: 'must be a series of 2 or more strings delimited with space-padded colons, e.g., "Registered By : mjgiarlo : now"'
message: 'must be a series of 2 or more strings delimited with space-padded colons, e.g., "Registered By : mjgiarlo : now"' # rubocop:disable Rails/I18nLocaleTexts
}
# rubocop:enable Rails/I18nLocaleTexts

scope :project, -> { where('tag like ?', 'Project : %') }
end
38 changes: 34 additions & 4 deletions spec/models/tag_label_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,49 @@
RSpec.describe TagLabel do
describe 'tag format validation' do
context 'with invalid values' do
['Configured With', 'Registered By:mjg'].each do |tag_string|
subject(:tag) { described_class.new(tag: tag_string) }
['Configured With', 'Registered By:mjg'].each do |tag|
subject(:label) { described_class.new(tag:) }

it { is_expected.not_to be_valid }
end
end

context 'with valid values' do
['Registered By : mjgiarlo', 'Process : Content Type : Map'].each do |tag_string|
subject(:tag) { described_class.new(tag: tag_string) }
['Registered By : mjgiarlo', 'Process : Content Type : Map'].each do |tag|
subject(:label) { described_class.new(tag:) }

it { is_expected.to be_valid }
end
end
end

describe 'tag format normalization' do
subject(:label) { described_class.new(tag:) }

let(:expected_tag) { 'Registered By : mjgiarlo' }

context 'with leading whitespace' do
let(:tag) { ' Registered By : mjgiarlo' }

it 'removes leading whitespace' do
expect(label.tag).to eq(expected_tag)
end
end

context 'with trailing whitespace' do
let(:tag) { 'Registered By : mjgiarlo ' }

it 'removes trailing whitespace' do
expect(label.tag).to eq(expected_tag)
end
end

context 'without leading and trailing whitespace' do
let(:tag) { 'Registered By : mjgiarlo' }

it 'leaves the tag as is' do
expect(label.tag).to eq(expected_tag)
end
end
end
end

0 comments on commit dc93a5d

Please sign in to comment.