Support search on markdown fields

This commit is contained in:
Jose Farias
2024-11-29 14:28:37 -06:00
parent d556e21287
commit 45e94e4484
9 changed files with 31 additions and 20 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ class Comment < ApplicationRecord
belongs_to :creator, class_name: "User", default: -> { Current.user }
searchable_by :body, using: :comments_search_index
searchable_by :body_plain_text, using: :comments_search_index, as: :body
has_markdown :body
end
+6 -5
View File
@@ -2,25 +2,26 @@ module Searchable
extend ActiveSupport::Concern
class_methods do
def searchable_by(field, using:)
define_method :search_field do field; end
def searchable_by(field, using:, as: field)
define_method :search_value do send(field); end
define_method :search_field do as; end
define_method :search_table do using; end
after_create_commit :create_in_search_index
after_update_commit :update_in_search_index
after_destroy_commit :remove_from_search_index
scope :search, ->(query) { joins("join #{using} idx on #{table_name}.id = idx.rowid").where("idx.#{field} match ?", query) }
scope :search, ->(query) { joins("join #{using} idx on #{table_name}.id = idx.rowid").where("idx.#{as} match ?", query) }
end
end
private
def create_in_search_index
execute_sql_with_binds "insert into #{search_table}(rowid, #{search_field}) values (?, ?)", id, send(search_field)
execute_sql_with_binds "insert into #{search_table}(rowid, #{search_field}) values (?, ?)", id, search_value
end
def update_in_search_index
execute_sql_with_binds "update #{search_table} set #{search_field} = ? where rowid = ?", send(search_field), id
execute_sql_with_binds "update #{search_table} set #{search_field} = ? where rowid = ?", search_value, id
end
def remove_from_search_index
+1 -1
View File
@@ -13,7 +13,7 @@
<% end %>
</div>
<div class="comment__body txt-align-start">
<%= comment.body.to_html %>
<%= comment.body_html %>
</div>
</div>
<% end %>
+1 -1
View File
@@ -14,7 +14,7 @@ module Fizzy
# Include the `lib` directory in autoload paths. Use the `ignore:` option
# to list subdirectories that don't contain `.rb` files or that shouldn't
# be reloaded or eager loaded.
config.autoload_lib ignore: %w[ assets tasks ]
config.autoload_lib ignore: %w[ assets tasks rails_ext ]
# Configuration for the application, engines, and railties goes here.
#
+1 -3
View File
@@ -1,3 +1 @@
%w[ rails_ext ].each do |extensions_dir|
Dir["#{Rails.root}/lib/#{extensions_dir}/*"].each { |path| require "#{extensions_dir}/#{File.basename(path)}" }
end
Dir["#{Rails.root}/lib/rails_ext/*"].each { |path| require "rails_ext/#{File.basename(path)}" }
@@ -9,6 +9,14 @@ module ActionText
markdown_#{name} || build_markdown_#{name}
end
def #{name}_html
#{name}.to_html
end
def #{name}_plain_text
#{name}.to_plain_text
end
def #{name}?
markdown_#{name}.present?
end
+8 -2
View File
@@ -1,3 +1,5 @@
require "redcarpet/render_strip"
module ActionText
class Markdown < Record
DEFAULT_RENDERER_OPTIONS = {
@@ -14,14 +16,18 @@ module ActionText
tables: true
}
mattr_accessor :renderer, default: Redcarpet::Markdown.new(
Redcarpet::Render::HTML.new(DEFAULT_RENDERER_OPTIONS), DEFAULT_MARKDOWN_EXTENSIONS)
mattr_accessor :renderer, default: Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(DEFAULT_RENDERER_OPTIONS), DEFAULT_MARKDOWN_EXTENSIONS)
mattr_accessor :plain_renderer, default: Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
belongs_to :record, polymorphic: true, touch: true
def to_html
(renderer.try(:call) || renderer).render(content).html_safe
end
def to_plain_text
(plain_renderer.try(:call) || plain_renderer).render(content)
end
end
end
@@ -1,9 +1,7 @@
module RailsExt
module ActiveSupportArrayConversions
def to_choice_sentence
to_sentence two_words_connector: " or ", last_word_connector: ", or "
end
module ChoiceSentenceArrayConversion
def to_choice_sentence
to_sentence two_words_connector: " or ", last_word_connector: ", or "
end
end
Array.include RailsExt::ActiveSupportArrayConversions
Array.include ChoiceSentenceArrayConversion
+1 -1
View File
@@ -10,7 +10,7 @@ class BubbleTest < ActiveSupport::TestCase
bubbles(:logo).capture Comment.new(body: "Agreed.")
end
assert_equal "Agreed.", bubbles(:logo).messages.last.messageable.body
assert_equal "Agreed.\n", bubbles(:logo).messages.last.messageable.body.to_plain_text
end
test "boosting" do