Sha256: a61d7a842d5e5b34d9510a96136600e01fc9ea4167b7c6d0c1690b7881237afd

Contents?: true

Size: 1.88 KB

Versions: 25

Compression:

Stored size: 1.88 KB

Contents

module Alchemy
  class EssenceRichtext < ActiveRecord::Base

    acts_as_essence(
      :preview_text_column => :stripped_body
    )

    attr_accessible :do_not_index, :body, :public, :stripped_body

    before_save :strip_content


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

      # Ensures that the current setting for do_not_index gets updated in the db.
      before_save { write_attribute(:do_not_index, description['do_not_index'] || false); return true }

      # Disables the ferret indexing, if do_not_index attribute is set to true
      #
      # You can disable indexing in the elements.yml file.
      #
      # === Example
      #
      #   name: secrets
      #   contents:
      #   - name: confidential
      #     type: EssenceRichtext
      #     do_not_index: true
      #
      def ferret_enabled?(is_bulk_index = false)
        !do_not_index?
      end
    end

  private

    def strip_content
      self.stripped_body = strip_tags(self.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

25 entries across 25 versions & 1 rubygems

Version Path
alchemy_cms-2.9.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.9.0 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.8.3 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.5 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.3 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.4 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.8.2 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.8.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.3 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.2 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.7.0 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.2.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.2 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.0 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.6.0.rc5 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.5.3.1 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.5.3 app/models/alchemy/essence_richtext.rb
alchemy_cms-2.5.2.2 app/models/alchemy/essence_richtext.rb