Sha256: f48f03e710387e9d821e8f1099f426f78556826689ab85b46384e2fe02f9b09d

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# == Schema Information
#
# Table name: alchemy_essence_richtexts
#
#  id            :integer          not null, primary key
#  body          :text
#  stripped_body :text
#  public        :boolean
#  creator_id    :integer
#  updater_id    :integer
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#

module Alchemy
  class EssenceRichtext < ActiveRecord::Base
    acts_as_essence preview_text_column: 'stripped_body'

    before_save :strip_content

    def has_tinymce?
      true
    end

    private

    def strip_content
      self.stripped_body = strip_tags(body)
    end

    # Stripping HTML Tags and only returns plain text.
    def strip_tags(html)
      return html if html.blank?
      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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alchemy_cms-3.3.0.rc2 app/models/alchemy/essence_richtext.rb
alchemy_cms-3.3.0.rc1 app/models/alchemy/essence_richtext.rb