Skip to content

Commit

Permalink
Add searchable field to page
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kulturbande committed Jan 23, 2023
1 parent 72194a5 commit 4ed4853
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Page < BaseRecord
:restricted,
:robot_index,
:robot_follow,
:searchable,
:sitemap,
:tag_list,
:title,
Expand Down
9 changes: 9 additions & 0 deletions app/views/alchemy/admin/pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
<%= f.input :title,
input_html: {'data-alchemy-char-counter' => 60} %>

<% if Alchemy.enable_searchable %>
<div class="input check_boxes">
<label class="control-label"><%= Alchemy.t(:fulltext_search) %></label>
<div class="control_group">
<%= page_status_checkbox(@page, :searchable) %>
</div>
</div>
<% end %>

<div class="input check_boxes">
<label class="control-label"><%= Alchemy.t(:search_engines) %></label>
<div class="control_group">
Expand Down
2 changes: 2 additions & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20230123112425_add_searchable_to_alchemy_pages.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ed4853

Please sign in to comment.