Ticket #6989: humanize_inflections_rev3.diff
| File humanize_inflections_rev3.diff, 4.1 kB (added by josh, 2 years ago) |
|---|
-
activesupport/test/inflector_test.rb
old new 306 306 end 307 307 end 308 308 309 def test_humanize_by_rule 310 Inflector.inflections do |inflect| 311 inflect.human(/_cnt$/i, '\1_count') 312 inflect.human(/^prefx_/i, '\1') 313 end 314 assert_equal("Jargon count", Inflector.humanize("jargon_cnt")) 315 assert_equal("Request", Inflector.humanize("prefx_request")) 316 end 317 318 def test_humanize_by_string 319 Inflector.inflections do |inflect| 320 inflect.human("col_rpted_bugs" => "Reported bugs") 321 end 322 assert_equal("Reported bugs", Inflector.humanize("col_rpted_bugs")) 323 assert_equal("Col rpted bugs", Inflector.humanize("COL_rpted_bugs")) 324 end 325 326 def test_clear_humans 327 Inflector.inflections do |inflect| 328 inflect.human("test","clear") 329 end 330 assert_equal("clear", "test".humanize) 331 Inflector.inflections.clear(:humans) 332 assert_equal("Test","test".humanize) 333 end 334 309 335 def test_constantize 310 336 assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") } 311 337 assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") } -
activesupport/lib/active_support/inflector.rb
old new 22 22 class Inflections 23 23 include Singleton 24 24 25 attr_reader :plurals, :singulars, :uncountables 25 attr_reader :plurals, :singulars, :uncountables, :humans, :human_strings 26 26 27 27 def initialize 28 @plurals, @singulars, @uncountables = [], [], []28 @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 29 29 end 30 30 31 31 # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. … … 61 61 (@uncountables << words).flatten! 62 62 end 63 63 64 # Specifies a humanized form of a string by a regular expression rule or by a string mapping. 65 # When using a regular expression based replacement, the normal humanize formatting is called after the replacement. 66 # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name') 67 # 68 # Examples: 69 # human /_cnt$/i, '\1_count' 70 # human "legacy_col_person_name", "Name" 71 # human "strt_dt" => "Start date", 72 # "upd_dt" => "Updated date" 73 def human(first, last = nil) 74 case first 75 when Hash 76 @human_strings.merge!(first) 77 when String 78 @human_strings[first] = last 79 else 80 @humans.insert(0, [first, last]) 81 end 82 end 83 64 84 # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type, 65 # the options are: :plurals, :singulars, :uncountables 85 # the options are: :plurals, :singulars, :uncountables, :humans 66 86 # 67 87 # Examples: 68 88 # clear :all … … 70 90 def clear(scope = :all) 71 91 case scope 72 92 when :all 73 @plurals, @singulars, @uncountables = [], [], []93 @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 74 94 else 75 95 instance_variable_set "@#{scope}", [] 96 @human_strings = {} if scope == :humans 76 97 end 77 98 end 78 99 end … … 188 209 # "employee_salary" #=> "Employee salary" 189 210 # "author_id" #=> "Author" 190 211 def humanize(lower_case_and_underscored_word) 191 lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 212 if result = inflections.human_strings[lower_case_and_underscored_word] 213 result 214 else 215 result = lower_case_and_underscored_word.to_s.dup 216 inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } 217 result.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 218 end 192 219 end 193 220 194 221 # Removes the module part from the expression in the string