Skip to content

Commit

Permalink
OpenData Export breaks when handling deleted users (decidim#13592)
Browse files Browse the repository at this point in the history
* Include deleted users to Open Data export

* Run linter

* Resolved comments

* Run linter

* Resolved comment

* Added missing translation to fix tests

* Normalize translations

* Resolved comment

---------

Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
  • Loading branch information
andra-panaite and alecslupu authored Nov 15, 2024
1 parent a7bc451 commit 09fcea7
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 6 deletions.
8 changes: 5 additions & 3 deletions decidim-debates/lib/decidim/debates/debate_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ def author_name(author)

def author_url(author)
if author.respond_to?(:nickname)
profile_url(author.nickname) # is a Decidim::User or Decidim::UserGroup
profile_url(author) # 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:)
def profile_url(author)
return "" if author.respond_to?(:deleted?) && author.deleted?

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

def root_url
Expand Down
29 changes: 29 additions & 0 deletions decidim-debates/spec/services/decidim/open_data_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@
it_behaves_like "open data exporter"
end

describe "debates by deleted user" do
let(:resource_file_name) { "debates" }
let(:component) do
create(:debates_component, organization:, published_at: Time.current)
end

let!(:deleted_user) { create(:user, :confirmed, :deleted, organization:) }
let!(:resource) { create(:debate, component:, author: deleted_user) }

let(:second_component) do
create(:debates_component, organization:, published_at: Time.current)
end
let!(:second_resource) { create(:debate, :closed, component: second_component, author: deleted_user) }

let(:resource_title) { "## debates" }
let(:help_lines) do
[
"* id: The unique identifier of the debate",
"* conclusions: The conclusions of the debate if it was closed"
]
end
let(:unpublished_component) do
create(:debates_component, organization:, published_at: nil)
end
let(:unpublished_resource) { create(:debate, component: unpublished_component) }

it_behaves_like "open data exporter"
end

describe "debate_comments" do
let(:resource_file_name) { "debate_comments" }
let(:component) do
Expand Down
1 change: 1 addition & 0 deletions decidim-meetings/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ en:
address: The address of the meeting in case it is in person and has a physical location
attachments: The number of attachments in this meeting
attendees: The number of people attending this meeting
author: The data for the author of this meeting
comments: The number of comments made in this meeting
component: The component that the meeting belongs to
contributions: The number of contributions in this meeting made by the atteendes
Expand Down
37 changes: 37 additions & 0 deletions decidim-meetings/lib/decidim/meetings/meeting_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def initialize(meeting)
def serialize
{
id: meeting.id,
author: {
**author_fields
},
participatory_space: {
id: meeting.participatory_space.id,
url: Decidim::ResourceLocatorPresenter.new(meeting.participatory_space).url
Expand Down Expand Up @@ -53,6 +56,40 @@ def serialize
attr_reader :meeting
alias resource meeting

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

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

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

def profile_url(author)
return "" if author.respond_to?(:deleted?) && author.deleted?

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

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

def host
resource.organization.host
end

def component
meeting.component
end
Expand Down
21 changes: 21 additions & 0 deletions decidim-meetings/spec/services/decidim/open_data_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@
it_behaves_like "open data exporter"
end

describe "meeting created by deleted user" do
let!(:deleted_user) { create(:user, :confirmed, :deleted, organization:) }
let(:resource_file_name) { "meetings" }
let(:component) do
create(:meeting_component, organization:, published_at: Time.current)
end
let!(:resource) { create(:meeting, component:, author: deleted_user) }
let(:resource_title) { "## meetings" }
let(:help_lines) do
[
"* id: The unique identifier of the meeting"
]
end
let(:unpublished_component) do
create(:meeting_component, organization:, published_at: nil)
end
let(:unpublished_resource) { create(:meeting, component: unpublished_component, author: deleted_user) }

it_behaves_like "open data exporter"
end

describe "meeting_comments" do
let(:resource_file_name) { "meeting_comments" }
let(:component) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,18 @@ def author_name(author)

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

def profile_url(nickname)
Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:)
def profile_url(author)
return "" if author.respond_to?(:deleted?) && author.deleted?

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

def meeting_url(meeting)
Expand Down
21 changes: 21 additions & 0 deletions decidim-proposals/spec/services/decidim/open_data_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@
it_behaves_like "open data exporter"
end

describe "proposals by deleted user" do
let(:resource_file_name) { "proposals" }
let(:component) do
create(:proposal_component, organization:, published_at: Time.current)
end
let!(:deleted_user) { create(:user, :confirmed, :deleted, organization:) }
let!(:resource) { create(:proposal, component:, users: [deleted_user]) }
let(:resource_title) { "## proposals" }
let(:help_lines) do
[
"* id: The unique identifier for the proposal"
]
end
let(:unpublished_component) do
create(:proposal_component, organization:, published_at: nil)
end
let(:unpublished_resource) { create(:proposal, component: unpublished_component, users: [deleted_user]) }

it_behaves_like "open data exporter"
end

describe "proposal_comments" do
let(:resource_file_name) { "proposal_comments" }
let(:component) do
Expand Down

0 comments on commit 09fcea7

Please sign in to comment.