Sha256: bd0c3a2fe76361f6619b0e061d7c6b2bc907e5404047e5a783365a217b4e7256

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 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
          .where(
            "GREATEST(#{title_similarity}, #{body_similarity}) >= ?",
            @proposal[:title],
            @proposal[:body],
            Decidim::Proposals.similarity_threshold
          )
          .limit(Decidim::Proposals.similarity_limit)
      end

      private

      attr_reader :proposal

      def title_similarity
        "similarity(title, ?)"
      end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-proposals-0.13.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.12.2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.13.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.12.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.13.0.pre1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.12.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.11.2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.12.0.pre app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.11.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.11.0.pre1 app/queries/decidim/proposals/similar_proposals.rb