Class: Decidim::Proposals::SimilarProposals

Inherits:
Query
  • Object
show all
Includes:
TranslationsHelper
Defined in:
decidim-proposals/app/queries/decidim/proposals/similar_proposals.rb

Overview

Class used to retrieve similar proposals.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TranslationsHelper

empty_translatable, ensure_translatable, multi_translation, translated_in_current_locale?

Methods included from TranslatableAttributes

#default_locale?

Constructor Details

#initialize(components, proposal) ⇒ SimilarProposals

Initializes the class.

components - Decidim::CurrentComponent proposal - Decidim::Proposals::Proposal


21
22
23
24
25
# File 'decidim-proposals/app/queries/decidim/proposals/similar_proposals.rb', line 21

def initialize(components, proposal)
  @components = components
  @proposal = proposal
  @translations_enabled = proposal.component.organization.enable_machine_translations
end

Class Method Details

.for(components, proposal) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

components - Decidim::CurrentComponent proposal - Decidim::Proposals::Proposal


13
14
15
# File 'decidim-proposals/app/queries/decidim/proposals/similar_proposals.rb', line 13

def self.for(components, proposal)
  new(components, proposal).query
end

Instance Method Details

#queryObject

Retrieves similar proposals


28
29
30
31
32
33
34
35
36
37
38
39
# File 'decidim-proposals/app/queries/decidim/proposals/similar_proposals.rb', line 28

def query
  Decidim::Proposals::Proposal
    .where(component: @components)
    .published
    .not_hidden
    .where(
      Arel.sql("GREATEST(#{title_similarity}, #{body_similarity}) >= ?").to_s,
      *similarity_params,
      Decidim::Proposals.similarity_threshold
    )
    .limit(Decidim::Proposals.similarity_limit)
end