Sha256: 0b91840c5dddb0a08c967f63635db8fa34a9c7980d9bb50130f7940e3e0acf84

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    # Class used to retrieve similar proposals.
    class SimilarProposals < Rectify::Query
      include Decidim::TranslationsHelper

      # Syntactic sugar to initialize the class and return the queried objects.
      #
      # components - Decidim::CurrentComponent
      # proposal - Decidim::Proposals::Proposal
      def self.for(components, proposal)
        new(components, proposal).query
      end

      # Initializes the class.
      #
      # components - Decidim::CurrentComponent
      # proposal - Decidim::Proposals::Proposal
      def initialize(components, proposal)
        @components = components
        @proposal = proposal
      end

      # Retrieves similar proposals
      def query
        Decidim::Proposals::Proposal
          .where(component: @components)
          .published
          .not_hidden
          .where(
            "GREATEST(#{title_similarity}, #{body_similarity}) >= ?",
            translated_attribute(@proposal.title),
            translated_attribute(@proposal.body),
            Decidim::Proposals.similarity_threshold
          )
          .limit(Decidim::Proposals.similarity_limit)
      end

      private

      def title_similarity
        "similarity(title::text, ?)"
      end

      def body_similarity
        "similarity(body::text, ?)"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-proposals-0.25.0.rc4 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.25.0.rc3 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.25.0.rc2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.25.0.rc1 app/queries/decidim/proposals/similar_proposals.rb