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

Ticket #6989: humanize_inflections.diff

File humanize_inflections.diff, 3.5 kB (added by dcmanges, 2 years ago)
  • test/inflector_test.rb

    old new  
    289289      assert_equal(human, Inflector.humanize(underscore)) 
    290290    end 
    291291  end 
     292   
     293  def test_humanize_by_rule 
     294    Inflector.inflections do |inflect| 
     295      inflect.human(/_cnt$/i, '\1_count') 
     296      inflect.human(/^prefx_/i, '\1') 
     297    end 
     298    assert_equal("Jargon count", Inflector.humanize("jargon_cnt")) 
     299    assert_equal("Request", Inflector.humanize("prefx_request")) 
     300  end 
     301   
     302  def test_humanize_by_string 
     303    Inflector.inflections do |inflect| 
     304      inflect.human("col_rpted_bugs" => "Reported bugs") 
     305    end 
     306    assert_equal("Reported bugs", Inflector.humanize("col_rpted_bugs")) 
     307    assert_equal("Col rpted bugs", Inflector.humanize("COL_rpted_bugs")) 
     308  end 
    292309 
    293310  def test_constantize 
    294311    assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") 
  • lib/active_support/inflector.rb

    old new  
    1414  #     inflect.irregular 'octopus', 'octopi' 
    1515  # 
    1616  #     inflect.uncountable "equipment" 
     17  # 
     18  #     inflect.human /^prefx_/i, '\1' 
    1719  #   end 
    1820  # 
    1921  # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the 
     
    2224  class Inflections 
    2325    include Singleton 
    2426     
    25     attr_reader :plurals, :singulars, :uncountables 
     27    attr_reader :plurals, :singulars, :uncountables, :humans, :human_strings 
    2628     
    2729    def initialize 
    28       @plurals, @singulars, @uncountables = [], [], [] 
     30      @plurals, @singulars, @uncountables, @humans, @human_strings = [], [], [], [], {} 
    2931    end 
    3032     
    3133    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.  
     
    6163      (@uncountables << words).flatten! 
    6264    end 
    6365     
     66    # Specifies a humanized form of a string by a regular expression rule or by a string mapping. 
     67    # When using a regular expression based replacement, the normal humanize formatting is called after the replacement. 
     68    # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name') 
     69    # 
     70    # Examples: 
     71    #   human /_cnt$/i, '\1_count' 
     72    #   human "legacy_col_person_name", "Name" 
     73    #   human "strt_dt" => "Start date", 
     74    #         "upd_dt"  => "Updated date" 
     75    def human(first, last = nil) 
     76      case first 
     77        when Hash 
     78          @human_strings.merge!(first) 
     79        when String 
     80          @human_strings[first] = last 
     81        else 
     82          @humans.insert(0, [first, last]) 
     83      end 
     84    end 
     85     
    6486    # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type, 
    6587    # the options are: :plurals, :singulars, :uncountables 
    6688    # 
     
    134156  end 
    135157 
    136158  def humanize(lower_case_and_underscored_word) 
    137     lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 
     159    if result = inflections.human_strings[lower_case_and_underscored_word] 
     160      result 
     161    else 
     162      result = lower_case_and_underscored_word.to_s.dup 
     163      inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } 
     164      result.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize 
     165    end 
    138166  end 
    139167 
    140168  def demodulize(class_name_in_module)