From 7fd9478b966cb909fe11673697afec40c5dd8eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Pereira=20de=20Lucena?= Date: Wed, 15 Jan 2025 11:56:17 +0100 Subject: [PATCH] Add Taxonomies to open data file (#13846) --- .../open_data_taxonomy_serializer.rb | 30 +++++++++ .../services/decidim/open_data_exporter.rb | 4 +- decidim-core/config/locales/en.yml | 13 ++++ decidim-core/lib/decidim/core.rb | 6 ++ .../open_data_taxonomy_serializer_spec.rb | 62 +++++++++++++++++++ .../decidim/open_data_exporter_spec.rb | 19 ++++++ 6 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 decidim-core/app/serializers/decidim/exporters/open_data_taxonomy_serializer.rb create mode 100644 decidim-core/spec/serializers/decidim/exporters/open_data_taxonomy_serializer_spec.rb diff --git a/decidim-core/app/serializers/decidim/exporters/open_data_taxonomy_serializer.rb b/decidim-core/app/serializers/decidim/exporters/open_data_taxonomy_serializer.rb new file mode 100644 index 0000000000000..d8796536aa03f --- /dev/null +++ b/decidim-core/app/serializers/decidim/exporters/open_data_taxonomy_serializer.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Decidim + module Exporters + class OpenDataTaxonomySerializer < Decidim::Exporters::Serializer + # Public: Initializes the serializer with a resource + def initialize(resource) + @resource = resource + end + + # Public: Exports a hash with the serialized data for this resource. + def serialize + { + id: resource.id, + name: resource.name, + parent_id: resource.parent_id, + weight: resource.weight, + children_count: resource.children_count, + taxonomizations_count: resource.taxonomizations_count, + created_at: resource.created_at, + updated_at: resource.updated_at, + filters_count: resource.filters_count, + filter_items_count: resource.filter_items_count, + part_of: resource.part_of, + is_root: resource.root? + } + end + end + end +end diff --git a/decidim-core/app/services/decidim/open_data_exporter.rb b/decidim-core/app/services/decidim/open_data_exporter.rb index 6dad5d9d33dd4..7ce5ff13e06ce 100644 --- a/decidim-core/app/services/decidim/open_data_exporter.rb +++ b/decidim-core/app/services/decidim/open_data_exporter.rb @@ -69,7 +69,7 @@ def data_for_resource(resource) export_manifest = (core_data_manifests + open_data_component_manifests + open_data_participatory_space_manifests) .select { |manifest| manifest.name == resource.to_sym }.first - case export_manifest.manifest + case export_manifest.respond_to?(:manifest) && export_manifest.manifest when Decidim::ComponentManifest data_for_component(export_manifest).read when Decidim::ParticipatorySpaceManifest @@ -83,7 +83,7 @@ def data_for_component(export_manifest, col_sep = Decidim.default_csv_col_sep) headers = [] collection = [] ActiveRecord::Base.uncached do - components.where(manifest_name: export_manifest.manifest.name).find_each do |component| + components.where(manifest_name: export_manifest.manifest.name).unscope(:order).find_each do |component| export_manifest.collection.call(component).find_in_batches(batch_size: 100) do |batch| serializer = export_manifest.open_data_serializer.nil? ? export_manifest.serializer : export_manifest.open_data_serializer exporter = Decidim::Exporters::CSV.new(batch, serializer) diff --git a/decidim-core/config/locales/en.yml b/decidim-core/config/locales/en.yml index 5de1194ab3a3e..76ac749853422 100644 --- a/decidim-core/config/locales/en.yml +++ b/decidim-core/config/locales/en.yml @@ -1508,6 +1508,19 @@ en: reported_content: The content that has been reported reported_url: The url of the reported resource reports: A list of reasons for which the resource has been reported + taxonomies: + children_count: The count of the children that this taxonomy has + created_at: The date when this taxonomy was created + filter_items_count: The number of items filters that are using this taxonomy + filters_count: The number of filters that is using this taxonomy + id: The unique identifier of this taxonomy + is_root: True if this taxonomy do not have any parent + name: The name of this taxonomy + parent_id: The unique identifiers of this taxonomy parent (if any) + part_of: Used to detect if this taxonomy is part of another taxonomy + taxonomizations_count: The count of the resources that use this taxonomy + updated_at: The date when this taxonomy was updated for the last time + weight: The order in which this taxonomy is shown user_groups: avatar_url: The avatar of the user group badge: The badge of the user group diff --git a/decidim-core/lib/decidim/core.rb b/decidim-core/lib/decidim/core.rb index 0df9e5e68dcd7..e5e1de24b1620 100644 --- a/decidim-core/lib/decidim/core.rb +++ b/decidim-core/lib/decidim/core.rb @@ -626,6 +626,12 @@ def self.open_data_manifests collection: ->(organization) { Decidim::Metric.where(organization:) }, serializer: Decidim::Exporters::OpenDataMetricSerializer, include_in_open_data: true + ), + CoreDataManifest.new( + name: :taxonomies, + collection: ->(organization) { Decidim::Taxonomy.where(organization:) }, + serializer: Decidim::Exporters::OpenDataTaxonomySerializer, + include_in_open_data: true ) ] end diff --git a/decidim-core/spec/serializers/decidim/exporters/open_data_taxonomy_serializer_spec.rb b/decidim-core/spec/serializers/decidim/exporters/open_data_taxonomy_serializer_spec.rb new file mode 100644 index 0000000000000..4631094d26907 --- /dev/null +++ b/decidim-core/spec/serializers/decidim/exporters/open_data_taxonomy_serializer_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "spec_helper" + +module Decidim::Exporters + describe OpenDataTaxonomySerializer do + subject { described_class.new(resource) } + + let(:resource) { create(:taxonomy) } + let(:serialized) { subject.serialize } + + describe "#serialize" do + it "includes the id" do + expect(serialized).to include(id: resource.id) + end + + it "includes the name" do + expect(serialized).to include(name: resource.name) + end + + it "includes the parent_id" do + expect(serialized).to include(parent_id: resource.parent_id) + end + + it "includes the weight" do + expect(serialized).to include(weight: resource.weight) + end + + it "includes the children_count" do + expect(serialized).to include(children_count: resource.children_count) + end + + it "includes the taxonomizations_count" do + expect(serialized).to include(taxonomizations_count: resource.taxonomizations_count) + end + + it "includes the created_at" do + expect(serialized).to include(created_at: resource.created_at) + end + + it "includes the updated_at" do + expect(serialized).to include(updated_at: resource.updated_at) + end + + it "includes the filters_count" do + expect(serialized).to include(filters_count: resource.filters_count) + end + + it "includes the filter_items_count" do + expect(serialized).to include(filter_items_count: resource.filter_items_count) + end + + it "includes the part_of" do + expect(serialized).to include(part_of: resource.part_of) + end + + it "includes the is_root" do + expect(serialized).to include(is_root: resource.root?) + end + end + end +end diff --git a/decidim-core/spec/services/decidim/open_data_exporter_spec.rb b/decidim-core/spec/services/decidim/open_data_exporter_spec.rb index f655517a0f5ed..28574630db797 100644 --- a/decidim-core/spec/services/decidim/open_data_exporter_spec.rb +++ b/decidim-core/spec/services/decidim/open_data_exporter_spec.rb @@ -113,6 +113,25 @@ end end + describe "with taxonomies" do + let(:resource_file_name) { "taxonomies" } + let(:resource_title) { "### taxonomies" } + let!(:resource) { create(:taxonomy, organization:) } + let(:help_lines) do + [ + "* id: The unique identifier of this taxonomy", + "* name: The name of this taxonomy" + ] + end + + include_examples "default open data exporter" + + it "includes the resource data" do + expect(data).to include(resource.id.to_s) + expect(data).to include(resource.weight.to_s) + end + end + describe "with moderations" do let(:resource_file_name) { "moderations" } let(:resource_title) { "### moderations" }