Class: Decidim::Maintenance::ImportModels::Scope

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#full_nameObject



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

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

  @full_name ||= name.dup
  @full_name[I18n.locale.to_s] = "#{parent.full_name[I18n.locale.to_s]} > #{@full_name[I18n.locale.to_s]}" if parent_ids.count > 2
  @full_name
end

Class Method Details

.all_filtersObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 138

def self.all_filters
  manifests = []
  filters = []
  Scope.participatory_space_classes.each do |space_class|
    manifests << space_class.to_s.split("::").last.underscore.pluralize
    # 1 filter per participatory space as space_filter=true
    # 1 filter per component as space_filter=false on those with scopes, suffix: "Component Name"
    # Only for components with scopes
    space_class.where(organization:).each do |space|
      space.components.each do |component|
        component_filter = filter_item_for_component(component, space)
        filters << component_filter if component_filter
      end
    end
  end
  filters << filter_item_for_space_manifest(manifests)
  filters
end

.all_taxonomiesObject



157
158
159
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 157

def self.all_taxonomies
  all_in_org.where(parent_id: nil).to_h { |scope| [scope.name[I18n.locale.to_s], scope.taxonomies] }
end

.filter_item_for_component(component, space) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 120

def self.filter_item_for_component(component, space)
  return unless component.settings.respond_to?(:taxonomy_filters)

  scopes_enabled = component.attributes.dig("settings", "global", "scopes_enabled")
  scope_id = component.attributes.dig("settings", "global", "scope_id")
  return unless scopes_enabled

  scope = find_by(id: scope_id) || (space.scopes_enabled? && find_by(id: space.decidim_scope_id))
  list = scope ? [scope] + scope.all_children : all

  {
    name: root_taxonomy_name,
    internal_name: "#{root_taxonomy_name}: #{component.name[I18n.locale.to_s]}",
    items: list.map(&:all_names),
    components: [component.to_global_id.to_s]
  }
end

.filter_item_for_space_manifest(participatory_space_manifests) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 103

def self.filter_item_for_space_manifest(participatory_space_manifests)
  items = []
  all_in_org.where(parent_id: nil).find_each do |scope|
    items << [scope.name[I18n.locale.to_s]]
    scope.all_children.map(&:all_names).each do |names|
      items << names
    end
  end

  {
    participatory_space_manifests:,
    name: root_taxonomy_name,
    items:,
    components: []
  }
end

.root_taxonomy_nameObject



20
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 20

def self.root_taxonomy_name = "~ #{I18n.t("decidim.admin.titles.scopes")}"

Instance Method Details

#all_childrenObject



56
57
58
59
60
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 56

def all_children
  @all_children ||= children.map do |child|
    [child] + child.all_children
  end.flatten
end

#all_namesObject



46
47
48
49
50
51
52
53
54
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 46

def all_names
  names = parent_ids.map do |id|
    Scope.find_by(id:).name[I18n.locale.to_s]
  end + [name[I18n.locale.to_s]]

  return names if names.count < 4

  names[0..1] + [names[2..].join(" > ")]
end

#children_taxonomiesObject



62
63
64
65
66
67
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 62

def children_taxonomies
  # next level is going to be too deep, we transform the children into siblings
  return sibling_taxonomies if parent_ids.count.positive?

  children.to_h { |child| [child.full_name[I18n.locale.to_s], child.taxonomies] }
end

#parent_idsObject



42
43
44
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 42

def parent_ids
  @parent_ids ||= parent_id ? parent.parent_ids + [parent_id] : []
end

#resourcesObject



92
93
94
95
96
97
98
99
100
101
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 92

def resources
  Scope.resource_classes.each_with_object({}) do |klass, hash|
    items = if klass.to_s == "Decidim::InitiativesTypeScope"
              klass.where(decidim_scopes_id: id)
            else
              klass.where(decidim_scope_id: id)
            end
    hash.merge!(items.to_h { |resource| [resource.to_global_id.to_s, resource_name(resource)] })
  end
end

#sibling_taxonomiesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'decidim-core/lib/decidim/maintenance/import_models/scope.rb', line 69

def sibling_taxonomies
  all_children.to_h do |child|
    [
      child.full_name[I18n.locale.to_s],
      {
        name: child.full_name,
        origin: child.to_global_id.to_s,
        children: {},
        resources: child.resources
      }
    ]
  end
end

#taxonomiesObject



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

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