Sha256: e5e8e6416c2dffa057cb6030def3cb1f19f155f8c4c04b7164d42cf602932285

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Decidim
  module Initiatives
    # Class uses to retrieve similar initiatives types.
    class SimilarInitiatives < Decidim::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,
            translated_attribute(form.title),
            translated_attribute(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

7 entries across 7 versions & 1 rubygems

Version Path
decidim-initiatives-0.28.4 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.3 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.2 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.1 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.0 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.0.rc5 app/queries/decidim/initiatives/similar_initiatives.rb
decidim-initiatives-0.28.0.rc4 app/queries/decidim/initiatives/similar_initiatives.rb