Skip to content

Commit

Permalink
do not throw an exception when attempting to assign a blank publicati…
Browse files Browse the repository at this point in the history
…on (#1055)

do not throw an exception when attempting to assign a blank publication
  • Loading branch information
peetucket authored May 8, 2019
2 parents 65ab3f1 + c4fddb5 commit 4f96347
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def harvestable?
# @param [Publication]
# @return [Contribution]
def assign_pub(pub)
unless pub # do not attempt to assign if no pub provided
logger.warn "nil publication assignment for author id #{id}"
return
end
raise 'Author must be saved before association' unless persisted?
pub.contributions.find_or_create_by!(author_id: id) do |contrib|
contrib.assign_attributes(
Expand Down
10 changes: 10 additions & 0 deletions spec/models/author_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,15 @@
# See https://github.com/sul-dlss/sul_pub/issues/1052
expect(Publication.find(pub.id).pub_hash[:authorship].length).to eq(1)
end

it 'does not attempt to assign a blank publication and does not throw an exception' do
expect(subject.publications.count).to eq(0)
expect(subject.contributions.count).to eq(0)

subject.assign_pub(nil)

expect(subject.publications.count).to eq(0)
expect(subject.contributions.count).to eq(0)
end
end
end

0 comments on commit 4f96347

Please sign in to comment.