DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Extend String To Use ActionView's Text Helpers

Snippets Manager user avatar by
Snippets Manager
·
Aug. 08, 06 · · Code Snippet
Like (0)
Save
Tweet
567 Views

Join the DZone community and get the full member experience.

Join For Free
ActionView's TextHelper methods are useful, but I often need to use them in my controller or my model. For several of the TextHelper methods that expect a string as input, it makes sense to extend the String class.

So, if I want to strip HTML tags, auto link any URLs, and then simple format a comment (in that order) before I save it in the database I can do:

class Comment < ActiveRecord::Base
#...
  def before_save
    self.text = self.text.strip_tags.auto_link.simple_format
  end
end


This method is much cleaner than including ActionView::Helpers::TextHelper in whatever class I'm in and passing the string as an argument to each method.

Below is the magic code. Since TextHelper is a module, we create a Singleton class to reference the methods, create the wrapper methods in their own module, and finally include that module in the String class. Note that not all TextHelper methods are included–just the ones that make sense. Drop this code into a file and require it in your environment or within a plugin.


# ActionView Text Helpers are great!
# Let's extend the String class to allow us to call
# some of these methods directly on a String.
# Note:
#  - cycle-related methods are not included
#  - concat is not included
#  - pluralize is not included because it is in
#       ActiveSupport String extensions already
#       (though they differ).
#  - markdown requires BlueCloth
#  - textilize methods require RedCloth
# Example:
# "coolness".strip_tags -> "coolness"

require 'singleton'

# Singleton to be called in wrapper module
class TextHelperSingleton
  include Singleton
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers::TagHelper #tag_options needed by auto_link
end

# Wrapper module
module MyExtensions #:nodoc:
  module CoreExtensions #:nodoc:
    module String #:nodoc:
      module TextHelper

        def auto_link(link = :all, href_options = {}, &block)
          TextHelperSingleton.instance.auto_link(self, link, href_options, &block)
        end

        def excerpt(phrase, radius = 100, excerpt_string = "…")
          TextHelperSingleton.instance.excerpt(self, phrase, radius, excerpt_string)
        end

        def highlight(phrase, highlighter = '\1')
          TextHelperSingleton.instance.highlight(self, phrase, highlighter)
        end
 
        begin
          require_library_or_gem 'bluecloth'

          def markdown
            TextHelperSingleton.instance.markdown(self)
          end
        rescue LoadError
          # do nothing.  method will be undefined
        end

        def sanitize
          TextHelperSingleton.instance.sanitize(self)
        end

        def simple_format
          TextHelperSingleton.instance.simple_format(self)
        end

        def strip_tags
          TextHelperSingleton.instance.strip_tags(self)
        end

        begin
          require_library_or_gem 'redcloth'

          def textilize
            TextHelperSingleton.instance.textilize(self)
          end

          def textilize_without_paragraph
            TextHelperSingleton.instance.textilize_without_paragraph(self)
          end
        rescue LoadError
          # do nothing.  methods will be undefined
        end

        def truncate(length = 30, truncate_string = "…")
          TextHelperSingleton.instance.truncate(self, length, truncate_string)
        end

        def word_wrap(line_width = 80)
          TextHelperSingleton.instance.word_wrap(self, line_width)
        end

      end
    end
  end
end

# extend String with the TextHelper functions
class String #:nodoc:
  include MyExtensions::CoreExtensions::String::TextHelper
end


This code originally posted on my blog:
Extend String to use ActionView's Text Helpers.

Idea was somewhat inspired by this snippet:
Using helpers inside a controller.
Strings Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Message Queuing and the Database: Solving the Dual Write Problem
  • A Simple Guide to Heaps, Stacks, References, and Values in JavaScript
  • What Is URL Rewriting? | Java Servlets
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo