Send an empty CSV file for invalid user segments

We were getting an exception in this case, which was OK I guess since
this shouldn't happen if the application is used in a normal way, but we
can simplify the code a little bit if we make the `recipients` code
return an empty list of users.

Note that the behavior of the `AdminNotification#list_of_recipients` and
`Newsletter#list_of_recipient_emails` methods is now slightly different;
previously they returned `nil` when given an invalid segment recipient,
while now they return an empty array. I haven't found a place where this
change is relevant. For example, in both of these models, the `deliver`
method used to raise an exception when given an invalid segment while
now it doesn't, but we always check the user segment is valid before
calling the `deliver` method anyway, so it doesn't really affect the
application.
This commit is contained in:
Javi Martín
2025-03-06 12:43:08 +01:00
parent 41ca271fcf
commit 90ae03795d
5 changed files with 21 additions and 4 deletions

View File

@@ -307,7 +307,7 @@ describe UserSegments do
recipients = UserSegments.recipients(:invalid_segment)
expect(recipients).to be nil
expect(recipients).to eq []
end
it "does not return any recipients when given a method name as a segment" do
@@ -315,7 +315,7 @@ describe UserSegments do
recipients = UserSegments.recipients(:ancestors)
expect(recipients).to be nil
expect(recipients).to eq []
end
end
@@ -327,6 +327,14 @@ describe UserSegments do
emails = UserSegments.user_segment_emails(:all_users)
expect(emails).to eq ["first@email.com", "last@email.com"]
end
it "returns an empty list when given an invalid segment" do
create(:user, email: "first@email.com")
emails = UserSegments.user_segment_emails(:invalid_segment)
expect(emails).to eq []
end
end
context "Geozones" do