Sha256: 2b31979c5435848fa8bbda9d87be9fe8e7f4f37cd9e9cd62b24d5827d7892c2f

Contents?: true

Size: 1.48 KB

Versions: 12

Compression:

Stored size: 1.48 KB

Contents

class EssenceRichtext < ActiveRecord::Base

  acts_as_essence(
    :preview_text_column => :stripped_body
  )

  # Require acts_as_ferret only if Ferret full text search is enabled (default).
  # You can disable it in +config/alchemy/config.yml+
  if Alchemy::Config.get(:ferret) == true
    require 'acts_as_ferret'
    acts_as_ferret(
      :fields => {
        :stripped_body => {:store => :yes}
      },
      :remote => false
    )
    before_save :check_ferret_indexing
  end

  before_save :strip_content

  # Saves the ingredient
  def save_ingredient(params, options = {})
    return true if params.blank?
    self.body = params['body'].to_s
    self.public = options[:public]
    self.save
  end

private

  def strip_content
    self.stripped_body = strip_tags(self.body)
  end

  def check_ferret_indexing
    if self.do_not_index
      self.disable_ferret(:always)
    end
  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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
alchemy_cms-2.0.7 app/models/essence_richtext.rb
alchemy_cms-2.0.6.1 app/models/essence_richtext.rb
alchemy_cms-2.0.5 app/models/essence_richtext.rb
alchemy_cms-2.0.4 app/models/essence_richtext.rb
alchemy_cms-2.0.3.1 app/models/essence_richtext.rb
alchemy_cms-2.1.beta1 app/models/essence_richtext.rb
alchemy_cms-2.0.3 app/models/essence_richtext.rb
alchemy_cms-2.0.2 app/models/essence_richtext.rb
alchemy_cms-2.0.1 app/models/essence_richtext.rb
alchemy_cms-2.0 app/models/essence_richtext.rb
alchemy_cms-2.0.rc6 app/models/essence_richtext.rb
alchemy_cms-2.0.rc5 app/models/essence_richtext.rb