-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4e0334
commit 1629faa
Showing
3 changed files
with
22 additions
and
56 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class Product < ApplicationRecord | ||
validates :name, presence: true | ||
validates :price, numericality: { greater_than: 0 } | ||
validate :validate_vat_rate | ||
validates :sku, presence: true | ||
|
||
default_scope { where(latest: true) } | ||
|
||
before_create :set_stock_level | ||
|
||
def set_stock_level | ||
self.stock_level = 0 | ||
end | ||
|
||
def validate_vat_rate | ||
unless vat_rate.is_a?(Numeric) | ||
errors.add(:vat_rate, "is not a number") | ||
end | ||
end | ||
end |
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