Add very basic export feature

This commit is contained in:
Eugene Burmakin 2024-03-23 21:46:18 +01:00
parent fe496a64dc
commit 2e4390f194
8 changed files with 41 additions and 18 deletions

View file

@ -0,0 +1,7 @@
class ExportController < ApplicationController
before_action :authenticate_user!
def index
@export = current_user.export_data
end
end

View file

@ -0,0 +1,2 @@
module ExportHelper
end

View file

@ -13,25 +13,9 @@ class Point < ApplicationRecord
after_create :async_reverse_geocode
def tracked_at
Time.at(timestamp).strftime('%Y-%m-%d %H:%M:%S')
end
private
def async_reverse_geocode
ReverseGeocodingJob.perform_later(id)
end
end
def group_records_by_hour(records)
grouped_records = Hash.new { |hash, key| hash[key] = [] }
records.each do |record|
# Round timestamp to the nearest hour
rounded_time = Time.at(record.timestamp).beginning_of_hour
grouped_records[rounded_time] << record
end
grouped_records
end

View file

@ -8,6 +8,16 @@ class User < ApplicationRecord
has_many :points, through: :imports
has_many :stats
def export_data
excepted_attributes = %w[raw_data id created_at updated_at country city import_id]
{
email => {
'dawarich-export' => self.points.map { _1.attributes.except(*excepted_attributes) }
}
}.to_json
end
def total_km
Stat.where(user: self).sum(:distance)
end
@ -19,5 +29,4 @@ class User < ApplicationRecord
def total_cities
Stat.where(user: self).pluck(:toponyms).flatten.size
end
end

View file

@ -0,0 +1,5 @@
<div class="w-full">
<div class="mockup-code p-5">
<code><%= current_user.export_data %></code>
</div>
</div>

View file

@ -33,8 +33,9 @@
<summary>
<%= "#{current_user.email}" %>
</summary>
<ul class="p-2 bg-base-100 rounded-t-none">
<ul class="p-2 bg-base-100 rounded-t-none z-10">
<li><%= link_to 'Settings', edit_user_registration_path %></li>
<li><%= link_to 'Your data', export_path %></li>
<li><%= link_to 'Logout', destroy_user_session_path, method: :delete, data: { turbo_method: :delete } %></li>
</ul>
</details>

View file

@ -1,4 +1,5 @@
Rails.application.routes.draw do
get 'export', to: 'export#index'
resources :imports
resources :stats, only: :index

View file

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe "Exports", type: :request do
describe "GET /create" do
before do
sign_in create(:user)
end
it "returns http success" do
get "/export"
expect(response).to have_http_status(:success)
end
end
end