2021-07-19 15:44:14 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LabelTemplate < ApplicationRecord
|
2022-07-27 16:10:32 +08:00
|
|
|
include SearchableModel
|
|
|
|
|
2021-07-19 15:44:14 +08:00
|
|
|
enum language_type: { zpl: 0 }
|
2022-07-27 16:10:32 +08:00
|
|
|
validates :name, presence: true, length: { minimum: Constants::NAME_MIN_LENGTH,
|
|
|
|
maximum: Constants::NAME_MAX_LENGTH }
|
2021-07-19 15:44:14 +08:00
|
|
|
validates :size, presence: true
|
|
|
|
validates :content, presence: true
|
|
|
|
|
2022-07-28 20:37:59 +08:00
|
|
|
validate :default_template
|
|
|
|
|
2021-07-19 15:44:14 +08:00
|
|
|
def render(locals)
|
|
|
|
locals.reduce(content.dup) do |rendered_content, (key, value)|
|
|
|
|
rendered_content.gsub!(/\{\{#{key}\}\}/, value.to_s)
|
|
|
|
end
|
|
|
|
end
|
2022-07-28 20:37:59 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_template
|
|
|
|
if default && LabelTemplate.where(team_id: team_id, default: true, language_type: language_type)
|
|
|
|
.where.not(id: id).any?
|
|
|
|
errors.add(:default, I18n.t('activerecord.errors.models.label_template.attributes.default.already_exist'))
|
|
|
|
end
|
|
|
|
end
|
2021-07-19 15:44:14 +08:00
|
|
|
end
|