Sha256: cb91416845f310214e25a163584ed4197c0f333b2e3b7b93004a5bb875112c8e

Contents?: true

Size: 988 Bytes

Versions: 3

Compression:

Stored size: 988 Bytes

Contents

# encoding: utf-8

module PagesSphinx
  module PagesControllerExtension
    extend ActiveSupport::Concern

    def search
      @search_query = params[:q] || params[:query] || ""
      @search_category_id = params[:category_id]

      @pages = Page.search(
        normalize_search_query(@search_query),
        search_options(category_id: @search_category_id)
      )
      @pages.each { |p| p.localize!(locale) }
      @pages
    end

    private

    def normalize_search_query(str)
      str.split(/\s+/)
         .map { |p| "#{p}*" }
         .join(" ")
    end

    def search_options(category_id: nil)
      options = {
        page:      (params[:page] || 1).to_i,
        per_page:  20,
        include:   [:localizations, :categories, :image, :author],
        order:     :published_at,
        sort_mode: :desc,
        with:      { status: 2, autopublish: 0 }
      }
      options[:with][:category_ids] = category_id unless category_id.blank?
      options
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_sphinx-0.0.3 lib/pages_sphinx/pages_controller_extension.rb
pages_sphinx-0.0.2 lib/pages_sphinx/pages_controller_extension.rb
pages_sphinx-0.0.1 lib/pages_sphinx/pages_controller_extension.rb