All ActionView Helpers on Strings

Insert all the helpers you want and “go!”.strip_tags.auto_link.truncate(25)

# ActionView text helpers
# https://grosser.it/2009/05/30/all-actionview-helpers-on-strings/
class String
  %w[auto_link excerpt highlight sanitize simple_format strip_tags word_wrap].each do |method|
    define_method method do |*args|
      ActionController::Base.helpers.send method, self, *args
    end
  end

  # called with a number or with :length=>10
  def truncate(*args)
    if args.first.is_a?(Hash)
      ActionController::Base.helpers.truncate(self, *args)
    else
      ActionController::Base.helpers.truncate(self, :length=>args[0])
    end
  end
end

2 thoughts on “All ActionView Helpers on Strings

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s