From 4ed4853c90bccd7ddbc6534e8b0093825192ae49 Mon Sep 17 00:00:00 2001 From: Sascha Karnatz <68833+kulturbande@users.noreply.github.com> Date: Mon, 23 Jan 2023 11:58:35 +0100 Subject: [PATCH] Add searchable field to page Allow to toggle searchable field on page level. The field is only available if it was enabled in the Alchemy - module. The information can be used by search plugins (e.g. https://github.com/AlchemyCMS/alchemy-pg_search). --- app/models/alchemy/page.rb | 1 + app/views/alchemy/admin/pages/_form.html.erb | 9 +++++++ config/locales/alchemy.en.yml | 2 ++ ...3112425_add_searchable_to_alchemy_pages.rb | 9 +++++++ lib/alchemy.rb | 13 +++++++++ ...add_searchable_to_alchemy_pages.alchemy.rb | 10 +++++++ spec/dummy/db/schema.rb | 3 ++- .../admin/page_editing_feature_spec.rb | 27 +++++++++++++++++++ 8 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230123112425_add_searchable_to_alchemy_pages.rb create mode 100644 spec/dummy/db/migrate/20230123105660_add_searchable_to_alchemy_pages.alchemy.rb diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index d276b3101c..47a4e8cbfb 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -79,6 +79,7 @@ class Page < BaseRecord :restricted, :robot_index, :robot_follow, + :searchable, :sitemap, :tag_list, :title, diff --git a/app/views/alchemy/admin/pages/_form.html.erb b/app/views/alchemy/admin/pages/_form.html.erb index f7601db285..1e38809448 100644 --- a/app/views/alchemy/admin/pages/_form.html.erb +++ b/app/views/alchemy/admin/pages/_form.html.erb @@ -19,6 +19,15 @@ <%= f.input :title, input_html: {'data-alchemy-char-counter' => 60} %> + <% if Alchemy.enable_searchable %> +
+ +
+ <%= page_status_checkbox(@page, :searchable) %> +
+
+ <% end %> +
diff --git a/config/locales/alchemy.en.yml b/config/locales/alchemy.en.yml index 130391e826..4c790ab6bd 100644 --- a/config/locales/alchemy.en.yml +++ b/config/locales/alchemy.en.yml @@ -582,6 +582,7 @@ en: saved_link: "Link saved." search: "search" search_engines: "Search engines" + fulltext_search: "Fulltext search" select_element: "Select element" seperate_tags_with_comma: "Seperate tags with comma" show_element_content: "Show content of this element." @@ -865,6 +866,7 @@ en: page_layout: "Page type" public: "public" restricted: "restricted" + searchable: "show in search" robot_follow: "robot may follow links" robot_index: "allow robot to index" sitemap: "visible in sitemap" diff --git a/db/migrate/20230123112425_add_searchable_to_alchemy_pages.rb b/db/migrate/20230123112425_add_searchable_to_alchemy_pages.rb new file mode 100644 index 0000000000..1a2665a218 --- /dev/null +++ b/db/migrate/20230123112425_add_searchable_to_alchemy_pages.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddSearchableToAlchemyPages < ActiveRecord::Migration[6.0] + def change + return if column_exists?(:alchemy_pages, :searchable) + + add_column :alchemy_pages, :searchable, :boolean, default: true, null: false + end +end diff --git a/lib/alchemy.rb b/lib/alchemy.rb index 20ddad5422..e5a477d7ce 100644 --- a/lib/alchemy.rb +++ b/lib/alchemy.rb @@ -65,4 +65,17 @@ def self.preview_sources=(sources) def self.publish_targets @_publish_targets ||= Set.new end + + # Enable full text search configuration + # + # It enables a searchable checkbox in the page form to toggle + # the searchable field. These information can used in a search + # plugin (e.g. https://github.com/AlchemyCMS/alchemy-pg_search). + # + # == Example + # + # # config/initializers/alchemy.rb + # Alchemy.enable_searchable = true + # + mattr_accessor :enable_searchable, default: false end diff --git a/spec/dummy/db/migrate/20230123105660_add_searchable_to_alchemy_pages.alchemy.rb b/spec/dummy/db/migrate/20230123105660_add_searchable_to_alchemy_pages.alchemy.rb new file mode 100644 index 0000000000..1b340384bf --- /dev/null +++ b/spec/dummy/db/migrate/20230123105660_add_searchable_to_alchemy_pages.alchemy.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# This migration comes from alchemy (originally 20230123112425) +class AddSearchableToAlchemyPages < ActiveRecord::Migration[6.0] + def change + return if column_exists?(:alchemy_pages, :searchable) + + add_column :alchemy_pages, :searchable, :boolean, default: true, null: false + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 611c3aa043..cde0fe9e9d 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_22_210804) do +ActiveRecord::Schema[7.0].define(version: 2023_01_23_105660) do create_table "alchemy_attachments", force: :cascade do |t| t.string "name" t.string "file_name" @@ -172,6 +172,7 @@ t.datetime "legacy_public_on", precision: nil t.datetime "legacy_public_until", precision: nil t.datetime "locked_at", precision: nil + t.boolean "searchable", default: true, null: false t.index ["creator_id"], name: "index_alchemy_pages_on_creator_id" t.index ["language_id"], name: "index_alchemy_pages_on_language_id" t.index ["locked_at", "locked_by"], name: "index_alchemy_pages_on_locked_at_and_locked_by" diff --git a/spec/features/admin/page_editing_feature_spec.rb b/spec/features/admin/page_editing_feature_spec.rb index c8c9f3e1a4..65807c23be 100644 --- a/spec/features/admin/page_editing_feature_spec.rb +++ b/spec/features/admin/page_editing_feature_spec.rb @@ -111,6 +111,33 @@ class FooPreviewSource < Alchemy::Admin::PreviewUrl; end expect(page).to_not have_selector('input[type="checkbox"]#page_sitemap') end end + + context "enable_searchable" do + before do + Alchemy.enable_searchable = searchable + visit alchemy.configure_admin_page_path(a_page) + end + + # reset default value + after { Alchemy.enable_searchable = false } + + context "is enabled" do + let(:searchable) { true } + + it "should show searchable checkbox" do + expect(page).to have_selector('input[type="checkbox"]#page_searchable') + end + end + + context "is disabled" do + let(:searchable) { false } + + it "should not show searchable checkbox" do + visit alchemy.configure_admin_page_path(a_page) + expect(page).to_not have_selector('input[type="checkbox"]#page_searchable') + end + end + end end context "when editing a global page" do