Skip to content

Commit

Permalink
Deprecated warnings for :skip_last_command and :connector of to_sente…
Browse files Browse the repository at this point in the history
…nce [#1847 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
guillermo authored and dhh committed Feb 22, 2009
1 parent b61cad6 commit ff1afbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 19 additions & 5 deletions activesupport/lib/active_support/core_ext/array/conversions.rb
Expand Up @@ -6,14 +6,28 @@ module Conversions
# * <tt>:words_connector</tt> - The sign or word used to join the elements in arrays with two or more elements (default: ", ")
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
def to_sentence(options = {})
options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)

def to_sentence(options = {})

default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])

# Try to emulate to_senteces previous to 2.3
if options.has_key?(:connector) || options.has_key?(:skip_last_comma)
::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector
::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma

skip_last_comma = options.delete :skip_last_comma
if connector = options.delete(:connector)
options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}"
else
options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector
end
end

options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector

case length
when 0
""
Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/core_ext/array_ext_test.rb
Expand Up @@ -48,6 +48,8 @@ def test_to_param_array
end

class ArrayExtToSentenceTests < Test::Unit::TestCase
include ActiveSupport::Testing::Deprecation

def test_plain_array_to_sentence
assert_equal "", [].to_sentence
assert_equal "one", ['one'].to_sentence
Expand All @@ -56,12 +58,28 @@ def test_plain_array_to_sentence
end

def test_to_sentence_with_words_connector
assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
end

assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
end

assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
end

def test_to_sentence_with_last_word_connector
assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true)
end

assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
end

assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
Expand Down

0 comments on commit ff1afbd

Please sign in to comment.