mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-12 20:24:43 +08:00
a3ea6e608c
* Add validation to print modal [SCI-7052] * Fix dropdown on label modal print [SCI-7052] * Fix hound [SCI-7052] * Rename validation endpoint [SCI-7052] * Add zpl label printing and fix fluics printing [SCI-7221] * Fix hound [SCI-7221]
67 lines
2 KiB
Ruby
67 lines
2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module LabelTemplates
|
|
class RepositoryRowService
|
|
class UnsupportedKeyError < StandardError; end
|
|
|
|
class ColumnNotFoundError < StandardError; end
|
|
|
|
class LogoNotFoundError < StandardError; end
|
|
|
|
MAX_PRINTABLE_ITEM_NAME_LENGTH = 64
|
|
|
|
def initialize(label_template, repository_row, print_mode=false)
|
|
@label_template = label_template
|
|
@repository_row = repository_row
|
|
@print_mode = print_mode
|
|
@repository_columns = RepositoryColumn.where(repository_id: @repository_row.repository_id).pluck(:name)
|
|
end
|
|
|
|
def render
|
|
keys = @label_template.content.scan(/(?<=\{\{).*?(?=\}\})/).uniq
|
|
|
|
keys.reduce(@label_template.content.dup) do |rendered_content, key|
|
|
rendered_content.gsub!(/\{\{#{key}\}\}/, fetch_value(key))
|
|
rescue ColumnNotFoundError, LogoNotFoundError => e
|
|
raise e unless @print_mode
|
|
|
|
rendered_content
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def fetch_value(key)
|
|
case key
|
|
when /^c_(.*)/
|
|
name = Regexp.last_match(1)
|
|
unless @repository_columns.include?(name)
|
|
raise ColumnNotFoundError, I18n.t('label_templates.repository_row.errors.column_not_found')
|
|
end
|
|
|
|
return '' unless @print_mode
|
|
|
|
repository_cell = @repository_row.repository_cells.joins(:repository_column).find_by(
|
|
repository_columns: { name: name }
|
|
)
|
|
repository_cell ? repository_cell.value.formatted : ''
|
|
when 'ITEM_ID'
|
|
@repository_row.code
|
|
when 'NAME'
|
|
@repository_row.name.truncate(MAX_PRINTABLE_ITEM_NAME_LENGTH)
|
|
when 'ADDED_BY'
|
|
@repository_row.created_by.full_name
|
|
when 'ADDED_ON'
|
|
@repository_row.created_at.to_s
|
|
when 'LOGO'
|
|
logo
|
|
else
|
|
raise ColumnNotFoundError, I18n.t('label_templates.repository_row.errors.column_not_found')
|
|
end
|
|
end
|
|
|
|
def logo
|
|
raise LogoNotFoundError, I18n.t('label_templates.repository_row.errors.logo_not_supported')
|
|
end
|
|
end
|
|
end
|