From 2e4390f194308174c1e3a4687b34bdb167374e14 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 23 Mar 2024 21:46:18 +0100 Subject: [PATCH] Add very basic export feature --- app/controllers/export_controller.rb | 7 +++++++ app/helpers/export_helper.rb | 2 ++ app/models/point.rb | 16 ---------------- app/models/user.rb | 11 ++++++++++- app/views/export/index.html.erb | 5 +++++ app/views/shared/_navbar.html.erb | 3 ++- config/routes.rb | 1 + spec/requests/export_spec.rb | 14 ++++++++++++++ 8 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 app/controllers/export_controller.rb create mode 100644 app/helpers/export_helper.rb create mode 100644 app/views/export/index.html.erb create mode 100644 spec/requests/export_spec.rb diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb new file mode 100644 index 00000000..461ece1c --- /dev/null +++ b/app/controllers/export_controller.rb @@ -0,0 +1,7 @@ +class ExportController < ApplicationController + before_action :authenticate_user! + + def index + @export = current_user.export_data + end +end diff --git a/app/helpers/export_helper.rb b/app/helpers/export_helper.rb new file mode 100644 index 00000000..34acf5f1 --- /dev/null +++ b/app/helpers/export_helper.rb @@ -0,0 +1,2 @@ +module ExportHelper +end diff --git a/app/models/point.rb b/app/models/point.rb index 56543b9e..ec5eda62 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 0a827ed4..8372108a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/export/index.html.erb b/app/views/export/index.html.erb new file mode 100644 index 00000000..037e48a6 --- /dev/null +++ b/app/views/export/index.html.erb @@ -0,0 +1,5 @@ +
+
+ <%= current_user.export_data %> +
+
diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index dc0a57e9..f49dbff1 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -33,8 +33,9 @@ <%= "#{current_user.email}" %> -