@@ -17,10 +17,25 @@ module Titleize
1717 #
1818 # "notes on a scandal" # => "Notes on a Scandal"
1919 # "the good german" # => "The Good German"
20- def titleize ( title )
20+ #
21+ # Pass additional small words in opts[:small_words]
22+ #
23+ # titleize("coffee w cream", small_words: ['w']) # => "Coffee w Cream"
24+ #
25+ # For strings in ALL CAPS, specify acronyms to be preserved in opts[:acronyms]
26+ #
27+ # titleize("SMITH TO HEAD SEC", acronyms: ['SEC']) # => "Smith to Head SEC"
28+ #
29+ def titleize ( title , opts = { } )
2130 title = title . dup
2231 title . downcase! unless title [ /[[:lower:]]/ ] # assume all-caps need fixing
2332
33+ small_words = SMALL_WORDS + ( opts [ :small_words ] || [ ] )
34+ small_words = small_words + small_words . map { |small | small . capitalize }
35+
36+ acronyms = opts [ :acronyms ] || [ ]
37+ acronyms = acronyms + acronyms . map { |acronym | acronym . downcase }
38+
2439 phrases ( title ) . map do |phrase |
2540 words = phrase . split
2641 words . map . with_index do |word , index |
@@ -40,12 +55,14 @@ def word.capitalize
4055 word
4156 when /^[[:digit:]]/ # first character is a number
4257 word
43- when *( SMALL_WORDS + SMALL_WORDS . map { | small | small . capitalize } )
58+ when *small_words
4459 if index == 0 || index == words . size - 1
4560 word . capitalize
4661 else
4762 word . downcase
4863 end
64+ when *acronyms
65+ word . upcase
4966 else
5067 word . capitalize
5168 end
@@ -87,17 +104,26 @@ class String
87104 #
88105 # "notes on a scandal" # => "Notes on a Scandal"
89106 # "the good german" # => "The Good German"
107+ #
108+ # Pass additional small words in opts[:small_words]
109+ #
110+ # titleize("coffee w cream", small_words: ['w']) # => "Coffee w Cream"
111+ #
112+ # For strings in ALL CAPS, specify acronyms to be preserved in opts[:acronyms]
113+ #
114+ # titleize("SMITH TO HEAD SEC", acronyms: ['SEC']) # => "Smith to Head SEC"
115+ #
90116 def titleize ( opts = { } )
91117 if defined? ActiveSupport ::Inflector
92118 ActiveSupport ::Inflector . titleize ( self , opts )
93119 else
94- Titleize . titleize ( self )
120+ Titleize . titleize ( self , opts )
95121 end
96122 end
97123 alias_method :titlecase , :titleize
98124
99- def titleize!
100- replace ( titleize )
125+ def titleize! ( opts = { } )
126+ replace ( titleize ( opts ) )
101127 end
102128 alias_method :titlecase! , :titleize!
103129end
@@ -114,19 +140,37 @@ module ActiveSupport::Inflector
114140 # This replaces the default Rails titleize. Like the default, it uses
115141 # Inflector.underscore and Inflector.humanize to convert
116142 # underscored_names and CamelCaseNames to a more human form. However, you can change
117- # this behavior by passing :humanize => false or :underscore => false as options.
143+ # this behavior by passing :humanize => false or :underscore => false as options.
118144 # This can be useful when dealing with words like "iPod" and "GPS".
119145 #
120146 # titleize is also aliased as titlecase.
121147 #
122148 # "notes on an active_record" # => "Notes on an Active Record"
123149 # "the GoodGerman" # => "The Good German"
150+ #
151+ # Pass additional small words in opts[:small_words]
152+ #
153+ # titleize("coffee w cream", small_words: ['w']) # => "Coffee w Cream"
154+ #
155+ # For strings in ALL CAPS, acronyms specified in
156+ # ActiveSupport::Inflector.inflections.acronyms will be properly
157+ # capitalized. To override the set of acronyms used, pass in
158+ # opts[:acronyms]
159+ #
160+ # titleize("SMITH TO HEAD SEC", acronyms: ['SEC']) # => "Smith to Head SEC"
161+ #
124162 def titleize ( title , opts = { } )
125163 opts = { :humanize => true , :underscore => true } . merge ( opts )
126164 title = ActiveSupport ::Inflector . underscore ( title ) if opts [ :underscore ]
127165 title = ActiveSupport ::Inflector . humanize ( title ) if opts [ :humanize ]
128166
129- Titleize . titleize ( title )
167+ # prioritize passed-in acronyms, fall back to those configured
168+ # for ActiveSupport::Inflector
169+ opts [ :acronyms ] ||= ActiveSupport ::Inflector . inflections . acronyms
170+
171+ passthru_opts = opts . select { |k , _ | [ :acronyms , :small_words ] . include? ( k ) }
172+
173+ Titleize . titleize ( title , passthru_opts )
130174 end
131175 alias_method :titlecase , :titleize
132176 end
0 commit comments