mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-04 19:53:19 +08:00
Refactor label templates backend [SCI-9676]
This commit is contained in:
parent
3211fa7cce
commit
9abdc8698a
10 changed files with 152 additions and 173 deletions
|
@ -113,4 +113,14 @@ class ApplicationController < ActionController::Base
|
|||
ensure
|
||||
I18n.backend.date_format = nil
|
||||
end
|
||||
|
||||
def pagination_dict(object)
|
||||
{
|
||||
current_page: object.current_page,
|
||||
next_page: object.next_page,
|
||||
prev_page: object.prev_page,
|
||||
total_pages: object.total_pages,
|
||||
total_count: object.total_count
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,7 +16,8 @@ class LabelTemplatesController < ApplicationController
|
|||
def index
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
render json: @label_templates, each_serializer: LabelTemplateSerializer, user: current_user
|
||||
label_templates = Lists::LabelTemplatesService.new(@label_templates, params).call
|
||||
render json: label_templates, each_serializer: Lists::LabelTemplateSerializer, user: current_user, meta: pagination_dict(label_templates)
|
||||
end
|
||||
format.html do
|
||||
unless LabelTemplate.enabled?
|
||||
|
@ -28,13 +29,6 @@ class LabelTemplatesController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def datatable
|
||||
render json: ::LabelTemplateDatatable.new(
|
||||
view_context,
|
||||
@label_templates
|
||||
)
|
||||
end
|
||||
|
||||
def show
|
||||
respond_to do |format|
|
||||
format.json { render json: @label_template, serializer: LabelTemplateSerializer, user: current_user }
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CustomDatatableV2
|
||||
attr_reader :params, :options
|
||||
|
||||
def initialize(view, raw_data, options = {})
|
||||
@raw_data = raw_data
|
||||
@params = view.params
|
||||
@options = options
|
||||
end
|
||||
|
||||
def as_json(_options = {})
|
||||
{
|
||||
data: data,
|
||||
pageTotal: records.total_pages
|
||||
}
|
||||
end
|
||||
|
||||
def records
|
||||
@records ||= fetch_records
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def order_params
|
||||
@order_params ||=
|
||||
params.require(:order).permit(:column, :dir).to_h
|
||||
end
|
||||
|
||||
def fetch_records
|
||||
records = get_raw_records
|
||||
records = sort_records(records) if params[:order].present?
|
||||
records = paginate_records(records) if params[:page].present?
|
||||
records = filter_records(records) if params[:search].present?
|
||||
records
|
||||
end
|
||||
|
||||
def paginate_records(records)
|
||||
records.page(params[:page]).per(params[:per_page])
|
||||
end
|
||||
|
||||
def sort_direction(order_params)
|
||||
order_params[:dir] == 'asc' ? 'ASC' : 'DESC'
|
||||
end
|
||||
|
||||
def sort_records(records)
|
||||
sort_by = "#{sortable_columns[order_params[:column].to_sym]} #{sort_direction(order_params)}"
|
||||
records.order(sort_by)
|
||||
end
|
||||
|
||||
def generate_sortable_displayed_columns
|
||||
@sortable_displayed_columns = []
|
||||
columns_params.each_value do |col|
|
||||
@sortable_displayed_columns << col[:data] if col[:orderable] == 'true'
|
||||
end
|
||||
@sortable_displayed_columns
|
||||
end
|
||||
|
||||
def formated_date
|
||||
f_date = I18n.backend.date_format.dup
|
||||
f_date.gsub!(/%-d/, 'FMDD')
|
||||
f_date.gsub!(/%d/, 'DD')
|
||||
f_date.gsub!(/%-m/, 'FMMM')
|
||||
f_date.gsub!(/%m/, 'MM')
|
||||
f_date.gsub!(/%b/, 'Mon')
|
||||
f_date.gsub!(/%B/, 'Month')
|
||||
f_date.gsub!('%Y', 'YYYY')
|
||||
f_date += ' HH24:MI'
|
||||
f_date
|
||||
end
|
||||
end
|
|
@ -1,87 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class LabelTemplateDatatable < CustomDatatableV2
|
||||
include InputSanitizeHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
TABLE_COLUMNS = {
|
||||
default: 'label_templates.default',
|
||||
name: 'label_templates.name',
|
||||
format: 'label_templates.type',
|
||||
description: 'label_templates.description',
|
||||
modified_by: 'label_templates.modified_by',
|
||||
updated_at: 'label_templates.updated_at',
|
||||
created_by: 'label_templates.created_by_user',
|
||||
created_at: 'label_templates.created_at'
|
||||
}.freeze
|
||||
|
||||
def sortable_columns
|
||||
@sortable_columns ||= TABLE_COLUMNS
|
||||
end
|
||||
|
||||
def searchable_columns
|
||||
@searchable_columns ||= TABLE_COLUMNS
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def order_params
|
||||
@order_params ||=
|
||||
params.require(:order).permit(:column, :dir).to_h
|
||||
end
|
||||
|
||||
def data
|
||||
records.map do |record|
|
||||
{
|
||||
id: record.id,
|
||||
default: record.default,
|
||||
name: append_format_icon(record),
|
||||
format: escape_input(record.label_format),
|
||||
description: escape_input(record.description),
|
||||
modified_by: escape_input(record.modified_by),
|
||||
updated_at: I18n.l(record.updated_at, format: :full),
|
||||
created_by: escape_input(record.created_by_user),
|
||||
created_at: I18n.l(record.created_at, format: :full),
|
||||
attributes: {
|
||||
edit_url: label_template_path(record)
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def append_format_icon(record)
|
||||
{
|
||||
icon_image_tag:
|
||||
ActionController::Base.helpers.image_tag(
|
||||
"label_template_icons/#{record.icon}.svg",
|
||||
class: 'label-template-icon'
|
||||
),
|
||||
name: escape_input(record.name)
|
||||
}
|
||||
end
|
||||
|
||||
def get_raw_records
|
||||
res = @raw_data.joins(
|
||||
'LEFT OUTER JOIN users AS creators ' \
|
||||
'ON label_templates.created_by_id = creators.id'
|
||||
).joins(
|
||||
'LEFT OUTER JOIN users AS modifiers '\
|
||||
'ON label_templates.last_modified_by_id = modifiers.id'
|
||||
).select('label_templates.* AS label_templates')
|
||||
.select('creators.full_name AS created_by_user')
|
||||
.select('modifiers.full_name AS modified_by')
|
||||
.select(
|
||||
"('#{Extends::LABEL_TEMPLATE_FORMAT_MAP.to_json}'::jsonb -> label_templates.type)::text "\
|
||||
"AS label_format"
|
||||
)
|
||||
LabelTemplate.from(res, :label_templates)
|
||||
end
|
||||
|
||||
def filter_records(records)
|
||||
records.where_attributes_like(
|
||||
['label_templates.name', 'label_templates.label_format', 'label_templates.description',
|
||||
'label_templates.modified_by', 'label_templates.created_by_user'],
|
||||
params[:search]
|
||||
)
|
||||
end
|
||||
end
|
|
@ -97,11 +97,10 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
labelNameRenderer(params) {
|
||||
let name = params.data.name;
|
||||
let editUrl = params.data.attributes.edit_url;
|
||||
let editUrl = params.data.urls.show;
|
||||
return `<a href="${editUrl}">
|
||||
${name.icon_image_tag}
|
||||
${name.name}
|
||||
${params.data.icon_url}
|
||||
${params.data.name}
|
||||
</a>`
|
||||
},
|
||||
defaultRenderer(params) {
|
||||
|
|
|
@ -158,6 +158,9 @@ export default {
|
|||
window.removeEventListener('resize', this.resize);
|
||||
},
|
||||
methods: {
|
||||
formatData(data) {
|
||||
return data.map( (item) => Object.assign({}, item.attributes, { id: item.id }) );
|
||||
},
|
||||
resize() {
|
||||
if (this.tableState) return;
|
||||
|
||||
|
@ -175,8 +178,8 @@ export default {
|
|||
})
|
||||
.then((response) => {
|
||||
this.selectedRows = [];
|
||||
this.gridApi.setRowData(response.data.data);
|
||||
this.totalPage = response.data.pageTotal;
|
||||
this.gridApi.setRowData(this.formatData(response.data.data));
|
||||
this.totalPage = response.data.meta.total_pages;
|
||||
this.$emit('tableReloaded');
|
||||
})
|
||||
},
|
||||
|
|
45
app/serializers/lists/label_template_serializer.rb
Normal file
45
app/serializers/lists/label_template_serializer.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Lists
|
||||
class LabelTemplateSerializer < ActiveModel::Serializer
|
||||
include Canaid::Helpers::PermissionsHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
attributes :name, :language_type, :urls, :type,
|
||||
:default, :format, :modified_by, :created_by,
|
||||
:created_at, :updated_at, :id, :icon_url, :description
|
||||
|
||||
def icon_url
|
||||
ActionController::Base.helpers.image_tag(
|
||||
"label_template_icons/#{object.icon}.svg",
|
||||
class: 'label-template-icon'
|
||||
)
|
||||
end
|
||||
|
||||
delegate :modified_by, to: :object
|
||||
|
||||
def format
|
||||
object.label_format
|
||||
end
|
||||
|
||||
def created_by
|
||||
object.created_by_user
|
||||
end
|
||||
|
||||
def created_at
|
||||
I18n.l(object.created_at, format: :full)
|
||||
end
|
||||
|
||||
def updated_at
|
||||
I18n.l(object.updated_at, format: :full)
|
||||
end
|
||||
|
||||
def urls
|
||||
return {} unless can_manage_label_templates?(object.team)
|
||||
|
||||
{
|
||||
show: label_template_path(object)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
39
app/services/lists/base_service.rb
Normal file
39
app/services/lists/base_service.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Lists
|
||||
class BaseService
|
||||
def initialize(raw_data, params, user: nil)
|
||||
@raw_data = raw_data
|
||||
@params = params
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
records = fetch_records
|
||||
records = filter_records(records)
|
||||
records = sort_records(records)
|
||||
paginate_records(records)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def order_params
|
||||
@order_params ||= @params.require(:order).permit(:column, :dir).to_h
|
||||
end
|
||||
|
||||
def paginate_records(records)
|
||||
records.page(@params[:page]).per(@params[:per_page])
|
||||
end
|
||||
|
||||
def sort_direction(order_params)
|
||||
order_params[:dir] == 'asc' ? 'ASC' : 'DESC'
|
||||
end
|
||||
|
||||
def sort_records(records)
|
||||
return records unless @params[:order]
|
||||
|
||||
sort_by = "#{sortable_columns[order_params[:column].to_sym]} #{sort_direction(order_params)}"
|
||||
records.order(sort_by)
|
||||
end
|
||||
end
|
||||
end
|
47
app/services/lists/label_templates_service.rb
Normal file
47
app/services/lists/label_templates_service.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Lists
|
||||
class LabelTemplatesService < BaseService
|
||||
private
|
||||
|
||||
def fetch_records
|
||||
res = @raw_data.joins(
|
||||
'LEFT OUTER JOIN users AS creators ' \
|
||||
'ON label_templates.created_by_id = creators.id'
|
||||
).joins(
|
||||
'LEFT OUTER JOIN users AS modifiers ' \
|
||||
'ON label_templates.last_modified_by_id = modifiers.id'
|
||||
).select('label_templates.* AS label_templates')
|
||||
.select('creators.full_name AS created_by_user')
|
||||
.select('modifiers.full_name AS modified_by')
|
||||
.select(
|
||||
"('#{Extends::LABEL_TEMPLATE_FORMAT_MAP.to_json}'::jsonb -> label_templates.type)::text " \
|
||||
"AS label_format"
|
||||
)
|
||||
LabelTemplate.from(res, :label_templates)
|
||||
end
|
||||
|
||||
def filter_records(records)
|
||||
return records unless @params[:search]
|
||||
|
||||
records.where_attributes_like(
|
||||
['label_templates.name', 'label_templates.label_format', 'label_templates.description',
|
||||
'label_templates.modified_by', 'label_templates.created_by_user'],
|
||||
@params[:search]
|
||||
)
|
||||
end
|
||||
|
||||
def sortable_columns
|
||||
@sortable_columns ||= {
|
||||
default: 'label_templates.default',
|
||||
name: 'label_templates.name',
|
||||
format: 'label_templates.type',
|
||||
description: 'label_templates.description',
|
||||
modified_by: 'label_templates.modified_by',
|
||||
updated_at: 'label_templates.updated_at',
|
||||
created_by: 'label_templates.created_by_user',
|
||||
created_at: 'label_templates.created_at'
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,7 +13,7 @@
|
|||
>
|
||||
<label-templates-table
|
||||
:actions-url="'<%= actions_toolbar_label_templates_url %>'"
|
||||
:data-source="'<%= datatable_label_templates_path(format: :json) %>'"
|
||||
:data-source="'<%= label_templates_path(format: :json) %>'"
|
||||
:create-url="'<%= label_templates_path if can_manage_label_templates?(current_team) %>'"
|
||||
:sync-fluics-url="'<%= sync_fluics_templates_label_templates_path if can_manage_label_templates?(current_team) %>'"
|
||||
/>
|
||||
|
|
Loading…
Reference in a new issue