Update views and specs

This commit is contained in:
Eugene Burmakin 2025-05-16 19:53:42 +02:00
parent d7f6f95c47
commit c69d4f45f1
16 changed files with 82 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class DataMigrations::SetPointsCountryIdsJob < ApplicationJob
queue_as :default

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class DataMigrations::StartSettingsPointsCountryIdsJob < ApplicationJob
queue_as :default

View file

@ -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

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class PointsLimitExceeded
def initialize(user)
@user = user

View file

@ -6,11 +6,21 @@
<h3 class='text-xl font-bold mt-4'>Usage examples</h3>
<h3 class='text-lg font-bold mt-4'>Dawarich iOS app</h3>
<p>Provide your instance URL:</p>
<p class='mb-2'><code><%= root_url %></code></p>
<p>And provide your API key:</p>
<p><code><%= current_user.api_key %></code></p>
<div class='divider'>OR</div>
<h3 class='text-lg font-bold mt-4'>OwnTracks</h3>
<p><code><%= api_v1_owntracks_points_url(api_key: current_user.api_key) %></code></p>
<div class='divider'>OR</div>
<h3 class='text-lg font-bold mt-4'>Overland</h3>
<p><code><%= api_v1_overland_batches_url(api_key: current_user.api_key) %></code></p>
</p>
<p class='py-2'>
<%= 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' %>

View file

@ -1,9 +1,9 @@
<% content_for :title, 'Account' %>
<div class="hero min-h-content bg-base-200">
<div class="hero-content flex-col lg:flex-row-reverse w-full my-10">
<div class="hero-content flex-col lg:flex-row-reverse w-full my-5">
<div class="text-center lg:text-left">
<h1 class="text-5xl font-bold">Edit your account!</h1>
<h1 class="text-5xl font-bold mb-5">Edit your account!</h1>
<%= render 'devise/registrations/api_key' %>
<%= render 'devise/registrations/points_usage' %>
</div>

View file

@ -1,9 +1,9 @@
<% if trip.countries.any? %>
<p class="text-lg text-base-content/60">
<p class="text-md text-base-content/60">
<%= "#{trip.countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %>
</p>
<% elsif trip.visited_countries.present? %>
<p class="text-lg text-base-content/60">
<p class="text-md text-base-content/60">
<%= "#{trip.visited_countries.join(', ')} (#{trip.distance} #{DISTANCE_UNIT})" %>
</p>
<% else %>

View file

@ -1,6 +1,6 @@
<% if trip.distance.present? %>
<span><%= trip.distance %> <%= DISTANCE_UNIT %></span>
<span class="text-md"><%= trip.distance %> <%= DISTANCE_UNIT %></span>
<% else %>
<span>Calculating...</span>
<span class="text-md">Calculating...</span>
<span class="loading loading-dots loading-sm"></span>
<% end %>

View file

@ -14,7 +14,7 @@
</div>
<% else %>
<div id="trip_countries">
<p class="text-lg text-base-content/60">
<p class="text-md text-base-content/60">
<span>Countries are being calculated...</span>
<span class="loading loading-dots loading-sm"></span>
</p>

View file

@ -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)

View file

@ -1 +1 @@
DataMigrate::Data.define(version: 20250516180933)
DataMigrate::Data.define(version: 20250516181033)

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CreateCountries < ActiveRecord::Migration[8.0]
def change
create_table :countries do |t|

View file

@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Country, type: :model do

View file

@ -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