diff --git a/app/models/author.rb b/app/models/author.rb index d0dd36632..423d4be90 100644 --- a/app/models/author.rb +++ b/app/models/author.rb @@ -171,9 +171,9 @@ def assign_pub(pub) cap_profile_id: cap_profile_id, featured: false, status: 'new', visibility: 'private' ) - pub.pubhash_needs_update! # Add to pub_hash[:authorship] - pub.save! # contrib.save! not needed end + pub.pubhash_needs_update! # Add to pub_hash[:authorship] + pub.save! # contrib.save! not needed end private diff --git a/spec/factories/publication.rb b/spec/factories/publication.rb index a5ff9f66b..8f82db480 100644 --- a/spec/factories/publication.rb +++ b/spec/factories/publication.rb @@ -52,4 +52,16 @@ identifier_value: '10048354') end end + + factory :publication_without_author, parent: :publication do + pub_hash do + { + title: title, + type: publication_type, + year: year, + author: [], + authorship: [] + } + end + end end diff --git a/spec/models/author_spec.rb b/spec/models/author_spec.rb index 635eff33f..0a88efe3e 100644 --- a/spec/models/author_spec.rb +++ b/spec/models/author_spec.rb @@ -177,4 +177,22 @@ expect(subject).not_to be_harvestable end end + + describe '#assign_pub' do + let(:pub) { create :publication_without_author } + + it 'creates contrib and updates pubhash' do + expect(subject.publications.count).to eq(0) + expect(subject.contributions.count).to eq(0) + expect(Publication.find(pub.id).pub_hash[:authorship].length).to eq(0) + + subject.assign_pub(pub) + expect(subject.publications.count).to eq(1) + expect(subject.contributions.count).to eq(1) + + # Verify that author added to publication + # See https://github.com/sul-dlss/sul_pub/issues/1052 + expect(Publication.find(pub.id).pub_hash[:authorship].length).to eq(1) + end + end end