Sha256: bb9687aaa7dd40431d2bf8d67b53543e5c12e7766e260270281af6cd70b793f6

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Decidim
  # Class used to retrieve similar emendations, scoped to the current component.
  class SimilarEmendations < Rectify::Query
    include Decidim::TranslationsHelper

    # Syntactic sugar to initialize the class and return the queried objects.
    #
    # amendment - Decidim::Amendment
    def self.for(amendment)
      new(amendment).query
    end

    # Initializes the class.
    #
    # amendment - Decidim::Amendment
    def initialize(amendment)
      @component = amendment.amendable.component
      @emendation = amendment.emendation
      @amender = amendment.amender
    end

    # Retrieves similar emendations
    def query
      emendation.class
                .where(component: component)
                .only_visible_emendations_for(amender, component)
                .published
                .not_hidden
                .where(
                  "GREATEST(#{title_similarity}, #{body_similarity}) >= ?",
                  translated_attribute(emendation.title),
                  translated_attribute(emendation.body),
                  amendable_module.similarity_threshold
                )
                .limit(amendable_module.similarity_limit)
    end

    private

    attr_reader :component, :emendation, :amender

    def amendable_module
      emendation.class.module_parent
    end

    def title_similarity
      "similarity(title::text, ?)"
    end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.25.2 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.1 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.0 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.0.rc4 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.0.rc3 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.0.rc2 app/queries/decidim/similar_emendations.rb
decidim-core-0.25.0.rc1 app/queries/decidim/similar_emendations.rb