0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-16 06:40:29 +01:00
discourse-custom-wizard/controllers/custom_wizard/admin/submissions.rb

47 Zeilen
1,3 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-04-13 14:17:22 +02:00
class CustomWizard::AdminSubmissionsController < CustomWizard::AdminController
2020-04-15 02:46:44 +02:00
skip_before_action :preload_json, :check_xhr, only: [:download]
2020-11-03 01:24:20 +01:00
before_action :find_wizard, except: [:index]
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def index
render json: ActiveModel::ArraySerializer.new(
2020-11-03 01:24:20 +01:00
CustomWizard::Wizard.list(current_user),
2020-04-13 14:17:22 +02:00
each_serializer: CustomWizard::BasicWizardSerializer
)
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def show
2020-11-03 01:24:20 +01:00
render_json_dump(
wizard: CustomWizard::BasicWizardSerializer.new(@wizard, root: false),
submissions: build_submissions.as_json
)
2020-04-13 14:17:22 +02:00
end
2021-03-11 07:30:15 +01:00
def download
2020-11-03 01:24:20 +01:00
send_data build_submissions.to_json,
2020-04-15 02:46:44 +02:00
filename: "#{Discourse.current_hostname}-wizard-submissions-#{@wizard.name}.json",
content_type: "application/json",
disposition: "attachment"
2020-04-13 14:17:22 +02:00
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
private
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
def build_submissions
PluginStoreRow.where(plugin_name: "#{@wizard.id}_submissions")
.order('id DESC')
.map do |row|
value = ::JSON.parse(row.value)
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
if user = User.find_by(id: row.key)
username = user.username
else
username = I18n.t('admin.wizard.submissions.no_user', id: row.key)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
value.map do |v|
{ username: username }.merge!(v.except("redirect_to"))
end
end.flatten
2020-04-13 14:17:22 +02:00
end
2021-03-11 07:30:15 +01:00
end