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.25.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.0.rc4 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.0.rc3 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.0.rc2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.25.0.rc1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.3 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.6 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.5 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.0.rc2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.4 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.24.0.rc1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.3 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.23.1.rc1 app/queries/decidim/initiatives/similar_initiatives.rb