Class: Mensa::Export
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Mensa::Export
- Defined in:
- app/models/mensa/export.rb
Overview
An export request for a Mensa table. Each export captures the table it was generated for, the view (if any), the requesting user and the request configuration (filters/query/order/page) needed to rebuild the data. Once processed by Mensa::ExportJob the generated CSV is stored in asset.
Constant Summary collapse
- STATUSES =
%w[pending processing completed failed].freeze
- FORMATS =
%w[csv_excel plain_csv].freeze
- SCOPES =
%w[all current_page].freeze
Class Method Summary collapse
- .badge_dom_id(table_name, user) ⇒ Object
-
.broadcast_refresh(table_name, user) ⇒ Object
Re-renders the export button badge and downloads list for everyone subscribed to this table/user’s export stream.
-
.completed_count(table_name, user) ⇒ Object
Number of completed (downloadable) exports for a table/user combination.
- .list_dom_id(table_name, user) ⇒ Object
- .stream_name(table_name, user) ⇒ Object
-
.token(table_name, user) ⇒ Object
A stable, page-independent key identifying the exports of a table/user combination, used for Turbo stream names and DOM ids so background jobs can target them after completion.
Instance Method Summary collapse
- #completed? ⇒ Boolean
-
#downloadable? ⇒ Boolean
True once the asset is ready to be downloaded by the user.
- #failed? ⇒ Boolean
- #pending? ⇒ Boolean
- #processing? ⇒ Boolean
Class Method Details
.badge_dom_id(table_name, user) ⇒ Object
61 62 63 |
# File 'app/models/mensa/export.rb', line 61 def self.badge_dom_id(table_name, user) "mensa-export-badge-#{token(table_name, user)}" end |
.broadcast_refresh(table_name, user) ⇒ Object
Re-renders the export button badge and downloads list for everyone subscribed to this table/user’s export stream. Best-effort: a missing Action Cable backend (or other broadcast failure) must never break the caller (job completion, download cleanup, …).
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/mensa/export.rb', line 73 def self.broadcast_refresh(table_name, user) stream = stream_name(table_name, user) Turbo::StreamsChannel.broadcast_replace_to( stream, target: badge_dom_id(table_name, user), partial: "mensa/exports/badge", locals: {table_name: table_name, user: user} ) Turbo::StreamsChannel.broadcast_replace_to( stream, target: list_dom_id(table_name, user), partial: "mensa/exports/list", locals: {table_name: table_name, user: user, exports: for_table(table_name).for_user(user).recent} ) rescue => e Mensa.config.logger&.warn("Mensa::Export broadcast failed: #{e.class}: #{e.}") end |
.completed_count(table_name, user) ⇒ Object
Number of completed (downloadable) exports for a table/user combination. This is the number rendered in the export button badge.
45 46 47 |
# File 'app/models/mensa/export.rb', line 45 def self.completed_count(table_name, user) for_table(table_name).for_user(user).completed.count end |
.list_dom_id(table_name, user) ⇒ Object
65 66 67 |
# File 'app/models/mensa/export.rb', line 65 def self.list_dom_id(table_name, user) "mensa-export-list-#{token(table_name, user)}" end |
.stream_name(table_name, user) ⇒ Object
57 58 59 |
# File 'app/models/mensa/export.rb', line 57 def self.stream_name(table_name, user) "mensa-exports-#{token(table_name, user)}" end |
.token(table_name, user) ⇒ Object
A stable, page-independent key identifying the exports of a table/user combination, used for Turbo stream names and DOM ids so background jobs can target them after completion.
52 53 54 55 |
# File 'app/models/mensa/export.rb', line 52 def self.token(table_name, user) user_key = user.respond_to?(:id) ? user&.id : user [table_name.to_s, user_key || "anonymous"].join("-").parameterize end |
Instance Method Details
#completed? ⇒ Boolean
22 23 24 |
# File 'app/models/mensa/export.rb', line 22 def completed? status == "completed" end |
#downloadable? ⇒ Boolean
True once the asset is ready to be downloaded by the user.
39 40 41 |
# File 'app/models/mensa/export.rb', line 39 def downloadable? completed? && asset.attached? end |
#failed? ⇒ Boolean
26 27 28 |
# File 'app/models/mensa/export.rb', line 26 def failed? status == "failed" end |
#pending? ⇒ Boolean
34 35 36 |
# File 'app/models/mensa/export.rb', line 34 def pending? status == "pending" end |
#processing? ⇒ Boolean
30 31 32 |
# File 'app/models/mensa/export.rb', line 30 def processing? status == "processing" end |