Sha256: ea7ad8a819a7c7ab2106a42290291801895a6b8389569d31c21530f405e9ebd9

Contents?: true

Size: 1.75 KB

Versions: 62

Compression:

Stored size: 1.75 KB

Contents

module Workarea
  module Search
    class Customization
      include ApplicationDocument
      include Releasable
      include Contentable
      include Commentable
      include FeaturedProducts
      include ProductList

      field :_id, type: String
      field :query, type: String
      field :rewrite, type: String
      field :redirect, type: String

      index({ query: 1 })

      validates :query, presence: true
      validate :not_star_query
      list_field :product_ids

      alias_method :name, :query

      def self.autocomplete(string)
        regex = /^#{::Regexp.quote(string)}/
        where(query: regex).pluck(:query)
      end

      def self.find_by_query(query)
        result = find_or_initialize_by(id: QueryString.new(query).id)
        result.query ||= query
        result
      end

      def self.positions_for_product(id)
        self.in(product_ids: Array(id)).reduce({}) do |memo, sort|
          memo[sort.id] = sort.product_ids.index(id.to_s)
          memo
        end
      end

      def self.sorts
        [Workarea::Sort.newest, Workarea::Sort.query]
      end

      def query=(value)
        super(value.downcase)
      end

      def redirect
        value = read_attribute(:redirect)

        return unless value.present?
        return value if UrlValidator.valid_url?(value)

        case value
        when /\A\//
          value
        when /[\.{2,}]/
          "http://#{value}"
        else
          "/#{value}"
        end
      end

      def rewrite?
        rewrite.present?
      end

      def redirect?
        redirect.present?
      end

      def not_star_query
        if query.to_s.strip == '*'
          errors.add(:query, I18n.t('workarea.errors.messages.not_star_query'))
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/search/customization.rb
workarea-core-3.5.26 app/models/workarea/search/customization.rb
workarea-core-3.4.45 app/models/workarea/search/customization.rb
workarea-core-3.5.25 app/models/workarea/search/customization.rb
workarea-core-3.5.23 app/models/workarea/search/customization.rb
workarea-core-3.4.44 app/models/workarea/search/customization.rb
workarea-core-3.5.22 app/models/workarea/search/customization.rb
workarea-core-3.4.43 app/models/workarea/search/customization.rb
workarea-core-3.5.21 app/models/workarea/search/customization.rb
workarea-core-3.4.42 app/models/workarea/search/customization.rb
workarea-core-3.5.20 app/models/workarea/search/customization.rb
workarea-core-3.4.41 app/models/workarea/search/customization.rb
workarea-core-3.5.19 app/models/workarea/search/customization.rb
workarea-core-3.4.40 app/models/workarea/search/customization.rb
workarea-core-3.5.18 app/models/workarea/search/customization.rb
workarea-core-3.4.39 app/models/workarea/search/customization.rb
workarea-core-3.5.17 app/models/workarea/search/customization.rb
workarea-core-3.4.38 app/models/workarea/search/customization.rb
workarea-core-3.5.16 app/models/workarea/search/customization.rb
workarea-core-3.4.37 app/models/workarea/search/customization.rb