Skip to content

Commit

Permalink
Add Taxonomies to open data file (decidim#13846)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreslucena authored Jan 15, 2025
1 parent 2d639be commit 7fd9478
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions decidim-core/app/services/decidim/open_data_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions decidim-core/lib/decidim/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions decidim-core/spec/services/decidim/open_data_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 7fd9478

Please sign in to comment.