Sha256: 738b12f41dc17263f54e1d4229c9d7288761c40a8c79a3a88fb58187f0ad9480

Contents?: true

Size: 1.69 KB

Versions: 12

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true
require_dependency 'thredded/search_parser'
module Thredded
  class TopicsSearch
    def initialize(query, scope)
      @terms = SearchParser.new(query).parse
      @scope = scope

      @search_categories = @search_users = @search_text = nil
    end

    # @return [ActiveRecord::Relation<Thredded::Topic>]
    def search
      if categories.present?
        @scope = @scope.joins(:topic_categories).merge(TopicCategory.where(category_id: categories))
      end
      if text.present? || users.present?
        [search_topics, search_posts].compact.reduce(:union)
      else
        @scope
      end
    end

    protected

    def search_topics
      scope = @scope
      scope = @scope.where(user_id: users) if users.present?
      scope = DbTextSearch::FullText.new(scope, :title).search(text) if text.present?
      scope
    end

    def search_posts
      posts_scope = Thredded::Post
      posts_scope = posts_scope.where(user_id: users) if users.present?
      posts_scope = DbTextSearch::FullText.new(posts_scope, :content).search(text) if text.present?
      @scope.joins(:posts).merge(posts_scope)
    end

    def categories
      @search_categories ||=
        if @terms['in'].present?
          DbTextSearch::CaseInsensitive
            .new(Category, :name)
            .in(@terms['in']).pluck(:id)
        else
          []
        end
    end

    def users
      @search_users ||=
        if @terms['by']
          DbTextSearch::CaseInsensitive
            .new(Thredded.user_class, Thredded.user_name_column)
            .in(@terms['by']).pluck(:id)
        else
          []
        end
    end

    # @return [Array<String>]
    def text
      @terms['text']
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
thredded-0.8.2 lib/thredded/topics_search.rb
thredded-0.7.0 lib/thredded/topics_search.rb
thredded-0.6.3 lib/thredded/topics_search.rb
thredded-0.6.2 lib/thredded/topics_search.rb
thredded-0.6.1 lib/thredded/topics_search.rb
thredded-0.6.0 lib/thredded/topics_search.rb
thredded-0.5.1 lib/thredded/topics_search.rb
thredded-0.5.0 lib/thredded/topics_search.rb
thredded-0.4.0 lib/thredded/topics_search.rb
thredded-0.3.2 lib/thredded/topics_search.rb
thredded-0.3.1 lib/thredded/topics_search.rb
thredded-0.3.0 lib/thredded/topics_search.rb