From e1e9377b214f07d9feb201ec0fa906f89e49388a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sun, 11 Aug 2024 10:15:35 +0800 Subject: [PATCH] Hide empty table entry. --- .../admin/universal_tables_controller.rb | 2 ++ .../universal_tables_controller.rb | 8 +++--- app/models/column_entry.rb | 27 +++++++++++++++++++ app/models/table_entry.rb | 25 +++++++++++++++-- config/routes.rb | 13 +++++++++ 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index 44cf0c1..ab85b43 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -171,6 +171,7 @@ class Admin::UniversalTablesController < OrbitAdminController def add_entry entry = TableEntry.new(table_entry_params) entry.save + entry.fix_have_data table = UTable.find(params[:table_entry][:u_table_id]) redirect_to admin_universal_table_new_entry_path(table) end @@ -195,6 +196,7 @@ class Admin::UniversalTablesController < OrbitAdminController def update_entry entry = TableEntry.find(params[:id]) entry.update_attributes(table_entry_params) + entry.fix_have_data # when new column insert table = entry.u_table redirect_to admin_universal_table_path(table) end diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 5b953be..5f8dd49 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -243,9 +243,9 @@ class UniversalTablesController < ApplicationController end end 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 - entries = TableEntry.where(:id.in=> table_entry_ids) + entries = TableEntry.where(:id.in=> table_entry_ids, "have_data.#{I18n.locale}" => {"$in" => [nil, true]}) end entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated) if paginated @@ -253,9 +253,9 @@ class UniversalTablesController < ApplicationController end else 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 - 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 entries diff --git a/app/models/column_entry.rb b/app/models/column_entry.rb index 704c7db..6631164 100644 --- a/app/models/column_entry.rb +++ b/app/models/column_entry.rb @@ -40,6 +40,33 @@ class ColumnEntry 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) text = "" case self.type diff --git a/app/models/table_entry.rb b/app/models/table_entry.rb index d72c106..f56d4dc 100644 --- a/app/models/table_entry.rb +++ b/app/models/table_entry.rb @@ -4,6 +4,7 @@ class TableEntry include Slug attr_accessor :sort_value + field :have_data, type: Boolean, localize: true field :sort_number, type: Integer field :view_count, type: Integer, default: 0 @@ -11,12 +12,32 @@ class TableEntry belongs_to :u_table, index: 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 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 - self.sort_number = sort_number_to_set + 1 + other_record = self.class.where(:u_table_id=> self.u_table_id, :id.ne=> self.id).order_by(sort_number: :desc).first + sort_number_to_set = other_record ? other_record.sort_number : 0 + self.sort_number = sort_number_to_set.to_i + 1 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 def self.u_table diff --git a/config/routes.rb b/config/routes.rb index eb58870..e00c875 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,18 @@ 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 scope "(:locale)", locale: Regexp.new(locales.join("|")) do