Skip to content

Commit

Permalink
Add Metrics to open data zip file (decidim#13709)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreslucena authored Dec 4, 2024
1 parent 74701b1 commit ba6947d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Decidim
module Exporters
class OpenDataMetricSerializer < 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
{
day: resource.day,
metric_type: resource.metric_type,
cumulative: resource.cumulative,
quantity: resource.quantity
}
end
end
end
end
5 changes: 5 additions & 0 deletions decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,11 @@ en:
main: Core
spaces: Spaces
title: Open Data files for %{organization}
metrics:
cumulative: The total number of the metric at this day
day: The day this metric was created
metric_type: The type of the metric
quantity: The number of the metrics for this day
moderated_users:
about: The about field of the user
block_reasons: The reason why the admin user blocked the user
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 @@ -620,6 +620,12 @@ def self.open_data_manifests
collection: ->(organization) { Decidim::UserGroup.where(organization:).confirmed.not_blocked.includes(avatar_attachment: :blob) },
serializer: Decidim::Exporters::OpenDataUserGroupSerializer,
include_in_open_data: true
),
CoreDataManifest.new(
name: :metrics,
collection: ->(organization) { Decidim::Metric.where(organization:) },
serializer: Decidim::Exporters::OpenDataMetricSerializer,
include_in_open_data: true
)
]
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim::Exporters
describe OpenDataMetricSerializer do
subject { described_class.new(resource) }

let(:resource) { create(:metric) }
let(:serialized) { subject.serialize }

describe "#serialize" do
it "includes the day" do
expect(serialized).to include(day: resource.day)
end

it "includes the metric_type" do
expect(serialized).to include(metric_type: resource.metric_type)
end

it "includes the cumulative" do
expect(serialized).to include(cumulative: resource.cumulative)
end

it "includes the quantity" do
expect(serialized).to include(quantity: resource.quantity)
end
end
end
end
20 changes: 20 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 @@ -93,6 +93,26 @@
it_behaves_like "open users data exporter"
end

describe "with metrics" do
let(:resource_file_name) { "metrics" }
let(:resource_title) { "### metrics" }
let!(:resource) { create(:metric, organization:) }
let(:help_lines) do
[
"* day: The day this metric was created",
"* metric_type: The type of the metric"
]
end

include_examples "default open data exporter"

it "includes the resource data" do
expect(data).to include(resource.quantity.to_s)
expect(data).to include(resource.cumulative.to_s)
expect(data).to include(resource.day.to_s)
end
end

describe "with moderations" do
let(:resource_file_name) { "moderations" }
let(:resource_title) { "### moderations" }
Expand Down

0 comments on commit ba6947d

Please sign in to comment.