Module: Decidim::CategoriesHelper

Defined in:
decidim-core/app/helpers/decidim/categories_helper.rb

Overview

Helper for rendering Decidim::Category related stuff.

Instance Method Summary collapse

Instance Method Details

#categories_for_select(scope) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'decidim-core/app/helpers/decidim/categories_helper.rb', line 6

def categories_for_select(scope)
  sorted_main_categories = scope.first_class.includes(:subcategories).sort_by do |category|
    translated_attribute(category.name, category.participatory_space.organization)
  end

  sorted_main_categories.flat_map do |category|
    parent = [[translated_attribute(category.name, category.participatory_space.organization), category.id]]

    sorted_subcategories = category.subcategories.sort_by do |subcategory|
      translated_attribute(subcategory.name, subcategory.participatory_space.organization)
    end

    sorted_subcategories.each do |subcategory|
      parent << ["- #{translated_attribute(subcategory.name, subcategory.participatory_space.organization)}", subcategory.id]
    end

    parent
  end
end