Skip to content

Instantly share code, notes, and snippets.

@victorgiraldes
Created December 10, 2019 13:41
Show Gist options
  • Save victorgiraldes/9c4d81c3a8713abb79d770a9afbed201 to your computer and use it in GitHub Desktop.
Save victorgiraldes/9c4d81c3a8713abb79d770a9afbed201 to your computer and use it in GitHub Desktop.
class Supply::SupplyCreateValidator < BaseValidator
MODEL = Supply
attr_reader :supply, :company, :stock_group
validates :supply, presence: true
#validate :validate_group_zprovider
#validate :check_invoice
validate :validate_date
def initialize(supply, company)
@supply = supply
@company = company
__setobj__(@supply)
end
protected
def validate_date
errors.add(:date, :blank) if @supply.date.blank?
false
end
def validate_group_provider
return unless @company.group.providers.where(id: @supply.provider_id).exists?
errors.add(:provider, :not_exist)
false
end
def check_invoice
return unless @company.supplies.where("invoice = ?", @supply.invoice).any?
errors.add(:invoice, :in_use, code: @supply.invoice)
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment