Class: Decidim::ScopesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/decidim/scopes_controller.rb

Overview

Exposes the scopes text search so users can choose a scope writing its name.

Instance Method Summary collapse

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#searchObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/decidim/scopes_controller.rb', line 6

def search
  authorize! :search, Scope
  root = Scope.where(id: params[:root], organization: current_organization).first
  scopes = if params[:term].present?
             FreetextScopes.for(current_organization, I18n.locale, params[:term], root)
           elsif root
             root.children
           else
             current_organization.top_scopes
           end
  root_option = if params[:include_root] == "true" && params[:term].blank?
                  if root
                    [{ id: root.id.to_s, text: root.name[I18n.locale.to_s] }]
                  else
                    [{ id: "global", text: I18n.t("decidim.scopes.global") }]
                  end
                else
                  []
                end

  render json: { results: root_option + scopes.map { |scope| { id: scope.id.to_s, text: scope.name[I18n.locale.to_s] } } }
end