RailsTweeter
RailsTweeter

Reputation: 1635

Override titleize method in Ruby on Rails

Is there a way to override the .titleize method in RoR?

For example, titleize does not work for names like TJ Watson. It converts 'tj watson' to Tj Watson. I would like to add such rules to it.

Also, I don't want to define a new method because my code already uses .titleize through it. How can I add features to titleize without having to change the method calls throughout the code?

Upvotes: 0

Views: 1846

Answers (1)

jdoe
jdoe

Reputation: 15771

Put some *.rb file in your initializers folder with the following content:

# encoding: utf-8

module ActiveSupport
  module Inflector
    def titleize(word)
      # old code for referencing:
      # humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
      < your code goes here >
    end
  end
end

Also check this approach: https://stackoverflow.com/a/10471857/1322562.

Upvotes: 2

Related Questions