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
This commit is contained in:
Toshi MARUYAMA 2014-02-06 03:33:33 +00:00
parent e4d152e84a
commit ae7a4b9678
3 changed files with 17 additions and 9 deletions

View File

@ -224,7 +224,7 @@ module ApplicationHelper
end end
def format_activity_title(text) def format_activity_title(text)
h(truncate_single_line(text, :length => 100)) h(truncate_single_line_raw(text, 100))
end end
def format_activity_day(date) def format_activity_day(date)
@ -397,9 +397,17 @@ module ApplicationHelper
# Truncates and returns the string as a single line # Truncates and returns the string as a single line
def truncate_single_line(string, *args) 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, ' ') truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
end 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] # Truncates at line break after 250 characters or options[:length]
def truncate_lines(string, options={}) def truncate_lines(string, options={})
length = options[:length] || 250 length = options[:length] || 250
@ -759,7 +767,7 @@ module ApplicationHelper
:repository_id => repository.identifier_param, :repository_id => repository.identifier_param,
:rev => changeset.revision}, :rev => changeset.revision},
:class => 'changeset', :class => 'changeset',
:title => truncate_single_line(changeset.comments, :length => 100)) :title => truncate_single_line_raw(changeset.comments, 100))
end end
end end
elsif sep == '#' elsif sep == '#'
@ -841,7 +849,7 @@ module ApplicationHelper
if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first) 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}, 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', :class => 'changeset',
:title => truncate_single_line(changeset.comments, :length => 100) :title => truncate_single_line_raw(changeset.comments, 100)
end end
else else
if repository && User.current.allowed_to?(:browse_repository, project) if repository && User.current.allowed_to?(:browse_repository, project)

View File

@ -1,6 +1,6 @@
xml.instruct! xml.instruct!
xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do 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" => "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.link "rel" => "alternate", "href" => url_for(params.merge(:only_path => false, :format => nil, :key => nil))
xml.id url_for(:controller => 'welcome', :only_path => false) 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 xml.entry do
url = url_for(item.event_url(:only_path => false)) url = url_for(item.event_url(:only_path => false))
if @project if @project
xml.title truncate_single_line(item.event_title, :length => 100) xml.title truncate_single_line_raw(item.event_title, 100)
else 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 end
xml.link "rel" => "alternate", "href" => url xml.link "rel" => "alternate", "href" => url
xml.id url xml.id url

View File

@ -1388,10 +1388,10 @@ RAW
def test_truncate_single_line def test_truncate_single_line
str = "01234" 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_equal "01234 0...", result
assert !result.html_safe? 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_equal "01234<&#> 012...", result
assert !result.html_safe? assert !result.html_safe?
end end
@ -1399,7 +1399,7 @@ RAW
def test_truncate_single_line_non_ascii def test_truncate_single_line_non_ascii
ja = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" ja = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding) 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_equal "#{ja} #{ja}...", result
assert !result.html_safe? assert !result.html_safe?
end end