Skip to content

Commit

Permalink
Add Debates to open data zip file (decidim#13436)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
  • Loading branch information
andreslucena and alecslupu authored Oct 18, 2024
1 parent f65fa39 commit 25f64af
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 42 deletions.
2 changes: 1 addition & 1 deletion decidim-core/app/services/decidim/open_data_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def data_for_participatory_space(export_manifest)

def get_help_definition(manifest_type, exporter, export_manifest)
help_definition[manifest_type] = {} if help_definition[manifest_type].nil?
help_definition[manifest_type][export_manifest.name] = {}
help_definition[manifest_type][export_manifest.name] = {} if help_definition[manifest_type][export_manifest.name].blank?
exporter.headers_without_locales.each do |header|
help_definition[manifest_type][export_manifest.name][header] = I18n.t("decidim.open_data.help.#{export_manifest.name}.#{header}")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def permissions
# The public part needs to be implemented yet
return permission_action if permission_action.scope != :admin

can_export_comments?
can_export_debates?

return permission_action if permission_action.subject != :debate

Expand All @@ -30,8 +30,8 @@ def debate
@debate ||= context.fetch(:debate, nil)
end

def can_export_comments?
allow! if permission_action.subject == :comments && permission_action.action == :export
def can_export_debates?
allow! if permission_action.subject == :debates && permission_action.action == :export
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="item_show__header">
<h1 class="item_show__header-title">
<%= t(".title") %>
<%= export_dropdown if allowed_to? :export, :comments %>
<%= export_dropdown if allowed_to? :export, :debates %>
<%= link_to t("actions.new", scope: "decidim.debates"), new_debate_path, class: "button button__sm button__secondary" if allowed_to? :create, :debate %>
<%= render partial: "decidim/admin/components/resource_action" %>
</h1>
Expand Down
39 changes: 38 additions & 1 deletion decidim-debates/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ en:
invalid: There was a problem updating this debate.
success: Debate successfully updated.
exports:
comments: Comments
debate_comments: Comments
debates: Debates
admin_log:
debate:
close: "%{user_name} closed the %{resource_name} debate on the %{space_name} space"
Expand Down Expand Up @@ -224,5 +225,41 @@ en:
description: Number of debates created
object: debates
title: Debates
open_data:
help:
debate_comments:
alignment: If this comment was a favour, against or neutral
author: The name of the participant that made this comment
body: The comment itself
commentable_id: The unique id of the commentable
commentable_type: The type of the commentable (if it was a result, a proposal, etc.)
created_at: The date when this comment was created
depth: The place where this comment is in the three of comments (if it is an answer or an answer of an answer)
id: The id for this comment
locale: The locale (language) that the participant had when leaving this comment
root_commentable_url: The URL of the resource that ties to this comment
user_group: The name of the user group that made this comment (if any)
debates:
author: The data for the author of this debate
category: The category of the debate (e.g. "Culture")
closed_at: The date when this debate was closed
comments: The number of comments this debate has
comments_enabled: Wether this debate has comments enabled or not
component: The component that the debate belongs to
conclusions: The conclusions of the debate if it was closed
created_at: The date and time when the debate was created
description: The debate description
end_time: When this debate ends, if it is an open debate and has a limited time
followers: The number of followers this debate has
id: The unique identifier of the debate
information_updates: The updates that the author has made to the debate
instructions: Which are the instructions to comment in this debate
last_comment_at: The date when this debate was commented by the last time
participatory_space: To which space (e.g. Participatory Process, or Assembly) this debate belongs to
reference: The unique identifier of the resource in this platform
scope: The scope of the debate (e.g. "City")
start_time: When this debate starts, if it is an open debate and has a limited time
title: The debate title
url: The URL where this debate can be found
statistics:
debates_count: Debates
1 change: 1 addition & 0 deletions decidim-debates/lib/decidim/debates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
module Decidim
# Base module for this engine.
module Debates
autoload :DebateSerializer, "decidim/debates/debate_serializer"
end
end
19 changes: 17 additions & 2 deletions decidim-debates/lib/decidim/debates/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,28 @@

component.actions = %w(create endorse comment)

component.exports :comments do |exports|
component.exports :debates do |exports|
exports.collection do |component_instance|
Decidim::Debates::Debate
.not_hidden
.where(component: component_instance)
.includes(:scope, :category, component: { participatory_space: :organization })
end

exports.include_in_open_data = true

exports.serializer Decidim::Debates::DebateSerializer
end

component.exports :debate_comments do |exports|
exports.collection do |component_instance|
Decidim::Comments::Export.comments_for_resource(
Decidim::Debates::Debate, component_instance
)
).includes(:author, :user_group, root_commentable: { component: { participatory_space: :organization } })
end

exports.include_in_open_data = true

exports.serializer Decidim::Comments::CommentSerializer
end

Expand Down
99 changes: 99 additions & 0 deletions decidim-debates/lib/decidim/debates/debate_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

module Decidim
module Debates
# This class serializes a Debate so can be exported to CSV, JSON or other
# formats.
class DebateSerializer < Decidim::Exporters::Serializer
include Decidim::ApplicationHelper
include Decidim::ResourceHelper

# Public: Initializes the serializer with a debate.
def initialize(debate)
@debate = debate
end

# Public: Exports a hash with the serialized data for this debate.
def serialize
{
id: debate.id,
author: {
**author_fields
},
title: debate.title,
description: debate.description,
instructions: debate.instructions,
start_time: debate.start_time,
end_time: debate.end_time,
information_updates: debate.information_updates,
category: {
id: debate.category.try(:id),
name: debate.category.try(:name)
},
scope: {
id: debate.scope.try(:id),
name: debate.scope.try(:name)
},
participatory_space: {
id: debate.participatory_space.id,
url: Decidim::ResourceLocatorPresenter.new(debate.participatory_space).url
},
component: { id: component.id },
reference: debate.reference,
comments: debate.comments_count,
followers: debate.follows.size,
url:,
last_comment_at: debate.last_comment_at,
comments_enabled: debate.comments_enabled,
conclusions: debate.conclusions,
closed_at: debate.closed_at
}
end

private

attr_reader :debate
alias resource debate

def component
debate.component
end

def url
Decidim::ResourceLocatorPresenter.new(debate).url
end

def author_fields
{
id: resource.author.id,
name: author_name(resource.author),
url: author_url(resource.author)
}
end

def author_name(author)
translated_attribute(author.name)
end

def author_url(author)
if author.respond_to?(:nickname)
profile_url(author.nickname) # is a Decidim::User or Decidim::UserGroup
else
root_url # is a Decidim::Organization
end
end

def profile_url(nickname)
Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:)
end

def root_url
Decidim::Core::Engine.routes.url_helpers.root_url(host:)
end

def host
resource.organization.host
end
end
end
end
Loading

0 comments on commit 25f64af

Please sign in to comment.