Skip to content
This repository
Browse code

Change Array#to_sentence I18n options to pass comma and space charact…

…er from outside.

[#1397 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 273c770011ab43d4b523caacb808bce070922d77 1 parent 091e6f7
Akira Matsuda authored November 18, 2008 jeremy committed December 08, 2008
19  activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -3,15 +3,16 @@ module CoreExtensions #:nodoc:
3 3
     module Array #:nodoc:
4 4
       module Conversions
5 5
         # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
6  
-        # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and")
7  
-        # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c".        
  6
+        # * <tt>:words_connector</tt> - The sign or word used to join the elements in arrays with two or more elements (default: ", ")
  7
+        # * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
  8
+        # * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
8 9
         def to_sentence(options = {})          
9  
-          options.assert_valid_keys(:connector, :skip_last_comma, :locale)
  10
+          options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
10 11
           
11  
-          default = I18n.translate(:'support.array.sentence_connector', :locale => options[:locale])
12  
-          default_skip_last_comma = I18n.translate(:'support.array.skip_last_comma', :locale => options[:locale])
13  
-          options.reverse_merge! :connector => default, :skip_last_comma => default_skip_last_comma
14  
-          options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
  12
+          default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
  13
+          default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
  14
+          default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
  15
+          options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
15 16
 
16 17
           case length
17 18
             when 0
@@ -19,9 +20,9 @@ def to_sentence(options = {})
19 20
             when 1
20 21
               self[0].to_s
21 22
             when 2
22  
-              "#{self[0]} #{options[:connector]}#{self[1]}"
  23
+              "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
23 24
             else
24  
-              "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
  25
+              "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
25 26
           end
26 27
         end
27 28
         
5  activesupport/lib/active_support/locale/en.yml
@@ -28,5 +28,6 @@ en:
28 28
 # Used in array.to_sentence.
29 29
   support:
30 30
     array:
31  
-      sentence_connector: "and"
32  
-      skip_last_comma: false
  31
+      words_connector: ", "
  32
+      two_words_connector: " and "
  33
+      last_word_connector: ", and "
19  activesupport/test/core_ext/array_ext_test.rb
@@ -55,21 +55,22 @@ def test_plain_array_to_sentence
55 55
     assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
56 56
   end
57 57
 
58  
-  def test_to_sentence_with_connector
59  
-    assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
60  
-    assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
61  
-    assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => nil)
62  
-    assert_equal "one, two,  three", ['one', 'two', 'three'].to_sentence(:connector => ' ')
63  
-    assert_equal "one, two, and  three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
  58
+  def test_to_sentence_with_words_connector
  59
+    assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
  60
+    assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
  61
+    assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
64 62
   end
65 63
 
66  
-  def test_to_sentence_with_skip_last_comma
67  
-    assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
  64
+  def test_to_sentence_with_last_word_connector
  65
+    assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
  66
+    assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
  67
+    assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
  68
+    assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ')
68 69
   end
69 70
 
70 71
   def test_two_elements
71 72
     assert_equal "one and two", ['one', 'two'].to_sentence
72  
-    assert_equal "one two", ['one', 'two'].to_sentence(:connector => '')
  73
+    assert_equal "one two", ['one', 'two'].to_sentence(:two_words_connector => ' ')
73 74
   end
74 75
 
75 76
   def test_one_element
21  activesupport/test/i18n_test.rb
@@ -71,19 +71,28 @@ def test_time_pm
71 71
     assert_equal 'pm', I18n.translate(:'time.pm')
72 72
   end
73 73
 
74  
-  def test_sentence_connector
75  
-    assert_equal 'and', I18n.translate(:'support.array.sentence_connector')
  74
+  def test_words_connector
  75
+    assert_equal ', ', I18n.translate(:'support.array.words_connector')
76 76
   end
77 77
 
78  
-  def test_skip_last_comma
79  
-    assert_equal false, I18n.translate(:'support.array.skip_last_comma')
  78
+  def test_two_words_connector
  79
+    assert_equal ' and ', I18n.translate(:'support.array.two_words_connector')
  80
+  end
  81
+
  82
+  def test_last_word_connector
  83
+    assert_equal ', and ', I18n.translate(:'support.array.last_word_connector')
80 84
   end
81 85
 
82 86
   def test_to_sentence
  87
+    default_two_words_connector = I18n.translate(:'support.array.two_words_connector')
  88
+    default_last_word_connector = I18n.translate(:'support.array.last_word_connector')
83 89
     assert_equal 'a, b, and c', %w[a b c].to_sentence
84  
-    I18n.backend.store_translations 'en', :support => { :array => { :skip_last_comma => true } }
  90
+    I18n.backend.store_translations 'en', :support => { :array => { :two_words_connector => ' & ' } }
  91
+    assert_equal 'a & b', %w[a b].to_sentence
  92
+    I18n.backend.store_translations 'en', :support => { :array => { :last_word_connector => ' and ' } }
85 93
     assert_equal 'a, b and c', %w[a b c].to_sentence
86 94
   ensure
87  
-    I18n.backend.store_translations 'en', :support => { :array => { :skip_last_comma => false } }
  95
+    I18n.backend.store_translations 'en', :support => { :array => { :two_words_connector => default_two_words_connector } }
  96
+    I18n.backend.store_translations 'en', :support => { :array => { :last_word_connector => default_last_word_connector } }
88 97
   end
89 98
 end

0 notes on commit 273c770

Please sign in to comment.
Something went wrong with that request. Please try again.