summaryrefslogtreecommitdiff
path: root/app/models/product.rb
blob: 87a7a710b1dfeacfb14c8eacc0a634f1e0c2de4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

# Product
# public_id:string
# name:string
# unitary_price:float
# bulk_price:float
# picture:attachment
# available_quantity:integer
# categories:text
class Product < ApplicationRecord
  belongs_to :company

  validates :name, presence: true
  validates :unitary_price, presence: true
  validates :unitary_price, comparison: { greater_than: 0 }
  validates :bulk_price, presence: true
  validates :bulk_price, comparison: { greater_than: 0 }
  validates :available_quantity, presence: true
  validates :available_quantity, comparison: { greater_than: 0 }

  has_one_attached :picture

  around_create :generate_public_id

  serialize :categories, Array

  def to_param
    public_id
  end

  private

  def generate_public_id
    self.public_id = SecureRandom.hex(12)
  end
end