Module: Decidim::ScopesHelper

Includes:
DecidimFormHelper, TranslatableAttributes
Included in:
ApplicationHelper, ScopesPickerCell, ViewModel
Defined in:
app/helpers/decidim/scopes_helper.rb

Overview

A Helper to render scopes, including a global scope, for forms.

Defined Under Namespace

Classes: Option

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Methods included from DecidimFormHelper

#areas_for_select, #base_error_messages, #decidim_form_for, #decidim_form_slug_url, #editor_field_tag, #form_field_has_error?, #form_required_explanation, #name_with_locale, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Instance Method Details

#has_visible_scopes?(resource) ⇒ Boolean

Checks if the resource should show its scope or not. resource - the resource to analize

Returns boolean.

Returns:

  • (Boolean)


15
16
17
18
19
# File 'app/helpers/decidim/scopes_helper.rb', line 15

def has_visible_scopes?(resource)
  resource.component.scopes_enabled? &&
    resource.scope.present? &&
    resource.component.scope != resource.scope
end

#scope_name_for_picker(scope, global_name) ⇒ Object

Retrieves the translated name and type for an scope. scope - a Decidim::Scope global_name - text to use when scope is nil

Returns a string



26
27
28
29
30
31
32
# File 'app/helpers/decidim/scopes_helper.rb', line 26

def scope_name_for_picker(scope, global_name)
  return global_name unless scope

  name = translated_attribute(scope.name)
  name << " (#{translated_attribute(scope.scope_type.name)})" if scope.scope_type
  name
end

#scopes_picker_field(form, name, root: false, options: { checkboxes_on_top: true }) ⇒ Object

Renders a scopes picker field in a form. form - FormBuilder object name - attribute name options - An optional Hash with options:

  • checkboxes_on_top - Show checked picker values on top (default) or below the picker prompt (only for multiple pickers)

Returns nothing.



41
42
43
44
45
46
47
# File 'app/helpers/decidim/scopes_helper.rb', line 41

def scopes_picker_field(form, name, root: false, options: { checkboxes_on_top: true })
  root = try(:current_participatory_space)&.scope if root == false
  form.scopes_picker name, options do |scope|
    { url: decidim.scopes_picker_path(root: root, current: scope&.id, field: form.label_for(name)),
      text: scope_name_for_picker(scope, I18n.t("decidim.scopes.global")) }
  end
end

#scopes_picker_filter(form, name, checkboxes_on_top: true) ⇒ Object

Renders a scopes picker field in a filter form. form - FilterFormBuilder object name - attribute name checkboxes_on_top - Show picker values on top (default) or below the picker prompt (only for multiple pickers)

Returns nothing.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/decidim/scopes_helper.rb', line 70

def scopes_picker_filter(form, name, checkboxes_on_top: true)
  options = {
    multiple: true,
    legend_title: I18n.t("decidim.scopes.scopes"),
    label: false,
    checkboxes_on_top: checkboxes_on_top
  }

  form.scopes_picker name, options do |scope|
    {
      url: decidim.scopes_picker_path(
        root: try(:current_participatory_space).try(:scope),
        current: scope&.id,
        title: I18n.t("decidim.scopes.prompt"),
        global_value: "global",
        max_depth: try(:current_participatory_space).try(:scope_type_max_depth)
      ),
      text: scope_name_for_picker(scope, I18n.t("decidim.scopes.prompt"))
    }
  end
end

#scopes_picker_tag(name, value, options = {}) ⇒ Object

Renders a scopes picker field in a form, not linked to a specific model. name - name for the input value - value for the input

Returns nothing.



54
55
56
57
58
59
60
61
62
# File 'app/helpers/decidim/scopes_helper.rb', line 54

def scopes_picker_tag(name, value, options = {})
  root = try(:current_participatory_space)&.scope
  field = options[:field] || name

  scopes_picker_field_tag name, value, id: options[:id] do |scope|
    { url: decidim.scopes_picker_path(root: root, current: scope&.id, field: field),
      text: scope_name_for_picker(scope, I18n.t("decidim.scopes.global")) }
  end
end