Sha256: 7e750f45d9c93d2ba9014d08eb48d14ac63ec036196969ef514398bc0d0f19b6

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 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}) >= ?",
                  emendation.title,
                  emendation.body,
                  amendable_module.similarity_threshold
                )
                .limit(amendable_module.similarity_limit)
    end

    private

    attr_reader :component, :emendation, :amender

    def amendable_module
      emendation.class.parent
    end

    def title_similarity
      "similarity(title, ?)"
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.22.0 app/queries/decidim/similar_emendations.rb
decidim-core-0.21.0 app/queries/decidim/similar_emendations.rb
decidim-core-0.20.1 app/queries/decidim/similar_emendations.rb
decidim-core-0.20.0 app/queries/decidim/similar_emendations.rb
decidim-core-0.19.1 app/queries/decidim/similar_emendations.rb
decidim-core-0.19.0 app/queries/decidim/similar_emendations.rb