diff --git a/app/models/publication.rb b/app/models/publication.rb index 54aeb985a0..ac6b98899c 100755 --- a/app/models/publication.rb +++ b/app/models/publication.rb @@ -49,7 +49,7 @@ class Publication < ApplicationRecord has_one :content_blob, ->(r) { where('content_blobs.asset_version =? AND deleted=?', r.version, false) }, as: :asset, foreign_key: :asset_id explicit_versioning(:version_column => "version", sync_ignore_columns: ['license','other_creators']) do - acts_as_versioned_resource + acts_as_versioned_resource has_one :content_blob, -> (r) { where('content_blobs.asset_version =? AND content_blobs.asset_type =?', r.version, r.parent.class.name) }, :primary_key => :publication_id,:foreign_key => :asset_id @@ -58,7 +58,7 @@ def doi_uri end alias_method :doi_identifier, :doi_uri -end + end belongs_to :publication_type @@ -78,6 +78,8 @@ def doi_uri accepts_nested_attributes_for :publication_authors + auto_strip_attributes :abstract + # Types of registration REGISTRATION_BY_PUBMED = 1 REGISTRATION_BY_DOI = 2 diff --git a/lib/tasks/seek_upgrades.rake b/lib/tasks/seek_upgrades.rake index d1d98840d6..6a8326e392 100644 --- a/lib/tasks/seek_upgrades.rake +++ b/lib/tasks/seek_upgrades.rake @@ -18,6 +18,7 @@ namespace :seek do update_rdf update_morpheus_model db:seed:018_discipline_vocab + strip_publication_abstracts ] # these are the tasks that are executes for each upgrade as standard, and rarely change @@ -98,6 +99,21 @@ namespace :seek do end end + task(strip_publication_abstracts: [:environment]) do + puts 'Stripping publication abstracts...' + updated_count = 0 + Publication.select(:id, :abstract).find_each do |publication| + if publication.abstract.present? + stripped = publication.abstract.strip + if stripped.length != publication.abstract.length + publication.update_column(:abstract, stripped) + updated_count += 1 + end + end + end + puts "... updated #{updated_count} publications" + end + private ## diff --git a/test/unit/publication_test.rb b/test/unit/publication_test.rb index 334b9eb21d..1669f811db 100644 --- a/test/unit/publication_test.rb +++ b/test/unit/publication_test.rb @@ -539,4 +539,49 @@ class PublicationTest < ActiveSupport::TestCase assert_includes publication.related_models, assay_model assert_includes publication.related_models, model end + + test 'strips leading and trailing whitespace from abstract on save' do + publication = FactoryBot.build(:publication, abstract: + " \n This is an abstract with leading and trailing whitespace \n ") + + User.with_current_user(publication.contributor) do + publication.save! + end + + assert_equal 'This is an abstract with leading and trailing whitespace', publication.abstract + end + + test 'handles nil abstract gracefully' do + publication = FactoryBot.build(:publication, abstract: nil) + + User.with_current_user(publication.contributor) do + publication.save! + end + + assert_nil publication.abstract + end + + test 'strips whitespace from abstract when extracted from pubmed' do + publication_hash = { + 'title' => 'SEEK publication', + 'abstract' => " An investigation into blalblabla \n ", + 'journal' => 'The testing journal', + 'pubmed' => '12345', + 'doi' => nil + } + bio_reference = Bio::Reference.new(publication_hash) + publication = Publication.new( + title: 'Test', + projects: [FactoryBot.create(:project)], + publication_type: FactoryBot.create(:journal) + ) + + publication.extract_pubmed_metadata(bio_reference) + + User.with_current_user(FactoryBot.create(:person).user) do + publication.save! + end + + assert_equal 'An investigation into blalblabla', publication.abstract + end end