Skip to content

Commit

Permalink
Update open data exports for Debates (decidim#13698)
Browse files Browse the repository at this point in the history
* updated and created (including specs) added

* added followers and the test for them

* added translations for debates

* normalize the tansalations

* followers_count removed as followers already added

* test to check closed_at when is false

* added comments author type and last comment id

* fixed updated_at test

* last_id and last_type refined

* updated transaltions

* renamed the followers count method

* added endorsements count and test

* endorsements_count updated translation

* Update decidim-debates/lib/decidim/debates/debate_serializer.rb

Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com>

* updated spec for follows count

* logic created to retrieve values if a comment is left on a debate

* tests for last comment fields if comments are left on debate and test for no values

* removed unused i18n translations

* updated translations of last_comment_by ans follows_count

* renamed author name and url to reflect user

---------

Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com>
  • Loading branch information
greenwoodt and andreslucena authored Dec 9, 2024
1 parent 3979723 commit ccf5c6f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
5 changes: 4 additions & 1 deletion decidim-debates/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,19 @@ en:
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
endorsements_count: The number of endorsements the debate has
follows_count: 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
last_comment_by: The data of last comment made within the debate
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
start_time: When this debate starts, if it is an open debate and has a limited time
taxonomies: The taxonomies of the project
title: The debate title
updated_at: The date of when the debate was last updated
url: The URL where this debate can be found
statistics:
debates_count: Debates
28 changes: 22 additions & 6 deletions decidim-debates/lib/decidim/debates/debate_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ def serialize
component: { id: component.id },
reference: debate.reference,
comments: debate.comments_count,
followers: debate.follows.size,
follows_count: debate.follows_count,
url:,
last_comment_at: debate.last_comment_at,
last_comment_by: {
**last_comment_by_fields
},
comments_enabled: debate.comments_enabled,
conclusions: debate.conclusions,
closed_at: debate.closed_at
closed_at: debate.closed_at,
created_at: debate.created_at,
updated_at: debate.updated_at,
endorsements_count: debate.endorsements_count
}
end

Expand All @@ -51,6 +57,16 @@ def serialize
attr_reader :debate
alias resource debate

def last_comment_by_fields
return {} unless debate.last_comment_by

{
id: debate.last_comment_by.id,
name: user_name(debate.last_comment_by),
url: user_url(debate.last_comment_by)
}
end

def component
debate.component
end
Expand All @@ -62,16 +78,16 @@ def url
def author_fields
{
id: resource.author.id,
name: author_name(resource.author),
url: author_url(resource.author)
name: user_name(resource.author),
url: user_url(resource.author)
}
end

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

def author_url(author)
def user_url(author)
if author.respond_to?(:nickname)
profile_url(author) # is a Decidim::User or Decidim::UserGroup
else
Expand Down
50 changes: 48 additions & 2 deletions decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Debates
let!(:taxonomies) { create_list(:taxonomy, 2, :with_parent, organization: component.organization) }
let(:participatory_process) { component.participatory_space }
let(:component) { debate.component }
let(:new_debate) { described_class.new(debate) }

before do
debate.update!(taxonomies:)
Expand Down Expand Up @@ -121,8 +122,8 @@ module Debates
expect(serialized).to include(comments: debate.comments_count)
end

it "serializes the followers" do
expect(serialized).to include(followers: debate.follows.size)
it "serializes the number of followers" do
expect(serialized).to include(follows_count: debate.follows_count)
end

it "serializes the url" do
Expand All @@ -137,6 +138,18 @@ module Debates
expect(serialized).to include(comments_enabled: debate.comments_enabled)
end

it "includes the created at" do
expect(serialized).to include(created_at: debate.created_at)
end

it "includes the updated at" do
expect(serialized).to include(updated_at: debate.updated_at)
end

it "serializes the endorsements" do
expect(serialized).to include(endorsements_count: debate.endorsements_count)
end

describe "conclusions and closed at" do
it "does not serializes the conclusion" do
expect(serialized[:conclusions]).to be_nil
Expand All @@ -157,6 +170,39 @@ module Debates
expect(serialized).to include(closed_at: debate.closed_at)
end
end

context "when the debate is not closed" do
let!(:debate) { create(:debate, closed_at: nil) }

it "does not serialize the conclusion" do
expect(serialized[:conclusions]).to be_nil
end

it "does not serialize the closed at" do
expect(serialized[:closed_at]).to be_nil
end
end
end

context "when there is a last comment" do
let(:last_comment_by) { create(:user, name: "User") }
let(:debate) { create(:debate, last_comment_by:) }

it "serializes the last comment by fields" do
expect(serialized[:last_comment_by]).to eq(
id: last_comment_by.id,
name: "User",
url: new_debate.send(:user_url, last_comment_by)
)
end
end

context "when there is no last comment" do
let(:debate) { create(:debate, last_comment_by: nil) }

it "returns no values" do
expect(serialized[:last_comment_by]).to eq({})
end
end
end

Expand Down

0 comments on commit ccf5c6f

Please sign in to comment.