This repository has been archived on 2021-11-24. You can view files and clone it, but cannot push or open issues or pull requests.
phone_app/app/models/s_brand.rb

27 lines
571 B
Ruby

class SBrand < ApplicationRecord
has_many :p_products
has_many :p_product_refs, through: :p_products
validates :name, :presence => true, :uniqueness => true
before_save do
if self.code.blank?
generate_code
end
end
def generate_code
if self.code.blank?
prefix = self.name.slice(0, 4).to_slug.upcase
last_number = 1
code = prefix + "%02d" % [last_number]
while self.class.find_by(code: code)
last_number += 1
code = prefix + "%02d" % [last_number]
end
self.code = code
end
end
end