Created
December 10, 2019 13:41
-
-
Save victorgiraldes/9c4d81c3a8713abb79d770a9afbed201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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