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
8 changes: 6 additions & 2 deletions app/models/concerns/provideable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ 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? }
validates :contact_name, presence: { message: "Must provide a name or a business name" }, if: proc { |ddp| ddp.business_name.blank? && validation_required? }
validates :business_name, presence: { message: "Must provide a name or a business name" }, if: proc { |ddp| ddp.contact_name.blank? && validation_required? }

scope :for_csv_export, ->(organization, *) {
where(organization: organization).order(:business_name)
}

def validation_required?
!is_a?(Vendor)
end

def self.import_csv(csv, organization)
csv.each do |row|
loc = new(row.to_hash)
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/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
4 changes: 3 additions & 1 deletion spec/support/provideable_shared_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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)
unless model_f == :vendor # business name is a required attribute for Vendor
expect(build(model_f, contact_name: "George Henry", business_name: nil).valid?).to eq(true)
end
end

it "is invalid without an organization" do
Expand Down