From 042661215bee396403bad05a2ecd65951144f84e Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Tue, 16 Jul 2024 14:45:45 +0200 Subject: [PATCH 1/7] Add authors to proposals exportation --- .../proposal_serializer_decorator.rb | 58 +++++++++++++++++++ docs/HOW_TO_UPGRADE.md | 6 +- .../proposal_serializer_decorator_spec.rb | 56 ++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb create mode 100644 spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb diff --git a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb new file mode 100644 index 00000000..bfe0b4c4 --- /dev/null +++ b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module Decidim::Proposals::ProposalSerializerDecorator + def self.decorate + Decidim::Proposals::ProposalSerializer.class_eval do + # Add authors to proposal exportation + def serialize + { + id: proposal.id, + category: { + id: proposal.category.try(:id), + name: proposal.category.try(:name) || empty_translatable + }, + scope: { + id: proposal.scope.try(:id), + name: proposal.scope.try(:name) || empty_translatable + }, + participatory_space: { + id: proposal.participatory_space.id, + url: Decidim::ResourceLocatorPresenter.new(proposal.participatory_space).url + }, + component: { id: component.id }, + title: proposal.title, + body: convert_to_plain_text(proposal.body), + address: proposal.address, + latitude: proposal.latitude, + longitude: proposal.longitude, + state: proposal.state.to_s, + reference: proposal.reference, + answer: ensure_translatable(proposal.answer), + supports: proposal.proposal_votes_count, + endorsements: { + total_count: proposal.endorsements.size, + user_endorsements: user_endorsements + }, + comments: proposal.comments_count, + attachments: proposal.attachments.size, + followers: proposal.follows.size, + published_at: proposal.published_at, + url: url, + meeting_urls: meetings, + related_proposals: related_proposals, + is_amend: proposal.emendation?, + original_proposal: { + title: proposal&.amendable&.title, + url: original_proposal_url + }, + # Gencat customization + authors_names: proposal.authors.pluck(:name), + authors_emails: proposal.authors.pluck(:email) + # Gencat customization + } + end + end + end +end + +::Decidim::Proposals::ProposalSerializerDecorator.decorate diff --git a/docs/HOW_TO_UPGRADE.md b/docs/HOW_TO_UPGRADE.md index c972ee52..7212d5c5 100644 --- a/docs/HOW_TO_UPGRADE.md +++ b/docs/HOW_TO_UPGRADE.md @@ -186,7 +186,7 @@ These are custom modules and this is what you have to keep in mind when updating #### 6. Also, there are custom files in the application "participa.gencat.cat". - ##### Modified files: + ##### Modified files: With decorator pattern: @@ -198,6 +198,10 @@ These are custom modules and this is what you have to keep in mind when updating * Override to allow private space users to acces public view * probably removable from Decidim v0.24 + * `decorators/lib/decidim/proposals/proposal_serializer_decorator.rb` + * Override to export proposal emails and names from authors + * probably removable from Decidim v0.28 (remember remove test too) + * `lib/decidim/has_private_users.rb` * Override to allow private space users to acces public view * Could not use a decorator so the whole class has been copied diff --git a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb new file mode 100644 index 00000000..63202503 --- /dev/null +++ b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Decidim + module Proposals + describe ProposalSerializer do + subject do + described_class.new(proposal) + end + + let!(:proposal) { create(:proposal, :accepted, body: body) } + let!(:category) { create(:category, participatory_space: component.participatory_space) } + let!(:scope) { create(:scope, organization: component.participatory_space.organization) } + let(:participatory_process) { component.participatory_space } + let(:component) { proposal.component } + + let!(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) } + let(:meetings) { create_list(:meeting, 2, :published, component: meetings_component) } + + let!(:proposals_component) { create(:component, manifest_name: "proposals", participatory_space: participatory_process) } + let(:other_proposals) { create_list(:proposal, 2, component: proposals_component) } + let(:body) { Decidim::Faker::Localized.localized { ::Faker::Lorem.sentences(number: 3).join("\n") } } + + let(:expected_answer) do + answer = proposal.answer + Decidim.available_locales.each_with_object({}) do |locale, result| + result[locale.to_s] = if answer.is_a?(Hash) + answer[locale.to_s] || "" + else + "" + end + end + end + + before do + proposal.update!(category: category) + proposal.update!(scope: scope) + proposal.link_resources(meetings, "proposals_from_meeting") + proposal.link_resources(other_proposals, "copied_from_component") + end + + describe "#serialize" do + let(:serialized) { subject.serialize } + + it "serializes the authors names" do + expect(serialized).to include(authors_names: proposal.authors.pluck(:name)) + end + + it "serializes the authors emails" do + expect(serialized).to include(authors_email: proposal.authors.pluck(:emails)) + end + end + end + end +end From 4effc9be050a2a9bbadb9d5062745a1512291f09 Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Tue, 16 Jul 2024 14:54:21 +0200 Subject: [PATCH 2/7] Rubocopify --- .../lib/decidim/proposals/proposal_serializer_decorator_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb index 63202503..70233a7f 100644 --- a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb +++ b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb @@ -2,6 +2,7 @@ require "rails_helper" +# rubocop:disable RSpec/MultipleMemoizedHelpers module Decidim module Proposals describe ProposalSerializer do @@ -54,3 +55,4 @@ module Proposals end end end +# rubocop:enable RSpec/MultipleMemoizedHelpers From f929991823d1009d75f5e4a18bc6a3ca16f75c7c Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Tue, 16 Jul 2024 15:33:33 +0200 Subject: [PATCH 3/7] Fix test --- .../lib/decidim/proposals/proposal_serializer_decorator_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb index 70233a7f..8f3466ce 100644 --- a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb +++ b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb @@ -49,7 +49,7 @@ module Proposals end it "serializes the authors emails" do - expect(serialized).to include(authors_email: proposal.authors.pluck(:emails)) + expect(serialized).to include(authors_emails: proposal.authors.pluck(:email)) end end end From 13116835e9b2c28fe4d20bbcba8c2fb1f63e4322 Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Wed, 17 Jul 2024 15:04:52 +0200 Subject: [PATCH 4/7] Refactor decorator --- .../proposal_serializer_decorator.rb | 47 ++----------------- 1 file changed, 4 insertions(+), 43 deletions(-) diff --git a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb index bfe0b4c4..f64ebd4d 100644 --- a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb +++ b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb @@ -3,53 +3,14 @@ module Decidim::Proposals::ProposalSerializerDecorator def self.decorate Decidim::Proposals::ProposalSerializer.class_eval do + alias_method :original_serialize, :serialize + # Add authors to proposal exportation def serialize - { - id: proposal.id, - category: { - id: proposal.category.try(:id), - name: proposal.category.try(:name) || empty_translatable - }, - scope: { - id: proposal.scope.try(:id), - name: proposal.scope.try(:name) || empty_translatable - }, - participatory_space: { - id: proposal.participatory_space.id, - url: Decidim::ResourceLocatorPresenter.new(proposal.participatory_space).url - }, - component: { id: component.id }, - title: proposal.title, - body: convert_to_plain_text(proposal.body), - address: proposal.address, - latitude: proposal.latitude, - longitude: proposal.longitude, - state: proposal.state.to_s, - reference: proposal.reference, - answer: ensure_translatable(proposal.answer), - supports: proposal.proposal_votes_count, - endorsements: { - total_count: proposal.endorsements.size, - user_endorsements: user_endorsements - }, - comments: proposal.comments_count, - attachments: proposal.attachments.size, - followers: proposal.follows.size, - published_at: proposal.published_at, - url: url, - meeting_urls: meetings, - related_proposals: related_proposals, - is_amend: proposal.emendation?, - original_proposal: { - title: proposal&.amendable&.title, - url: original_proposal_url - }, - # Gencat customization + original_serialize.merge({ authors_names: proposal.authors.pluck(:name), authors_emails: proposal.authors.pluck(:email) - # Gencat customization - } + }) end end end From dd62fe3831e177f88964417ed47a200a655bda1a Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Wed, 17 Jul 2024 17:47:43 +0200 Subject: [PATCH 5/7] Rubocopify --- .../lib/decidim/proposals/proposal_serializer_decorator.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb index f64ebd4d..aab6a992 100644 --- a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb +++ b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb @@ -8,9 +8,9 @@ def self.decorate # Add authors to proposal exportation def serialize original_serialize.merge({ - authors_names: proposal.authors.pluck(:name), - authors_emails: proposal.authors.pluck(:email) - }) + authors_names: proposal.authors.pluck(:name), + authors_emails: proposal.authors.pluck(:email) + }) end end end From 419c07b99edfe66b055f54fb12a9b3f71fabed2c Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Mon, 29 Jul 2024 11:08:14 +0200 Subject: [PATCH 6/7] Set only author names in exportation --- .../lib/decidim/proposals/proposal_serializer_decorator.rb | 3 +-- .../decidim/proposals/proposal_serializer_decorator_spec.rb | 4 ---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb index aab6a992..9158e85c 100644 --- a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb +++ b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb @@ -8,8 +8,7 @@ def self.decorate # Add authors to proposal exportation def serialize original_serialize.merge({ - authors_names: proposal.authors.pluck(:name), - authors_emails: proposal.authors.pluck(:email) + authors_names: proposal.authors.pluck(:name) }) end end diff --git a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb index 8f3466ce..1a1977f6 100644 --- a/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb +++ b/spec/decorators/lib/decidim/proposals/proposal_serializer_decorator_spec.rb @@ -47,10 +47,6 @@ module Proposals it "serializes the authors names" do expect(serialized).to include(authors_names: proposal.authors.pluck(:name)) end - - it "serializes the authors emails" do - expect(serialized).to include(authors_emails: proposal.authors.pluck(:email)) - end end end end From 33fe337dee71b1018cc5f872b1c9c6562112e2cf Mon Sep 17 00:00:00 2001 From: Laura Jaime Date: Wed, 4 Sep 2024 09:50:17 +0200 Subject: [PATCH 7/7] Fix decorator for Zeitwerk --- .../lib/decidim/proposals/proposal_serializer_decorator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb index 9158e85c..e22d6f49 100644 --- a/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb +++ b/app/decorators/lib/decidim/proposals/proposal_serializer_decorator.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module Decidim::Proposals::ProposalSerializerDecorator +module Lib::Decidim::Proposals::ProposalSerializerDecorator def self.decorate Decidim::Proposals::ProposalSerializer.class_eval do alias_method :original_serialize, :serialize @@ -15,4 +15,4 @@ def serialize end end -::Decidim::Proposals::ProposalSerializerDecorator.decorate +::Lib::Decidim::Proposals::ProposalSerializerDecorator.decorate