From 804c99fce66998ddcfd4970ff2c63141915bafd1 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Tue, 1 Jul 2025 21:11:07 +0200 Subject: searches: Add rich text description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows for adding rich content to searches so it can be displayed in shared searches, for example. Signed-off-by: Miquel Sabaté Solà --- app/controllers/searches_controller.rb | 2 +- app/controllers/shared_searches_controller.rb | 1 + app/javascript/controllers/search_controller.js | 2 + app/models/search.rb | 2 + app/views/searches/_form.html.erb | 6 ++ app/views/shared_searches/show.html.erb | 71 +++++++++++++--------- .../20250701132737_add_description_to_search.rb | 5 ++ db/schema.rb | 3 +- 8 files changed, 60 insertions(+), 32 deletions(-) create mode 100644 db/migrate/20250701132737_add_description_to_search.rb diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 752ccec..376455c 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -55,6 +55,6 @@ class SearchesController < ApplicationController end def search_params - params.require(:search).permit(:name, :body, :shared) + params.require(:search).permit(:name, :body, :description, :shared) end end diff --git a/app/controllers/shared_searches_controller.rb b/app/controllers/shared_searches_controller.rb index dd33bc5..ca65539 100644 --- a/app/controllers/shared_searches_controller.rb +++ b/app/controllers/shared_searches_controller.rb @@ -10,5 +10,6 @@ class SharedSearchesController < ApplicationController def show @search = Search.where(shared: true).find(params[:search_id]) + @results = @search.results.values.flatten end end diff --git a/app/javascript/controllers/search_controller.js b/app/javascript/controllers/search_controller.js index b35593a..63df81d 100644 --- a/app/javascript/controllers/search_controller.js +++ b/app/javascript/controllers/search_controller.js @@ -30,7 +30,9 @@ export default class extends Controller { document.getElementById("search-form-submit").classList.toggle("hidden"); document.getElementById("save-search").classList.toggle("hidden"); document.getElementById("search-form-name").classList.toggle("hidden"); + document.getElementById("search-form-description").classList.toggle("hidden"); document.getElementById("search-form-shared").classList.toggle("hidden"); + document.getElementById("search-body-label").classList.toggle("hidden"); document.getElementById("search_name").focus(); } } diff --git a/app/models/search.rb b/app/models/search.rb index e1c3a9b..9886f36 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -6,6 +6,8 @@ class Search < ApplicationRecord belongs_to :user + has_rich_text :description + # Returns all the results that can be fetched with the current `body`. It's # returned into a hash which groups the taggable types. def results diff --git a/app/views/searches/_form.html.erb b/app/views/searches/_form.html.erb index 15b5735..e3a326f 100644 --- a/app/views/searches/_form.html.erb +++ b/app/views/searches/_form.html.erb @@ -8,7 +8,13 @@ <%= form.text_field :name, required: true, autocomplete: 'off', autocapitalize: 'on' %> + +
+ <%= form.label :body, id: 'search-body-label', class: 'hidden' %> <%= form.text_field :body, "data-action": "keydown.enter->search#load", "data-search-target": "body", placeholder: 'Search body' %>
diff --git a/app/views/shared_searches/show.html.erb b/app/views/shared_searches/show.html.erb index d981cde..ed605bd 100644 --- a/app/views/shared_searches/show.html.erb +++ b/app/views/shared_searches/show.html.erb @@ -1,30 +1,41 @@ - - <% @search.results.values.flatten.each do |res| %> - - - - <% end %> -
-
- <% if res.is_a? Thing %> - <%= res.title %> -
-
<%= I18n.t('activerecord.attributes.thing.authors') %>: <%= authors_or_editors(res) %>
-
<%= I18n.t('activerecord.attributes.thing.publisher') %>: <%= string_maybe(res.publisher) %>
-
<%= I18n.t('activerecord.attributes.thing.year') %>: <%= string_maybe(res.year) %>
-
<%= I18n.t('activerecord.attributes.thing.kind') %>: <%= I18n.t("things.kind.#{res.kind}") %>
- <% if res.note.present? %> -
<%= I18n.t('activerecord.attributes.thing.note') %>: <%= res.note %>
- <% end %> - <% unless res.url.blank? %> - - <% end %> -
- <% else %> - <%= I18n.t('comments.title') %> <%= I18n.t('general.in') %> «<%= res.thing.title %>» -
- <%= res.content %> -
- <% end %> -
-
+<% unless @search.description.blank? %> +
+ <%= @search.description %> +
+
+<% end %> + +<% if @results.empty? %> +

<%= I18n.t('searches.none') %>.

+<% else %> + + <% @results.each do |res| %> + + + + <% end %> +
+
+ <% if res.is_a? Thing %> + <%= res.title %> +
+
<%= I18n.t('activerecord.attributes.thing.authors') %>: <%= authors_or_editors(res) %>
+
<%= I18n.t('activerecord.attributes.thing.publisher') %>: <%= string_maybe(res.publisher) %>
+
<%= I18n.t('activerecord.attributes.thing.year') %>: <%= string_maybe(res.year) %>
+
<%= I18n.t('activerecord.attributes.thing.kind') %>: <%= I18n.t("things.kind.#{res.kind}") %>
+ <% if res.note.present? %> +
<%= I18n.t('activerecord.attributes.thing.note') %>: <%= res.note %>
+ <% end %> + <% unless res.url.blank? %> + + <% end %> +
+ <% else %> + <%= I18n.t('comments.title') %> <%= I18n.t('general.in') %> «<%= res.thing.title %>» +
+ <%= res.content %> +
+ <% end %> +
+
+<% end %> diff --git a/db/migrate/20250701132737_add_description_to_search.rb b/db/migrate/20250701132737_add_description_to_search.rb new file mode 100644 index 0000000..25a89b6 --- /dev/null +++ b/db/migrate/20250701132737_add_description_to_search.rb @@ -0,0 +1,5 @@ +class AddDescriptionToSearch < ActiveRecord::Migration[8.0] + def change + add_column :searches, :description, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 32aa6c5..90b93b2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_11_08_085227) do +ActiveRecord::Schema[8.0].define(version: 2025_07_01_132737) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -64,6 +64,7 @@ ActiveRecord::Schema[8.0].define(version: 2024_11_08_085227) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "shared", default: false + t.text "description" t.index ["body"], name: "index_searches_on_body", unique: true t.index ["name"], name: "index_searches_on_name", unique: true t.index ["user_id"], name: "index_searches_on_user_id" -- cgit v1.2.3