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
|
|
|
|
|
2022-08-08 16:06:00 +08:00
|
|
|
belongs_to :team
|
2022-08-29 15:52:54 +08:00
|
|
|
belongs_to :created_by, class_name: 'User', optional: true
|
|
|
|
belongs_to :last_modified_by, class_name: 'User', optional: true
|
2022-08-08 16:06:00 +08:00
|
|
|
|
2022-10-11 16:58:37 +08:00
|
|
|
enum unit: { in: 0, mm: 1 }
|
|
|
|
|
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 :content, presence: true
|
|
|
|
|
2022-08-08 16:06:00 +08:00
|
|
|
validate :ensure_single_default_template!
|
2022-07-28 20:37:59 +08:00
|
|
|
|
2023-08-17 19:44:22 +08:00
|
|
|
scope :default, -> { where(default: true) }
|
|
|
|
|
2022-08-02 20:51:18 +08:00
|
|
|
def self.enabled?
|
2023-08-01 16:30:57 +08:00
|
|
|
ApplicationSettings.instance.values['label_templates_enabled'] == true
|
2022-08-02 20:51:18 +08:00
|
|
|
end
|
|
|
|
|
2022-08-08 16:06:00 +08:00
|
|
|
def icon
|
|
|
|
'zpl'
|
|
|
|
end
|
|
|
|
|
|
|
|
def language_type
|
|
|
|
'zpl'
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_only?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def label_format
|
|
|
|
Extends::LABEL_TEMPLATE_FORMAT_MAP[type]
|
2022-08-04 16:51:30 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 20:37:59 +08:00
|
|
|
private
|
|
|
|
|
2022-08-08 16:06:00 +08:00
|
|
|
def ensure_single_default_template!
|
2022-08-08 17:21:38 +08:00
|
|
|
if default && self.class.where(team_id: team_id, default: true, type: type)
|
2022-08-08 16:06:00 +08:00
|
|
|
.where.not(id: id).any?
|
2022-07-28 20:37:59 +08:00
|
|
|
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
|