Sha256: 0982af31a667eddbfcd80595961626f20d1ab9c6b3dd142cfad7cec1282a3902

Contents?: true

Size: 1.29 KB

Versions: 20

Compression:

Stored size: 1.29 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

      def title_similarity
        "similarity(title, ?)"
      end

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
decidim-proposals-0.22.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.21.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.20.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.20.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.19.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.18.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.19.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.17.2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.18.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.17.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.16.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.17.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.16.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.15.2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.15.1 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.15.0 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.14.4 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.14.3 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.14.2 app/queries/decidim/proposals/similar_proposals.rb
decidim-proposals-0.14.1 app/queries/decidim/proposals/similar_proposals.rb