Sha256: aa89663ba0a3fcf15a01fd14616978f47f048bd84a9689e35259ae477a1d7425

Contents?: true

Size: 1.49 KB

Versions: 12

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Decidim
  # Class used to retrieve similar emendations, scoped to the current component.
  class SimilarEmendations < Decidim::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(
                  Arel.sql("GREATEST(#{title_similarity}, #{body_similarity}) >= ?").to_s,
                  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

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.8 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.7 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.6 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.5 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.4 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.3 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.2 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.1 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.0 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.0.rc2 app/queries/decidim/similar_emendations.rb
decidim-core-0.27.0.rc1 app/queries/decidim/similar_emendations.rb