Sha256: 61b4822a9ac12aacc3520d354d2c6c8622e4d39ff00b495fe476583989427388

Contents?: true

Size: 1.39 KB

Versions: 52

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # Class uses to retrieve similar initiatives types.
    class SimilarInitiatives < Rectify::Query
      include Decidim::TranslationsHelper
      include CurrentLocale

      # Syntactic sugar to initialize the class and return the queried objects.
      #
      # organization - Decidim::Organization
      # form - Decidim::Initiatives::PreviousForm
      def self.for(organization, form)
        new(organization, form).query
      end

      # Initializes the class.
      #
      # organization - Decidim::Organization
      # form - Decidim::Initiatives::PreviousForm
      def initialize(organization, form)
        @organization = organization
        @form = form
      end

      # Retrieves similar initiatives
      def query
        Initiative
          .published
          .where(organization: @organization)
          .where(
            "GREATEST(#{title_similarity}, #{description_similarity}) >= ?",
            form.title,
            form.description,
            Decidim::Initiatives.similarity_threshold
          )
          .limit(Decidim::Initiatives.similarity_limit)
      end

      private

      attr_reader :form

      def title_similarity
        "similarity(title->>'#{current_locale}', ?)"
      end

      def description_similarity
        "similarity(description->>'#{current_locale}', ?)"
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
decidim-initiatives-0.14.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.13.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.12.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.13.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.12.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.13.0.pre1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.12.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.11.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.12.0.pre app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.11.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.11.0.pre1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.9.1 app/queries/decidim/initiatives/similar_initiatives.rb