diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index f193bdd8..0e28d3b4 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -106,6 +106,6 @@ class ImportsController < ApplicationController def validate_points_limit limit_exceeded = PointsLimitExceeded.new(current_user).call - redirect_to new_import_path, alert: 'Points limit exceeded', status: :unprocessable_entity if limit_exceeded + redirect_to imports_path, alert: 'Points limit exceeded', status: :unprocessable_entity if limit_exceeded end end diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index 6a4aaf60..1af33dc1 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -16,8 +16,9 @@ class TripsController < ApplicationController end @photo_sources = @trip.photo_sources - # Trigger calculation jobs if data is missing - Trips::CalculateAllJob.perform_later(@trip.id) unless @trip.path.present? && @trip.distance.present? && @trip.visited_countries.present? + if @trip.path.blank? || @trip.distance.blank? || @trip.visited_countries.blank? + Trips::CalculateAllJob.perform_later(@trip.id) + end end def new diff --git a/app/jobs/data_migrations/set_points_country_ids_job.rb b/app/jobs/data_migrations/set_points_country_ids_job.rb index 85c1a436..1ca9ac42 100644 --- a/app/jobs/data_migrations/set_points_country_ids_job.rb +++ b/app/jobs/data_migrations/set_points_country_ids_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class DataMigrations::SetPointsCountryIdsJob < ApplicationJob queue_as :default diff --git a/app/jobs/data_migrations/start_settings_points_country_ids_job.rb b/app/jobs/data_migrations/start_settings_points_country_ids_job.rb index 7bb13c5f..28185c1c 100644 --- a/app/jobs/data_migrations/start_settings_points_country_ids_job.rb +++ b/app/jobs/data_migrations/start_settings_points_country_ids_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class DataMigrations::StartSettingsPointsCountryIdsJob < ApplicationJob queue_as :default diff --git a/app/models/trip.rb b/app/models/trip.rb index 60fd27cc..64422563 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -32,7 +32,6 @@ class Trip < ApplicationRecord @photo_sources ||= photos.map { _1[:source] }.uniq end - # These methods are now public since they're called from jobs def calculate_path trip_path = Tracks::BuildPath.new(points.pluck(:lonlat)).call diff --git a/app/services/points_limit_exceeded.rb b/app/services/points_limit_exceeded.rb index 97909efc..62f9b821 100644 --- a/app/services/points_limit_exceeded.rb +++ b/app/services/points_limit_exceeded.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class PointsLimitExceeded def initialize(user) @user = user diff --git a/app/views/devise/registrations/_api_key.html.erb b/app/views/devise/registrations/_api_key.html.erb index 64fb732c..c04b7b85 100644 --- a/app/views/devise/registrations/_api_key.html.erb +++ b/app/views/devise/registrations/_api_key.html.erb @@ -6,11 +6,21 @@

Usage examples

+

Dawarich iOS app

+

Provide your instance URL:

+

<%= root_url %>

+ +

And provide your API key:

+

<%= current_user.api_key %>

+
OR
+

OwnTracks

<%= api_v1_owntracks_points_url(api_key: current_user.api_key) %>

+
OR

Overland

<%= api_v1_overland_batches_url(api_key: current_user.api_key) %>

+

<%= link_to "Generate new API key", generate_api_key_path, data: { confirm: "Are you sure? This will invalidate the current API key.", turbo_confirm: "Are you sure?", turbo_method: :post }, class: 'btn btn-primary' %> diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 97c8ce33..5a2c3c20 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -1,9 +1,9 @@ <% content_for :title, 'Account' %>

-
+
-

Edit your account!

+

Edit your account!

<%= render 'devise/registrations/api_key' %> <%= render 'devise/registrations/points_usage' %>
diff --git a/app/views/trips/_countries.html.erb b/app/views/trips/_countries.html.erb index f80415ac..ba6a0b99 100644 --- a/app/views/trips/_countries.html.erb +++ b/app/views/trips/_countries.html.erb @@ -1,9 +1,9 @@ <% if trip.countries.any? %> -

