Skip to content

Commit ca3cece

Browse files
[Subevent] Allow for downloading subevents as CSV (#11756)
## Summary of the problem One of our partners wants to use the v3 API to fetch information regarding each of their subevents. Right now, there's no way for them to easily retrieve a list of subevents with their IDs. ## Describe your changes This PR adds a CSV export to the org's Subevents page. The CSV export includes ID, Name, Slug, and Balance. <img width="957" height="190" alt="image" src="https://github.com/user-attachments/assets/d42225fd-74c3-408e-8418-2a9620862a1e" /> Example: <img width="514" height="100" alt="image" src="https://github.com/user-attachments/assets/0e4c46b7-7b78-4b94-b0f9-5967fdc2a562" /> [The Orpheus Foundation's sub-organizations.csv](https://github.com/user-attachments/files/22566814/The.Orpheus.Foundation.s.sub-organizations.csv) We should maybe also consider adding a subevents route to the v3 API, but in the meantime, this will do. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent b614f66 commit ca3cece

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

app/controllers/events_controller.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,13 +819,34 @@ def employees
819819
def sub_organizations
820820
authorize @event
821821

822-
search = params[:q] || params[:search]
822+
respond_to do |format|
823+
format.html do
824+
search = params[:q] || params[:search]
825+
826+
relation = @event.subevents
827+
relation = relation.where("name ILIKE ?", "%#{search}%") if search.present?
828+
relation = relation.order(created_at: :desc)
829+
830+
@sub_organizations = relation
831+
end
823832

824-
relation = @event.subevents
825-
relation = relation.where("name ILIKE ?", "%#{search}%") if search.present?
826-
relation = relation.order(created_at: :desc)
833+
# CSV export intentionally does not consider filters
834+
format.csv do
835+
csv = CSV.generate(headers: true) do |csv|
836+
# We include the public ID because our partners iterate this CSV to
837+
# access organizations via the V3 API. The public ID serves has a
838+
# robust, immutable identifier compared to slugs.
839+
csv << %w[ID Name Slug Balance]
840+
841+
@event.subevents.find_each do |e|
842+
csv << [e.public_id, e.name, e.slug, e.balance_v2_cents / 100.0]
843+
end
844+
end
845+
846+
send_data csv, filename: "#{@event.name}'s sub-organizations.csv", type: "text/csv", disposition: :attachment
847+
end
848+
end
827849

828-
@sub_organizations = relation
829850
end
830851

831852
def create_sub_organization

app/views/events/sub_organizations.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
<h1 class="heading flex">
66
<span class="flex-grow">Sub-organizations</span>
7+
<%= pop_icon_to "download",
8+
{ format: :csv },
9+
class: "align-middle tooltipped tooltipped--w", "aria-label": "Download all as CSV" %>
710
<%= link_to "#", class: "btn bg-success", data: { behavior: "modal_trigger", modal: "create" }, disabled: !policy(@event).create_sub_organization? do %>
811
<%= inline_icon "plus" %>
912
Create a subsidiary

0 commit comments

Comments
 (0)