2024-09-05 22:54:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-07-20 16:52:12 +02:00
|
|
|
module RichTextTargetBlank
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
|
# Override has_rich_text to include target="_blank" functionality
|
|
|
|
|
def has_rich_text(name)
|
|
|
|
|
super # Call the original has_rich_text to set up the rich text association
|
|
|
|
|
|
|
|
|
|
# Define the before_save callback to modify the links
|
|
|
|
|
before_save do
|
|
|
|
|
rich_text_attribute = send(name)
|
|
|
|
|
if rich_text_attribute.present?
|
|
|
|
|
doc = Nokogiri::HTML::DocumentFragment.parse(rich_text_attribute.body.to_html)
|
2024-09-05 22:54:38 +02:00
|
|
|
doc.css("a").each { |a| a["target"] ||= "_blank" }
|
2024-07-20 16:52:12 +02:00
|
|
|
rich_text_attribute.body = doc.to_html
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|