Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6989: humanize_inflections_rev3.diff

File humanize_inflections_rev3.diff, 4.1 kB (added by josh, 2 years ago)

Tidied up patch against r6216.

  • activesupport/test/inflector_test.rb

    old new  
    306306    end 
    307307  end 
    308308 
     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 
    309335  def test_constantize 
    310336    assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") } 
    311337    assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") } 
  • activesupport/lib/active_support/inflector.rb

    old new  
    2222  class Inflections 
    2323    include Singleton 
    2424 
    25     attr_reader :plurals, :singulars, :uncountables 
     25    attr_reader :plurals, :singulars, :uncountables, :humans, :human_strings 
    2626 
    2727    def initialize 
    28       @plurals, @singulars, @uncountables = [], [], [] 
     28      @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 
    2929    end 
    3030 
    3131    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. 
     
    6161      (@uncountables << words).flatten! 
    6262    end 
    6363 
     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 
    6484    # 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 
    6686    # 
    6787    # Examples: 
    6888    #   clear :all 
     
    7090    def clear(scope = :all) 
    7191      case scope 
    7292        when :all 
    73           @plurals, @singulars, @uncountables = [], [], [] 
     93          @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 
    7494        else 
    7595          instance_variable_set "@#{scope}", [] 
     96          @human_strings = {} if scope == :humans 
    7697      end 
    7798    end 
    7899  end 
     
    188209  #   "employee_salary" #=> "Employee salary" 
    189210  #   "author_id" #=> "Author" 
    190211  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 
    192219  end 
    193220 
    194221  # Removes the module part from the expression in the string