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, #ordered_scopes_descendants, #ordered_scopes_descendants_for_select, #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 analyze

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
48
# 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:, 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, help_text: nil, checkboxes_on_top: true) ⇒ Object

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

Returns nothing.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/decidim/scopes_helper.rb', line 82

def scopes_picker_filter(form, name, help_text: nil, checkboxes_on_top: true)
  options = {
    multiple: true,
    legend_title: I18n.t("decidim.scopes.scopes"),
    label: false,
    help_text:,
    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_select_field(form, name, root: false, options: {}, html_options: {}) ⇒ Object

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

Returns nothing.



56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/decidim/scopes_helper.rb', line 56

def scopes_select_field(form, name, root: false, options: {}, html_options: {})
  options = options.merge(include_blank: I18n.t("decidim.scopes.prompt")) unless options.has_key?(:include_blank)

  form.select(
    name,
    ordered_scopes_descendants_for_select(root),
    options,
    html_options
  )
end

#scopes_select_tag(name, root: false, options: {}) ⇒ Object



67
68
69
70
71
72
73
# File 'app/helpers/decidim/scopes_helper.rb', line 67

def scopes_select_tag(name, root: false, options: {})
  select_tag(
    name,
    options_for_select(ordered_scopes_descendants_for_select(root)),
    options.merge(include_blank: I18n.t("decidim.scopes.prompt"))
  )
end