Draft of the GPX export feature

This commit is contained in:
Eugene Burmakin 2024-09-02 18:32:21 +02:00
parent 0cc5ac0860
commit 41f0a713ea
5 changed files with 27 additions and 15 deletions

File diff suppressed because one or more lines are too long

View file

@ -12,7 +12,7 @@ class ExportsController < ApplicationController
export_name = "export_from_#{params[:start_at].to_date}_to_#{params[:end_at].to_date}"
export = current_user.exports.create(name: export_name, status: :created)
ExportJob.perform_later(export.id, params[:start_at], params[:end_at])
ExportJob.perform_later(export.id, params[:start_at], params[:end_at], format: params[:format])
redirect_to exports_url, notice: 'Export was successfully initiated. Please wait until it\'s finished.'
rescue StandardError => e

View file

@ -3,9 +3,9 @@
class ExportJob < ApplicationJob
queue_as :exports
def perform(export_id, start_at, end_at)
def perform(export_id, start_at, end_at, format: :json)
export = Export.find(export_id)
Exports::Create.new(export:, start_at:, end_at:).call
Exports::Create.new(export:, start_at:, end_at:, format:).call
end
end

View file

@ -1,11 +1,12 @@
# frozen_string_literal: true
class Exports::Create
def initialize(export:, start_at:, end_at:)
def initialize(export:, start_at:, end_at:, format: :json)
@export = export
@user = export.user
@start_at = start_at.to_datetime
@end_at = end_at.to_datetime
@format = format
end
def call
@ -18,11 +19,11 @@ class Exports::Create
Rails.logger.debug "====Exporting #{points.size} points"
data = ::ExportSerializer.new(points, user.email).call
file_path = Rails.root.join('public', 'exports', "#{export.name}.json")
file_path = Rails.root.join('public', 'exports', "#{export.name}.#{format}")
File.open(file_path, 'w') { |file| file.write(data) }
export.update!(status: :completed, url: "exports/#{export.name}.json")
export.update!(status: :completed, url: "exports/#{export.name}.#{format}")
create_export_finished_notification
rescue StandardError => e
@ -35,7 +36,7 @@ class Exports::Create
private
attr_reader :user, :export, :start_at, :end_at
attr_reader :user, :export, :start_at, :end_at, :format
def time_framed_points
user
@ -60,4 +61,10 @@ class Exports::Create
content: "Export \"#{export.name}\" failed: #{error.message}, stacktrace: #{error.backtrace.join("\n")}"
).call
end
def process_json_export(points)
end
def process_gpx_export(points)
end
end

View file

@ -3,28 +3,33 @@
<div class="w-full">
<%= form_with url: points_path, method: :get do |f| %>
<div class="flex flex-col md:flex-row md:space-x-4 md:items-end">
<div class="w-full md:w-2/6">
<div class="w-full md:w-3/12">
<div class="flex flex-col space-y-2">
<%= f.label :start_at, class: "text-sm font-semibold" %>
<%= f.datetime_local_field :start_at, class: "rounded-md w-full", value: @start_at %>
</div>
</div>
<div class="w-full md:w-2/6">
<div class="w-full md:w-3/12">
<div class="flex flex-col space-y-2">
<%= f.label :end_at, class: "text-sm font-semibold" %>
<%= f.datetime_local_field :end_at, class: "rounded-md w-full", value: @end_at %>
</div>
</div>
<div class="w-full md:w-2/6">
<div class="w-full md:w-1/12">
<div class="flex flex-col space-y-2">
<%= f.submit "Search", class: "px-4 py-2 bg-blue-500 text-white rounded-md" %>
</div>
</div>
<div class="w-full md:w-2/6">
<div class="w-full md:w-2/12">
<div class="flex flex-col space-y-2 text-center">
<%= link_to 'Export points', exports_path(start_at: @start_at, end_at: @end_at), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This will start background process of exporting points withing timeframe, selected between #{@start_at} and #{@end_at}", turbo_method: :post }, class: "px-4 py-2 bg-green-500 text-white rounded-md" %>
<%= link_to 'Export points', exports_path(start_at: @start_at, end_at: @end_at), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This will start background process of exporting points withing timeframe, selected between #{@start_at} and #{@end_at}", turbo_method: :post }, class: "px-4 py-2 bg-green-500 text-white rounded-md join-item" %>
</div>
</div>
<%# <div class="w-full md:w-2/12"> %>
<%# <div class="flex flex-col space-y-2 text-center"> %>
<%# <%= link_to 'Export as GPX', exports_path(start_at: @start_at, end_at: @end_at, format: :gpx), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure? This will start background process of exporting points withing timeframe, selected between #{@start_at} and #{@end_at}", turbo_method: :post }, class: "px-4 py-2 bg-green-500 text-white rounded-md join-item" %>
<%# </div> %>
<%# </div> %>
</div>
<% end %>