forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Debates to open data zip file (decidim#13436)
Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
- Loading branch information
1 parent
f65fa39
commit 25f64af
Showing
13 changed files
with
463 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.