Skip to content

Resolves #4677 - Require Business Name for Vendors #4704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 changes: 0 additions & 3 deletions app/models/concerns/provideable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module Provideable
included do
belongs_to :organization # Automatically validates presence as of Rails 5

validates :contact_name, presence: { message: "Must provide a name or a business name" }, if: proc { |ddp| ddp.business_name.blank? }
validates :business_name, presence: { message: "Must provide a name or a business name" }, if: proc { |ddp| ddp.contact_name.blank? }

scope :for_csv_export, ->(organization, *) {
where(organization: organization).order(:business_name)
}
Expand Down
2 changes: 2 additions & 0 deletions app/models/product_drive_participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ProductDriveParticipant < ApplicationRecord

validates :phone, presence: { message: "Must provide a phone or an e-mail" }, if: proc { |pdp| pdp.email.blank? }
validates :email, presence: { message: "Must provide a phone or an e-mail" }, if: proc { |pdp| pdp.phone.blank? }
validates :contact_name, presence: { message: "Must provide a name or a business name" }, if: proc { |pdp| pdp.business_name.blank? }
validates :business_name, presence: { message: "Must provide a name or a business name" }, if: proc { |pdp| pdp.contact_name.blank? }
validates :comment, length: { maximum: 500 }

scope :alphabetized, -> { order(:contact_name) }
Expand Down
2 changes: 2 additions & 0 deletions app/models/vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Vendor < ApplicationRecord

has_many :purchases, inverse_of: :vendor, dependent: :destroy

validates :business_name, presence: true

scope :alphabetized, -> { order(:business_name) }

def volume
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BackfillVendorBusinessNameFromContact < ActiveRecord::Migration[7.1]
def change
Vendor.where(business_name: [nil, ""]).update_all('business_name = contact_name')
end
end
7 changes: 7 additions & 0 deletions spec/models/product_drive_participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
expect(build(:product_drive_participant, phone: nil)).to be_valid
expect(build(:product_drive_participant, email: nil)).to be_valid
end

it "is invalid unless it has either a contact name or a business name" do
expect(build(:product_drive_participant, contact_name: nil, business_name: nil)).not_to be_valid
expect(build(:product_drive_participant, contact_name: nil, business_name: "George Company").valid?).to eq(true)
expect(build(:product_drive_participant, contact_name: "George Henry", business_name: nil).valid?).to eq(true)
end

it "is invalid if the comment field has more than 500 characters" do
long_comment = "a" * 501
expect(build(:product_drive_participant, comment: long_comment)).not_to be_valid
Expand Down
7 changes: 7 additions & 0 deletions spec/models/vendor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
RSpec.describe Vendor, type: :model do
it_behaves_like "provideable"

context "Validations" do
it "validates that a business name is present" do
expect(build(:vendor, business_name: "Diaper Enterprises")).to be_valid
expect(build(:vendor, business_name: nil)).to_not be_valid
end
end

context "Methods" do
describe "volume" do
it "retrieves the amount of product that has been bought from this vendor" do
Expand Down
6 changes: 0 additions & 6 deletions spec/support/provideable_shared_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
let(:model_f) { described_class.to_s.underscore.to_sym }

context "Validations" do
it "is invalid unless it has either a contact name or a business name" do
expect(build(model_f, contact_name: nil, business_name: nil)).not_to be_valid
expect(build(model_f, contact_name: nil, business_name: "George Company").valid?).to eq(true)
expect(build(model_f, contact_name: "George Henry").valid?).to eq(true)
end

it "is invalid without an organization" do
expect(build(model_f, organization: nil)).not_to be_valid
end
Expand Down