From ccf5c6fddedf1e3494ef8d098333920681b8cd39 Mon Sep 17 00:00:00 2001 From: Tom Greenwood <101816158+greenwoodt@users.noreply.github.com> Date: Mon, 9 Dec 2024 09:47:24 +0100 Subject: [PATCH] Update open data exports for Debates (#13698) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 --- decidim-debates/config/locales/en.yml | 5 +- .../lib/decidim/debates/debate_serializer.rb | 28 ++++++++--- .../decidim/debates/debate_serializer_spec.rb | 50 ++++++++++++++++++- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/decidim-debates/config/locales/en.yml b/decidim-debates/config/locales/en.yml index dad46111a78c5..795029d889aa8 100644 --- a/decidim-debates/config/locales/en.yml +++ b/decidim-debates/config/locales/en.yml @@ -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 diff --git a/decidim-debates/lib/decidim/debates/debate_serializer.rb b/decidim-debates/lib/decidim/debates/debate_serializer.rb index b039231da4987..4d7322a339d2d 100644 --- a/decidim-debates/lib/decidim/debates/debate_serializer.rb +++ b/decidim-debates/lib/decidim/debates/debate_serializer.rb @@ -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 @@ -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 @@ -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 diff --git a/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb b/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb index 40b8af925331e..1b345cbe8d11d 100644 --- a/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb +++ b/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb @@ -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:) @@ -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 @@ -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 @@ -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