+

<%= "#{trip.countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %>

<% elsif trip.visited_countries.present? %> -

+

<%= "#{trip.visited_countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %>

<% else %> diff --git a/app/views/trips/_distance.html.erb b/app/views/trips/_distance.html.erb index 51d61714..853742f1 100644 --- a/app/views/trips/_distance.html.erb +++ b/app/views/trips/_distance.html.erb @@ -1,6 +1,6 @@ <% if trip.distance.present? %> - <%= trip.distance %> <%= DISTANCE_UNIT %> + <%= trip.distance %> <%= DISTANCE_UNIT %> <% else %> - Calculating... + Calculating... <% end %> diff --git a/app/views/trips/show.html.erb b/app/views/trips/show.html.erb index 85ee31ce..f057ac24 100644 --- a/app/views/trips/show.html.erb +++ b/app/views/trips/show.html.erb @@ -14,7 +14,7 @@
<% else %>
-

+

Countries are being calculated...

diff --git a/config/initializers/01_constants.rb b/config/initializers/01_constants.rb index 1f3203da..d91f9988 100644 --- a/config/initializers/01_constants.rb +++ b/config/initializers/01_constants.rb @@ -17,7 +17,7 @@ NOMINATIM_API_KEY = ENV.fetch('NOMINATIM_API_KEY', nil) NOMINATIM_API_USE_HTTPS = ENV.fetch('NOMINATIM_API_USE_HTTPS', 'true') == 'true' GEOAPIFY_API_KEY = ENV.fetch('GEOAPIFY_API_KEY', nil) -STORE_GEODATA = ENV.fetch('STORE_GEODATA', 'false') == 'true' +STORE_GEODATA = ENV.fetch('STORE_GEODATA', 'true') == 'true' # /Reverse geocoding settings SENTRY_DSN = ENV.fetch('SENTRY_DSN', nil) diff --git a/db/data_schema.rb b/db/data_schema.rb index c470d5b8..ce0074c2 100644 --- a/db/data_schema.rb +++ b/db/data_schema.rb @@ -1 +1 @@ -DataMigrate::Data.define(version: 20250516180933) +DataMigrate::Data.define(version: 20250516181033) diff --git a/db/migrate/20250515190752_create_countries.rb b/db/migrate/20250515190752_create_countries.rb index 5233a34d..e95feb56 100644 --- a/db/migrate/20250515190752_create_countries.rb +++ b/db/migrate/20250515190752_create_countries.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateCountries < ActiveRecord::Migration[8.0] def change create_table :countries do |t| diff --git a/spec/models/country_spec.rb b/spec/models/country_spec.rb index 5a953329..128be05d 100644 --- a/spec/models/country_spec.rb +++ b/spec/models/country_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'rails_helper' RSpec.describe Country, type: :model do diff --git a/spec/services/points_limit_exceeded_spec.rb b/spec/services/points_limit_exceeded_spec.rb new file mode 100644 index 00000000..8edfcad3 --- /dev/null +++ b/spec/services/points_limit_exceeded_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PointsLimitExceeded do + describe '#call' do + subject(:points_limit_exceeded) { described_class.new(user).call } + + let(:user) { create(:user) } + + context 'when app is self-hosted' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(true) + end + + it { is_expected.to be false } + end + + context 'when app is not self-hosted' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(false) + stub_const('DawarichSettings::BASIC_PAID_PLAN_LIMIT', 10) + end + + context 'when user points count is equal to the limit' do + before do + allow(user.points).to receive(:count).and_return(10) + end + + it { is_expected.to be true } + end + + context 'when user points count exceeds the limit' do + before do + allow(user.points).to receive(:count).and_return(11) + end + + it { is_expected.to be true } + end + + context 'when user points count is below the limit' do + before do + allow(user.points).to receive(:count).and_return(9) + end + + it { is_expected.to be false } + end + end + end +end