Sha256: c030965673c687ec9af12f3abec41248af9bca972862076d5797957767382304

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Locomotive
  class SearchService

    def from_backoffice(site, text, options = { radius: 150 })
      # scope the search by the site
      conditions = { 'site_id' => site._id }

      # locale agnostic
      options[:locale] = false

      # launch the search with the current engine
      ::ActiveSearch.search(text, conditions, options).map do |entry|
        # keep only what we really need to display in the suggestions list
        entry.slice('label', 'locale', 'search_type').tap do |_entry|
          _entry['path']         = entry_path(entry)
          _entry['content']      = entry_content(entry)
          _entry['with_locale']  = site.localized?
        end
      end
    end

    protected

    def entry_content(entry)
      content = nil

      # get the first non blank highligthed field
      entry['highlighted'].each do |name, value|
        content = value if content.nil? && value.present?
      end

      content
    end

    def entry_path(entry)
      case entry['original_type']
      when 'Locomotive::Page'
        page_path(entry['original_id'], entry['locale'])
      when /^Locomotive::ContentEntry/
        content_entry_path(entry['content_type_slug'], entry['original_id'], entry['locale'])
      else
        nil
      end
    end

    def page_path(id, locale)
      Locomotive::Engine.routes.url_helpers.edit_page_path(id, content_locale: locale)
    end

    def content_entry_path(content_type_slug, id, locale)
      Locomotive::Engine.routes.url_helpers.edit_content_entry_path(content_type_slug, id, content_locale: locale)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms-search-0.3.6 app/services/locomotive/search_service.rb
locomotivecms-search-0.3.5 app/services/locomotive/search_service.rb