Skip to content

Commit

Permalink
Issue#dcc2983 - Fix for preventing adding a Contributor via Contributor
Browse files Browse the repository at this point in the history
page if both Name and Email are not present.

Fixes DCC bug #2983

Changes:
  - Changes in Contributor model:
       - Renamed validation method name_or_email_presence() -> name_and_email_presence()
       - Updated conditions in name_and_email_presence() method to return errors if Name or Email missing.
  - Updated tests in spec/models/contributor_spec.rb and spec/services/api/v1/contextual_error_service_spec.rb to reflect change in
Contributor model.
  • Loading branch information
John Pinto committed Nov 4, 2021
1 parent 43c5b03 commit 26b4976
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/models/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Contributor < ApplicationRecord
validates :roles, numericality: { greater_than: 0,
message: _("You must specify at least one role.") }

validate :name_or_email_presence
validate :name_and_email_presence

ONTOLOGY_NAME = "CRediT - Contributor Roles Taxonomy"
ONTOLOGY_LANDING_PAGE = "https://credit.niso.org/"
Expand Down Expand Up @@ -130,11 +130,11 @@ def merge(other)

private

def name_or_email_presence
return true unless name.blank? && email.blank?
def name_and_email_presence
return true unless name.blank? || email.blank?

errors.add(:name, _("can't be blank if no email is provided"))
errors.add(:email, _("can't be blank if no name is provided"))
errors.add(:name, _("can't be blank.")) if name.blank?
errors.add(:email, _("can't be blank.")) if email.blank?
end

end
10 changes: 5 additions & 5 deletions spec/models/contributor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.with_message("You must specify at least one role.")
end

describe "#name_or_email_presence" do
describe "#name_and_email_presence" do
before(:each) do
@contributor = build(:contributor, plan: create(:plan), investigation: true)
end
Expand All @@ -27,13 +27,13 @@
expect(@contributor.errors[:name].present?).to eql(true)
expect(@contributor.errors[:email].present?).to eql(true)
end
it "is valid if a name is present" do
it "is valid if a name is present and email is blank" do
@contributor.email = nil
expect(@contributor.valid?).to eql(true)
expect(@contributor.valid?).to eql(false)
end
it "is valid if an email is present" do
it "is valid if an email is present and name is blank" do
@contributor.name = nil
expect(@contributor.valid?).to eql(true)
expect(@contributor.valid?).to eql(false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/services/api/v1/contextual_error_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
result = described_class.contextualize(errors: @plan.errors)
expect(result.length).to eql(2)
expect(result.first.start_with?("Contact/Contributor ")).to eql(true)
expect(result.first.include?(" can't be blank if no ")).to eql(true)
expect(result.first.include?(" can't be blank")).to eql(true)
end
it "returns errors if a Contributor Org is invalid" do
@plan.contributors.first.org.name = nil
Expand Down

0 comments on commit 26b4976

Please sign in to comment.