Hide empty table entry.

This commit is contained in:
邱博亞 2024-08-11 10:15:35 +08:00
parent 99d6d9a849
commit e1e9377b21
5 changed files with 69 additions and 6 deletions

View File

@ -171,6 +171,7 @@ class Admin::UniversalTablesController < OrbitAdminController
def add_entry def add_entry
entry = TableEntry.new(table_entry_params) entry = TableEntry.new(table_entry_params)
entry.save entry.save
entry.fix_have_data
table = UTable.find(params[:table_entry][:u_table_id]) table = UTable.find(params[:table_entry][:u_table_id])
redirect_to admin_universal_table_new_entry_path(table) redirect_to admin_universal_table_new_entry_path(table)
end end
@ -195,6 +196,7 @@ class Admin::UniversalTablesController < OrbitAdminController
def update_entry def update_entry
entry = TableEntry.find(params[:id]) entry = TableEntry.find(params[:id])
entry.update_attributes(table_entry_params) entry.update_attributes(table_entry_params)
entry.fix_have_data # when new column insert
table = entry.u_table table = entry.u_table
redirect_to admin_universal_table_path(table) redirect_to admin_universal_table_path(table)
end end

View File

@ -243,9 +243,9 @@ class UniversalTablesController < ApplicationController
end end
end end
if table_entry_ids.nil? if table_entry_ids.nil?
entries = TableEntry.where(:u_table_id=>table.id) entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]})
else else
entries = TableEntry.where(:id.in=> table_entry_ids) entries = TableEntry.where(:id.in=> table_entry_ids, "have_data.#{I18n.locale}" => {"$in" => [nil, true]})
end end
entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated) entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated)
if paginated if paginated
@ -253,9 +253,9 @@ class UniversalTablesController < ApplicationController
end end
else else
if paginated if paginated
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,page_num: params["page_no"],per: OrbitHelper.page_data_count) entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]}).sorting(params: params,table: table,page_num: params["page_no"],per: OrbitHelper.page_data_count)
else else
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,paginated: false) entries = TableEntry.where(:u_table_id=>table.id, "have_data.#{I18n.locale}" => {"$in" => [nil, true]}).sorting(params: params,table: table,paginated: false)
end end
end end
entries entries

View File

@ -40,6 +40,33 @@ class ColumnEntry
end end
end end
def have_data(locale)
flag = nil
case self.type
when "text"
flag = self.text_translations[locale].present?
when "integer"
flag = true
when "editor"
flag = self.content_translations[locale].present?
when "date"
flag = self.date.present?
when "period"
flag = self.period_from.present? || self.period_to.present?
when "image"
flag = self.image.present?
when "file"
flag = false
self.column_entry_files.each do |entry_file|
next unless entry_file.choose_lang_display(locale) && entry_file.file.present?
flag = true
end
else
flag = true
end
flag
end
def get_frontend_text(column) def get_frontend_text(column)
text = "" text = ""
case self.type case self.type

View File

@ -4,6 +4,7 @@ class TableEntry
include Slug include Slug
attr_accessor :sort_value attr_accessor :sort_value
field :have_data, type: Boolean, localize: true
field :sort_number, type: Integer field :sort_number, type: Integer
field :view_count, type: Integer, default: 0 field :view_count, type: Integer, default: 0
@ -12,11 +13,31 @@ class TableEntry
accepts_nested_attributes_for :column_entries, :allow_destroy => true accepts_nested_attributes_for :column_entries, :allow_destroy => true
I18n.available_locales.each do |locale|
index({have_data: 1}, { collation: {locale: locale.to_s}, unique: false, background: true })
end
before_save do before_save do
if self[:sort_number].nil? if self[:sort_number].nil?
sort_number_to_set = self.class.where(u_table_id: self.u_table_id).order_by(sort_number: :desc).first.sort_number rescue 0 other_record = self.class.where(:u_table_id=> self.u_table_id, :id.ne=> self.id).order_by(sort_number: :desc).first
self.sort_number = sort_number_to_set + 1 sort_number_to_set = other_record ? other_record.sort_number : 0
self.sort_number = sort_number_to_set.to_i + 1
end end
self.get_have_data
end
def fix_have_data
have_data_translations = self.get_have_data
self.class.where(:id=> self.id).update_all(have_data_translations.map{|l, v| ["have_data.#{l}", v]}.to_h)
end
def get_have_data
searchable_field_ids = TableColumn.filter_searchable.where(u_table_id: self.u_table_id).pluck(:id)
searchable_column_entries = self.column_entries.where(:table_column_id.in=> searchable_field_ids).to_a
self.have_data_translations = I18n.available_locales.map do |locale|
flag = searchable_column_entries.detect{|ce| ce.have_data(locale)}.present?
[locale.to_s, flag]
end.to_h
end end
def self.u_table def self.u_table

View File

@ -1,5 +1,18 @@
Rails.application.routes.draw do Rails.application.routes.draw do
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
stored_flag = "utf1"
need_update = Site.pluck(:tmp_flags).flatten.compact.exclude?(stored_flag)
if need_update
TableEntry.all.to_a.each do |te|
te.fix_have_data
end
Site.update_all("$push"=>{"tmp_flags"=> stored_flag})
end
end
end
locales = Site.first.in_use_locales rescue I18n.available_locales locales = Site.first.in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do