Sha256: 549d8af0a564ced0bfdbc52f1d5e7ff3ad133556d2f1c1fea8975abbc79aa997

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 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(
            Arel.sql("GREATEST(#{title_similarity}, #{description_similarity}) >= ?").to_s,
            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

12 entries across 12 versions & 1 rubygems

Version Path
decidim-initiatives-0.26.10 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.9 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.8 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.7 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.5 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.4 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.3 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.0.rc2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.26.0.rc1 app/queries/decidim/initiatives/similar_initiatives.rb