diff --git a/app/models/comment.rb b/app/models/comment.rb index dd9c07dc7..3de28081f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb index 125deb54e..b3bc64f5d 100644 --- a/app/models/concerns/searchable.rb +++ b/app/models/concerns/searchable.rb @@ -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 diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 70e69ddb9..7efab5588 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -13,7 +13,7 @@ <% end %>
- <%= comment.body.to_html %> + <%= comment.body_html %>
<% end %> diff --git a/config/application.rb b/config/application.rb index 984bfb09f..2f544b5a0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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. # diff --git a/config/initializers/extensions.rb b/config/initializers/extensions.rb index 6fa93d530..455b96d47 100644 --- a/config/initializers/extensions.rb +++ b/config/initializers/extensions.rb @@ -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)}" } diff --git a/lib/rails_ext/action_text_has_markdown.rb b/lib/rails_ext/action_text_has_markdown.rb index 0e8f255e4..b792657d5 100644 --- a/lib/rails_ext/action_text_has_markdown.rb +++ b/lib/rails_ext/action_text_has_markdown.rb @@ -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 diff --git a/lib/rails_ext/action_text_markdown.rb b/lib/rails_ext/action_text_markdown.rb index fa0a62298..41c21a893 100644 --- a/lib/rails_ext/action_text_markdown.rb +++ b/lib/rails_ext/action_text_markdown.rb @@ -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 diff --git a/lib/rails_ext/active_support_array_conversions.rb b/lib/rails_ext/active_support_array_conversions.rb index 74bda581e..1764fb115 100644 --- a/lib/rails_ext/active_support_array_conversions.rb +++ b/lib/rails_ext/active_support_array_conversions.rb @@ -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 diff --git a/test/models/bubble_test.rb b/test/models/bubble_test.rb index d32ec380c..1bc5f3da6 100644 --- a/test/models/bubble_test.rb +++ b/test/models/bubble_test.rb @@ -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