From 21e2c0819b750af15ff0d992b8c51b7501540694 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Wed, 4 Jun 2025 22:37:42 +0800 Subject: [PATCH] added related entries and stuff --- .../universal_tables_controller.rb | 48 ++++++++++---- app/models/table_entry.rb | 14 ++-- .../universal_tables/_entry_form.html.erb | 66 +++++++++++++++++-- config/locales/en.yml | 4 +- config/locales/zh_tw.yml | 4 +- modules/universal_table/show.html.erb | 12 +++- 6 files changed, 123 insertions(+), 25 deletions(-) diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 8400d4e..52ed4fd 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -155,19 +155,43 @@ class UniversalTablesController < ApplicationController "order" => ct.order } if text != "" end - sorted = rows.sort{ |k,v| k["order"] <=> v["order"] } - sorted << { + sorted = rows.sort{ |k,v| k["order"] <=> v["order"] } + sorted << { + "title" => t("universal_table.hashtags"), + "text" => entry.tags_for_frontend + } + entry.inc(view_count: 1) + related_entries = [] + entry.get_related_entries.each do |e| + rows = [] + e.column_entries.each do |ce| + ct = ce.table_column + text = ce.get_frontend_text(ct) + if ct.is_link_to_show + text = "#{text}" + end + rows << { + "title" => ct.title, + "text" => text, + "url" => OrbitHelper.url_to_show(e.to_param) + } if text != "" + end + rows << { "title" => t("universal_table.hashtags"), - "text" => entry.tags_for_frontend + "text" => e.tags_for_frontend } - entry.inc(view_count: 1) - { - "entry" => sorted, - "extras" => { - "view_count_head" => I18n.t("view_count"), - "view_count" => entry.view_count - } - } + related_entries << { + "related_entry" => rows + } + end + { + "entry" => sorted, + "related_entries" => related_entries, + "extras" => { + "view_count_head" => I18n.t("view_count"), + "view_count" => entry.view_count + } + } end def download_file @@ -334,7 +358,7 @@ class UniversalTablesController < ApplicationController if !ce.nil? text = ce.get_frontend_text(column) if column.is_link_to_show - text = "#{text}" + text = "#{text}" end cols << {"text" => text} diff --git a/app/models/table_entry.rb b/app/models/table_entry.rb index 7bb8462..f347272 100644 --- a/app/models/table_entry.rb +++ b/app/models/table_entry.rb @@ -3,10 +3,11 @@ class TableEntry include Mongoid::Timestamps 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 + attr_accessor :sort_value + field :have_data, type: Boolean, localize: true + field :sort_number, type: Integer + field :view_count, type: Integer, default: 0 + field :related_entries, type: String, default: "" has_many :column_entries, :dependent => :destroy belongs_to :u_table, index: true @@ -32,6 +33,11 @@ class TableEntry self.class.where(:id=> self.id).update_all(have_data_translations.map{|l, v| ["have_data.#{l}", v]}.to_h) end + def get_related_entries + tids = self.related_entries.split(',') + TableEntry.find(tids) + 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 diff --git a/app/views/admin/universal_tables/_entry_form.html.erb b/app/views/admin/universal_tables/_entry_form.html.erb index b3fd844..8bac7d6 100644 --- a/app/views/admin/universal_tables/_entry_form.html.erb +++ b/app/views/admin/universal_tables/_entry_form.html.erb @@ -15,6 +15,9 @@ #s2id_autogen1{ width: 500px !important; } + #s2id_autogen2 { + width: 500px !important; + }
@@ -24,6 +27,15 @@
+ <% + tbData = @entry.get_related_entries.map{|tb| {id: tb.id.to_s, text: tb.column_entries.first.text}} + %> +
+ +
+ <%= f.text_field :related_entries, :class => "select2", data: { value: tbData } %> +
+
<% @columns.each_with_index do |column, index| %> <% if @entry.new_record? %> <% object = f.object.send(:column_entries).build rescue nil %> @@ -56,11 +68,55 @@ $("#universal_table_tags").select2({ if (!data.length) return { id: term, text: "#" + term.trim().toLowerCase() }; } - // ajax: { - // url: '/api/v1.1/locations', - // dataType: 'json' - // } - }); $("#universal_table_tags").val(<%= raw(@entry.table_tags.collect { |tag| tag.id.to_s }) %>).trigger("change"); + + var select2Options = { + maximumSelectionSize: 10, + minimumResultsForSearch: Infinity, + multiple: true, + minimumInputLength: 2, + placeholder: "<%= t("universal_table.search_entries") %>", + ajax: { + type: 'get', + url: "/admin/universal_tables/get_entries?uid=<%= @entry.u_table.uid %>", + allowClear: true, + dataType: 'json', + delay: 250, + data: function (term) { + var query = { + q: term + } + return query; + }, + results: function(data, page) { + return { + results: data + }; + }, + cache: true + }, + formatResult: function(i) { + return '
' + i.text + '
'; + }, // Formats results in drop down + formatSelection: function(i) { + return '
' + i.text + '
'; + }, //Formats result that is selected + escapeMarkup: function(m) { + return m; + } // we do not want to escape markup since we are displaying html in results + } + + $("#table_entry_related_entries").select2(select2Options); + $('.select2').each(function () { + const data = $(this).data('value'); + if (data) { + console.log($(this)) + $(this).select2({ + data: data, + multiple: true, + width: '100%' + }).val(data.map(i => i.id)).trigger('change'); + } + }); \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 333490e..b869863 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,4 +30,6 @@ en: disable_editing: Disable editing enable_editing: Enable editing save_mind_map: Save mind map - hashtags: Hashtags \ No newline at end of file + hashtags: Hashtags + related_entries: Related entries + search_entries: Search entries \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index a447ef4..20bc5d2 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -30,4 +30,6 @@ zh_tw: disable_editing: Disable editing enable_editing: Enable editing save_mind_map: Save mind map - hashtags: Hashtags \ No newline at end of file + hashtags: Hashtags + related_entries: Related entries + search_entries: Search entries \ No newline at end of file diff --git a/modules/universal_table/show.html.erb b/modules/universal_table/show.html.erb index 2d690ae..211c087 100644 --- a/modules/universal_table/show.html.erb +++ b/modules/universal_table/show.html.erb @@ -14,7 +14,7 @@ - + @@ -22,4 +22,12 @@
{{view_count_head}}: {{view_count}} -
\ No newline at end of file + + +
+
+ + + + +
{{title}}{{title}} {{text}}
{{text}}