From 4dd10153509ef7273830fb21ea10298c6a542e92 Mon Sep 17 00:00:00 2001 From: Katsuya Hidaka Date: Thu, 19 Sep 2024 01:07:19 +0900 Subject: [PATCH 6/7] Separate the implementation for building a quote into Redmine::QuoteReply --- app/controllers/journals_controller.rb | 18 ++++------ app/controllers/messages_controller.rb | 14 ++++---- lib/redmine/quote_reply.rb | 46 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index 55f68503d..dd75b99a5 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -31,6 +31,7 @@ class JournalsController < ApplicationController helper :queries helper :attachments include QueriesHelper + include Redmine::QuoteReply::Builder def index retrieve_query @@ -65,18 +66,11 @@ class JournalsController < ApplicationController def new @journal = Journal.visible.find(params[:journal_id]) if params[:journal_id] - if @journal - user = @journal.user - text = @journal.notes - @content = "#{ll(Setting.default_language, :text_user_wrote_in, {:value => user, :link => "#note-#{params[:journal_indice]}"})}\n> " - else - user = @issue.author - text = @issue.description - @content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " - end - # Replaces pre blocks with [...] - text = params[:quote].presence || text.to_s.strip.gsub(%r{
(.*?)
}m, '[...]') - @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" + @content = if @journal + quote_issue_journal(@journal, indice: params[:journal_indice], partial_quote: params[:quote]) + else + quote_issue(@issue, partial_quote: params[:quote]) + end rescue ActiveRecord::RecordNotFound render_404 end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 55caa0f43..5159bf540 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -29,6 +29,7 @@ class MessagesController < ApplicationController helper :watchers helper :attachments include AttachmentsHelper + include Redmine::QuoteReply::Builder REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) @@ -119,14 +120,11 @@ class MessagesController < ApplicationController @subject = @message.subject @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:') - if @message.root == @message - @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> " - else - @content = "#{ll(Setting.default_language, :text_user_wrote_in, {:value => @message.author, :link => "message##{@message.id}"})}\n> " - end - - quote_text = params[:quote].presence || @message.content.to_s.strip.gsub(%r{
(.*?)
}m, '[...]') - @content << quote_text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" + @content = if @message.root == @message + quote_root_message(@message, partial_quote: params[:quote]) + else + quote_message(@message, partial_quote: params[:quote]) + end respond_to do |format| format.html { render_404 } diff --git a/lib/redmine/quote_reply.rb b/lib/redmine/quote_reply.rb index 18a13eb6f..1f0ba20f7 100644 --- a/lib/redmine/quote_reply.rb +++ b/lib/redmine/quote_reply.rb @@ -37,5 +37,51 @@ module Redmine ) end end + + module Builder + def quote_issue(issue, partial_quote: nil) + user = issue.author + + build_quote( + "#{ll(Setting.default_language, :text_user_wrote, user)}\n> ", + issue.description, + partial_quote + ) + end + + def quote_issue_journal(journal, indice:, partial_quote: nil) + user = journal.user + + build_quote( + "#{ll(Setting.default_language, :text_user_wrote_in, {value: journal.user, link: "#note-#{indice}"})}\n> ", + journal.notes, + partial_quote + ) + end + + def quote_root_message(message, partial_quote: nil) + build_quote( + "#{ll(Setting.default_language, :text_user_wrote, message.author)}\n> ", + message.content, + partial_quote + ) + end + + def quote_message(message, partial_quote: nil) + build_quote( + "#{ll(Setting.default_language, :text_user_wrote_in, {value: message.author, link: "message##{message.id}"})}\n> ", + message.content, + partial_quote + ) + end + + private + + def build_quote(quote_header, text, partial_quote = nil) + quote_text = partial_quote.presence || text.to_s.strip.gsub(%r{
(.*?)
}m, '[...]') + + "#{quote_header}#{quote_text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"}" + end + end end end -- 2.44.0