Class: Decidim::Initiatives::FreetextInitiativeTypes

Inherits:
Query
  • Object
show all
Defined in:
decidim-initiatives/app/queries/decidim/initiatives/freetext_initiative_types.rb

Overview

This query searches scopes by name.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Query

#cached_query, #each, #eager?, #exists?, merge, #none?, #relation?, #|

Constructor Details

#initialize(organization, lang, text) ⇒ FreetextInitiativeTypes

Initializes the class.

organization - an Organization context for the initiative type search lang - the language code to be used for the search text - the text to be searched in initiative type titles



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

def initialize(organization, lang, text)
  @organization = organization
  @lang = lang
  @text = text
end

Class Method Details

.for(organization, lang, text) ⇒ Object

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

organization - an Organization context for the initiative type search lang - the language code to be used for the search text - the text to be searched in initiative type titles



12
13
14
# File 'decidim-initiatives/app/queries/decidim/initiatives/freetext_initiative_types.rb', line 12

def self.for(organization, lang, text)
  new(organization, lang, text).query
end

Instance Method Details

#queryObject

Finds scopes in the given organization and language whose name begins with the indicated text.

Returns an ActiveRecord::Relation.



30
31
32
33
34
35
36
# File 'decidim-initiatives/app/queries/decidim/initiatives/freetext_initiative_types.rb', line 30

def query
  return InitiativesType.where(organization: @organization) if @text.blank?

  InitiativesType
    .where(organization: @organization)
    .where("title->>? ilike ?", @lang, "#{@text}%")
end