Sha256: 18ed78e2d012c0370200e8c718a50bedbcd7b9ad1e2b63cf83b320c38c555a10

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require_dependency Locomotive::Engine.root.join('app', 'models', 'locomotive', 'page').to_s

Locomotive::Page.class_eval do

  include Locomotive::Search::Extension

  ## fields ##
  field :searchable, type: Boolean, default: true

  ## behaviours ##
  search_by [
    :title, :searchable_content,
    store:  [:search_type, :label, :site_id, :fullpath],
    locale: proc { ::Mongoid::Fields::I18n.locale }
  ], if: :is_searchable?

  ## methods

  def search_type
    'page'
  end

  def label
    self.title
  end

  def indexable_id
    if respond_to?(:site_id)
      "site_#{site_id}_page_#{id}"
    else
      "page_#{id}"
    end
  end

  def is_searchable?
    !not_found? && searchable? && published?
  end

  def searchable_content
    [].tap do |content|
      # # 1. add the editable elements
      # self.editable_elements.each do |element|
      #   # we don't want to include fixed editable elements of children.
      #   next if !element.is_a?(Locomotive::EditableText) || (element.fixed? && element.from_parent?)
      #   content << element.content
      # end

      # add the raw template by rendering it (will render the editable elements)

      # get a simple version of the template. not need to apply the "layout"
      # for instance.
      # then, render this template
      template = self.raw_template.sub(/\{\%\s*extends [^%]*\s*\%\}/, '')

      # modify the context instance so that the exceptions which might raise
      # won't be displayed in the rendered output.
      context = ::Liquid::Context.new({}, {}, { site: site, page: self }, false)

      context.instance_eval do
        def handle_error(e); '' end
      end

      # render the page
      content << ::Liquid::Template.parse(template, {}).render(context)
    end.join("\n")
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotivecms-search-0.3.4 app/models/locomotive/page.rb
locomotivecms-search-0.3.3 app/models/locomotive/page.rb
locomotivecms-search-0.3.2 app/models/locomotive/page.rb