Sha256: 00d286b361a751c2f73a87eb0b29274ed2a7e2711c61f9c39a11c21126174cf1

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

module Kms
  class SearchItem
    extend ActionView::Helpers::TextHelper
    include Liquor::Dropable
    include Liquor::External

    attr_reader :title, :content, :link

    HIGHLIGHTER = '<mark class="highlight">\1</mark>'.freeze

    def initialize opts
      @title = opts[:title]
      @link = opts[:link]
      @content = opts[:content]
    end

    export :link
    export :title
    export :content

    def link
      @link
    end

    def title
      @title
    end

    def content
      @content
    end

    # attributes_map for Kms::Page is:
    # {title: :title, link: :fullpath, content: :content}
    def self.item_for_searchable(searchable, keywords, attributes_map)
      sanitized_title = ActionController::Base.helpers.sanitize(searchable[attributes_map[:title]])
      sanitized_content = ActionController::Base.helpers.sanitize(searchable[attributes_map[:content]])
      Kms::SearchItem.new(
        title: highlight(sanitized_title, keywords, highlighter: HIGHLIGHTER, sanitize: false),
        link: searchable[attributes_map[:link]],
        content: highlight(sanitized_content, keywords, highlighter: HIGHLIGHTER, sanitize: false)
      )
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kms-1.2.1 lib/kms/search_item.rb
kms-1.2.0 lib/kms/search_item.rb
kms-1.1.0 lib/kms/search_item.rb
kms-1.0.1 lib/kms/search_item.rb
kms-1.0.0 lib/kms/search_item.rb