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
|
2024-02-27 19:10:27 +08:00
|
|
|
include SearchableByNameModel
|
2022-07-27 16:10:32 +08:00
|
|
|
|
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
|
|
|
|
2024-03-27 19:41:30 +08:00
|
|
|
SEARCHABLE_ATTRIBUTES = ['label_templates.name',
|
|
|
|
'label_templates.description'].freeze
|
|
|
|
|
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) }
|
|
|
|
|
2024-02-27 19:10:27 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
|
|
|
joins("INNER JOIN user_assignments team_user_assignments
|
|
|
|
ON team_user_assignments.assignable_id = label_templates.team_id
|
|
|
|
AND team_user_assignments.assignable_type = 'Team'
|
|
|
|
AND team_user_assignments.user_id = #{user.id}
|
|
|
|
INNER JOIN user_roles team_user_roles
|
|
|
|
ON team_user_roles.id = team_user_assignments.user_role_id
|
|
|
|
AND team_user_roles.permissions @> ARRAY['#{TeamPermissions::LABEL_TEMPLATES_READ}']::varchar[]")
|
|
|
|
.where(team: teams)
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2024-03-27 19:41:30 +08:00
|
|
|
def self.search(
|
2024-04-03 07:04:20 +08:00
|
|
|
_user,
|
|
|
|
_include_archived,
|
2024-03-27 19:41:30 +08:00
|
|
|
query = nil,
|
|
|
|
page = 1,
|
2024-04-03 07:04:20 +08:00
|
|
|
_current_team = nil,
|
2024-03-27 19:41:30 +08:00
|
|
|
options = {}
|
|
|
|
)
|
|
|
|
|
|
|
|
new_query = LabelTemplate.where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options)
|
|
|
|
|
|
|
|
# Show all results if needed
|
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
|
|
|
new_query
|
|
|
|
else
|
|
|
|
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
|
|
|
|
end
|
|
|
|
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
|