Forum: Ruby on Rails help putting this code into plugin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
03846c43c27a94ae5ffe4cddb3cc5951?d=identicon&s=25 scot (Guest)
on 2007-04-13 18:29
Hi,

I have the following code that allows me to call before_validation
:acts_as_tagless in my models.

I would like to put this into aplugin so I can do somthing like
acts_as_tagless :fieldName, :fieldName, :etc

any suggestions on where to start?

Thanks,
Scot

    #
    #removes html tags
    def acts_as_tagless

        string_attributes = {}
        self.attributes.each do |each_name, each_attribute|
            if each_attribute.kind_of? String
              string_attributes[each_name] = strip_tags(each_attribute)
            end
        end
        self.attributes = string_attributes

    end

  #
    #strip_tags function taken from ActionView
    def self.strip_tags(html)
        if html.index("<")
            text = ""
            tokenizer = HTML::Tokenizer.new(html)

            while token = tokenizer.next
                node = HTML::Node.parse(nil, 0, 0, token, false)
                # result is only the content of any Text nodes
                text << node.to_s if node.class == HTML::Text
            end
                # strip any comments, and if they have a newline at the
end (ie. line with
                # only a comment) strip that too
                text.gsub(/<!--(.*?)-->[\n]?/m, "")
        else
            html # already plain text
        end
    end
This topic is locked and can not be replied to.