From: Toshi MARUYAMA Date: Thu, 6 Feb 2014 03:33:33 +0000 (+0000) Subject: Rails4: add ApplicationHelper#truncate_single_line_raw method replacing truncate_sing... X-Git-Tag: 2.5.0~70 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ae7a4b9678dedbdfae0c62d9cc84667ef7388866;p=redmine.git Rails4: add ApplicationHelper#truncate_single_line_raw method replacing truncate_single_line git-svn-id: http://svn.redmine.org/redmine/trunk@12830 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54852abf5..e2e3349ed 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -224,7 +224,7 @@ module ApplicationHelper end def format_activity_title(text) - h(truncate_single_line(text, :length => 100)) + h(truncate_single_line_raw(text, 100)) end def format_activity_day(date) @@ -397,9 +397,17 @@ module ApplicationHelper # Truncates and returns the string as a single line def truncate_single_line(string, *args) + ActiveSupport::Deprecation.warn( + "ApplicationHelper#truncate_single_line is deprecated and will be removed in Rails 4 poring") + # Rails 4 ActionView::Helpers::TextHelper#truncate escapes. + # So, result is broken. truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ') end + def truncate_single_line_raw(string, length) + string.truncate(length).gsub(%r{[\r\n]+}m, ' ') + end + # Truncates at line break after 250 characters or options[:length] def truncate_lines(string, options={}) length = options[:length] || 250 @@ -759,7 +767,7 @@ module ApplicationHelper :repository_id => repository.identifier_param, :rev => changeset.revision}, :class => 'changeset', - :title => truncate_single_line(changeset.comments, :length => 100)) + :title => truncate_single_line_raw(changeset.comments, 100)) end end elsif sep == '#' @@ -841,7 +849,7 @@ module ApplicationHelper if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first) link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier}, :class => 'changeset', - :title => truncate_single_line(changeset.comments, :length => 100) + :title => truncate_single_line_raw(changeset.comments, 100) end else if repository && User.current.allowed_to?(:browse_repository, project) diff --git a/app/views/common/feed.atom.builder b/app/views/common/feed.atom.builder index b4c830ee3..e279859b1 100644 --- a/app/views/common/feed.atom.builder +++ b/app/views/common/feed.atom.builder @@ -1,6 +1,6 @@ xml.instruct! xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do - xml.title truncate_single_line(@title, :length => 100) + xml.title truncate_single_line_raw(@title, 100) xml.link "rel" => "self", "href" => url_for(params.merge(:only_path => false)) xml.link "rel" => "alternate", "href" => url_for(params.merge(:only_path => false, :format => nil, :key => nil)) xml.id url_for(:controller => 'welcome', :only_path => false) @@ -12,9 +12,9 @@ xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do xml.entry do url = url_for(item.event_url(:only_path => false)) if @project - xml.title truncate_single_line(item.event_title, :length => 100) + xml.title truncate_single_line_raw(item.event_title, 100) else - xml.title truncate_single_line("#{item.project} - #{item.event_title}", :length => 100) + xml.title truncate_single_line_raw("#{item.project} - #{item.event_title}", 100) end xml.link "rel" => "alternate", "href" => url xml.id url diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 596bfd7fb..44cdf1693 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -1388,10 +1388,10 @@ RAW def test_truncate_single_line str = "01234" - result = truncate_single_line("#{str}\n#{str}", :length => 10) + result = truncate_single_line_raw("#{str}\n#{str}", 10) assert_equal "01234 0...", result assert !result.html_safe? - result = truncate_single_line("#{str}<&#>\n#{str}#{str}", :length => 16) + result = truncate_single_line_raw("#{str}<&#>\n#{str}#{str}", 16) assert_equal "01234<&#> 012...", result assert !result.html_safe? end @@ -1399,7 +1399,7 @@ RAW def test_truncate_single_line_non_ascii ja = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding) - result = truncate_single_line("#{ja}\n#{ja}\n#{ja}", :length => 10) + result = truncate_single_line_raw("#{ja}\n#{ja}\n#{ja}", 10) assert_equal "#{ja} #{ja}...", result assert !result.html_safe? end