Class: Decidim::Maintenance::ImportModels::Category

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
decidim-core/lib/decidim/maintenance/import_models/category.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_filtersObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 109

def self.all_filters
  participatory_space_classes.each_with_object([]) do |klass, list|
    klass.where(organization:).each do |participatory_space|
      name = "#{klass.model_name.human}: #{participatory_space.title[I18n.locale.to_s]}"
      components = children_components(participatory_space)
      next unless components.any?

      list << {
        name: root_taxonomy_name,
        internal_name: name,
        items: Category.where(participatory_space:).map do |category|
          names = [name]
          if category.parent
            names << if category.parent.parent
                       category.parent.parent.name[I18n.locale.to_s]
                     else
                       category.parent.name[I18n.locale.to_s]
                     end
          end
          names << category.full_name[I18n.locale.to_s]
          names
        end,
        components:
      }
    end
  end
end

.all_taxonomiesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 93

def self.all_taxonomies
  participatory_space_classes.each_with_object({}) do |klass, hash|
    klass.where(organization:).each do |participatory_space|
      next unless Category.where(participatory_space:, parent_id: nil).any?

      name = "#{klass.model_name.human}: #{participatory_space.title[I18n.locale.to_s]}"
      hash.merge!(name => {
                    name: { I18n.locale.to_s => name },
                    origin: participatory_space.to_global_id.to_s,
                    resources: {},
                    children: children_taxonomies(participatory_space)
                  })
    end
  end
end

.children_components(participatory_space) ⇒ Object



84
85
86
87
88
89
90
91
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 84

def self.children_components(participatory_space)
  participatory_space.components.filter_map do |component|
    # skip components without taxonomies in settings
    next unless component.settings.respond_to?(:taxonomy_filters)

    component.to_global_id.to_s
  end
end

.children_taxonomies(participatory_space) ⇒ Object



75
76
77
78
79
80
81
82
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 75

def self.children_taxonomies(participatory_space)
  Category.where(participatory_space:, parent_id: nil).to_h do |category|
    [
      category.name[I18n.locale.to_s],
      category.taxonomies
    ]
  end
end

.root_taxonomy_nameObject



16
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 16

def self.root_taxonomy_name = "~ #{I18n.t("decidim.admin.categories.index.categories_title")}"

Instance Method Details

#full_nameObject



27
28
29
30
31
32
33
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 27

def full_name
  return @full_name if defined?(@full_name)

  @full_name = name.dup
  @full_name[I18n.locale.to_s] = "#{parent.name[I18n.locale.to_s]} > #{@full_name[I18n.locale.to_s]}" if parent && parent.parent_id
  @full_name
end

#invalid_categorization?(categorization) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 18

def invalid_categorization?(categorization)
  return true if categorization.categorizable.nil?

  return categorization.categorizable.participatory_space != participatory_space if categorization.categorizable.respond_to?(:participatory_space)
  return categorization.categorizable.component.participatory_space != participatory_space if categorization.categorizable.respond_to?(:component)

  false
end

#resourcesObject



35
36
37
38
39
40
41
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 35

def resources
  categorizations.filter_map do |categorization|
    next if invalid_categorization?(categorization)

    [categorization.categorizable.to_global_id.to_s, resource_name(categorization.categorizable)]
  end.to_h
end

#sub_taxonomy_childrenObject

original categories are allowed one level more than the current taxonomies



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 53

def sub_taxonomy_children
  subcategories.each_with_object({}) do |subcategory, hash|
    hash[subcategory.name[I18n.locale.to_s]] = {
      name: subcategory.name,
      origin: subcategory.to_global_id.to_s,
      resources: subcategory.resources,
      children: {}
    }

    next unless subcategory.subcategories.any?

    subcategory.subcategories.each do |last_category|
      hash[last_category.full_name[I18n.locale.to_s]] = {
        name: last_category.full_name,
        origin: last_category.to_global_id.to_s,
        resources: last_category.resources,
        children: {}
      }
    end
  end
end

#taxonomiesObject



43
44
45
46
47
48
49
50
# File 'decidim-core/lib/decidim/maintenance/import_models/category.rb', line 43

def taxonomies
  {
    name:,
    origin: to_global_id.to_s,
    children: sub_taxonomy_children,
    resources:
  }
end