Skip to content

Commit

Permalink
Handle special characters in "target" completion
Browse files Browse the repository at this point in the history
Hyphens should be suppressed and underscores are to be taken as pairs
which contain the real value to be picked.

Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Mar 20, 2024
1 parent c064618 commit 37d4967
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
26 changes: 21 additions & 5 deletions app/javascript/controllers/thing_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,30 @@ export default class extends Controller {
return;
}

const authors = text.split(',').map(function(author) {
return author.split(' ').pop().trim();
});

this.firstPart = authors.join('');
this.firstPart = text.split(',').map(this.parseAuthor).join('');
this.identifierTarget.value = `${this.firstPart}${this.lastPart}`;
}

// Returns the author compressed as expected for "target" autocompletion.
parseAuthor(author) {
// First of all try to match a pair of "_". If these are found, then we have
// to return whatever is in there with no spaces.
let idx = author.indexOf('_');

if (idx > -1) {
let idx2 = author.indexOf('_', idx + 1);
if (idx2 > -1) {
return author.substring(idx + 1, idx2).replace(' ', '');
}
}

// There was no pair of underscores, let's proceed by picking on the last
// name.
return author
.split(' ').pop().trim() // Pick the last element.
.replace('-', ''); // "Last-Other" => "LastOther"
}

// Update the information we get from the year field so to autocomplete it
// into the "target" field if possible.
yearUpdate() {
Expand Down
28 changes: 25 additions & 3 deletions test/system/things_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ThingsTest < ApplicationSystemTestCase
assert_text I18n.t('activerecord.attributes.thing.access')
end

test '"target" gets generated from last name' do
test 'js: "target" gets generated from last name' do
visit new_thing_url

fill_in I18n.t('activerecord.attributes.thing.authors'), with: 'Name LastName'
Expand All @@ -131,7 +131,7 @@ class ThingsTest < ApplicationSystemTestCase
assert_equal 'LastName', text
end

test '"target" gets generated from last name and year' do
test 'js: "target" gets generated from last name and year' do
visit new_thing_url

fill_in I18n.t('activerecord.attributes.thing.authors'), with: 'Name LastName'
Expand All @@ -142,7 +142,7 @@ class ThingsTest < ApplicationSystemTestCase
assert_equal 'LastName2024', text
end

test '"target" gets generated from multiple names and year' do
test 'js: "target" gets generated from multiple names and year' do
visit new_thing_url

fill_in I18n.t('activerecord.attributes.thing.authors'), with: 'Name LastName, John Smith'
Expand All @@ -152,4 +152,26 @@ class ThingsTest < ApplicationSystemTestCase

assert_equal 'LastNameSmith2024', text
end

test 'js: hyphenated names are treated together on "target" automation' do
visit new_thing_url

fill_in I18n.t('activerecord.attributes.thing.authors'), with: 'Name Last-Other, John Smith'
fill_in I18n.t('activerecord.attributes.thing.year'), with: '2024'

text = find_field(I18n.t('activerecord.attributes.thing.target')).value

assert_equal 'LastOtherSmith2024', text
end

test 'js: names surrounded by underscores are treated together on "target" automation' do
visit new_thing_url

fill_in I18n.t('activerecord.attributes.thing.authors'), with: 'Name _Last Other_, John Smith'
fill_in I18n.t('activerecord.attributes.thing.year'), with: '2024'

text = find_field(I18n.t('activerecord.attributes.thing.target')).value

assert_equal 'LastOtherSmith2024', text
end
end

0 comments on commit 37d4967

Please sign in to comment.