mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Add very basic export feature
This commit is contained in:
parent
fe496a64dc
commit
2e4390f194
8 changed files with 41 additions and 18 deletions
7
app/controllers/export_controller.rb
Normal file
7
app/controllers/export_controller.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class ExportController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
@export = current_user.export_data
|
||||
end
|
||||
end
|
||||
2
app/helpers/export_helper.rb
Normal file
2
app/helpers/export_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module ExportHelper
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
5
app/views/export/index.html.erb
Normal file
5
app/views/export/index.html.erb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<div class="w-full">
|
||||
<div class="mockup-code p-5">
|
||||
<code><%= current_user.export_data %></code>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
Rails.application.routes.draw do
|
||||
get 'export', to: 'export#index'
|
||||
resources :imports
|
||||
resources :stats, only: :index
|
||||
|
||||
|
|
|
|||
14
spec/requests/export_spec.rb
Normal file
14
spec/requests/export_spec.rb
Normal 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
|
||||
Loading…
Reference in a new issue