This file contains essential information for Claude to work effectively with the Dawarich codebase.
Dawarich is a self-hostable web application built with Ruby on Rails 8.0 that serves as a replacement for Google Timeline (Google Location History). It allows users to track, visualize, and analyze their location data through an interactive web interface.
- Location history tracking and visualization
- Interactive maps with multiple layers (heatmap, points, lines, fog of war)
- Import from various sources (Google Maps Timeline, OwnTracks, Strava, GPX, GeoJSON, photos)
- Export to GeoJSON and GPX formats
- Statistics and analytics (countries visited, distance traveled, etc.)
- Public sharing of monthly statistics with time-based expiration
- Trips management with photo integration
- Areas and visits tracking
- Integration with photo management systems (Immich, Photoprism)
- Framework: Ruby on Rails 8.0
- Database: PostgreSQL with PostGIS extension
- Background Jobs: Sidekiq with Redis
- Authentication: Devise
- Authorization: Pundit
- API Documentation: rSwag (Swagger)
- Monitoring: Prometheus, Sentry
- File Processing: AWS S3 integration
- CSS Framework: Tailwind CSS with DaisyUI components
- JavaScript: Stimulus, Turbo Rails, Hotwired
- Maps: Leaflet.js
- Charts: Chartkick
- Enums over strings: Prefer Rails enums (integer columns) over string columns for status/type fields. Use
enum :field_name, { ... }, prefix: :field_nameto get scoped predicate methods and avoid name collisions. - Turbo first: Follow Rails 8 conventions — use Turbo Frames and Turbo Streams/broadcasts wherever appropriate to avoid full page reloads and provide smooth, in-place UI updates.
- SVGs as files: Never inline SVG markup in views. Instead, save SVGs to
app/assets/svg/iconsand useinline_svg_tag "name.svg"to render them. This keeps views clean and SVGs reusable. Userails_iconsto manage SVG assets and ensure consistent styling.
- Follow rubocop conventions (see
.rubocop.yml) - Rails defaults: convention over configuration
- Prefer Hotwire (Turbo Frames/Streams + Stimulus) over custom JS
- Use importmap for JS dependencies — no npm/yarn
activerecord-postgis-adapter- PostgreSQL PostGIS supportgeocoder- Geocoding servicesrgeo- Ruby Geometric Librarygpx- GPX file processingparallel- Parallel processingsidekiq- Background job processingchartkick- Chart generation
├── app/
│ ├── controllers/ # Rails controllers
│ ├── models/ # ActiveRecord models with PostGIS support
│ ├── views/ # ERB templates
│ ├── services/ # Business logic services
│ ├── jobs/ # Sidekiq background jobs
│ ├── queries/ # Database query objects
│ ├── policies/ # Pundit authorization policies
│ ├── serializers/ # API response serializers
│ ├── javascript/ # Stimulus controllers and JS
│ └── assets/ # CSS and static assets
├── config/ # Rails configuration
├── db/ # Database migrations and seeds
├── docker/ # Docker configuration
├── spec/ # RSpec test suite
└── swagger/ # API documentation
- User: Authentication and user management
- Point: Individual location points with coordinates and timestamps
- Track: Collections of related points forming routes
- Area: Geographic areas drawn by users
- Visit: Detected visits to areas
- Trip: User-defined travel periods with analytics
- Import: Data import operations
- Export: Data export operations
- Stat: Calculated statistics and metrics with public sharing capabilities
- Uses PostGIS for advanced geographic queries
- Implements distance calculations and spatial relationships
- Supports various coordinate systems and projections
- Docker Development: Use
docker-compose -f docker/docker-compose.yml up - DevContainer: VS Code devcontainer support available
- Local Development:
bundle exec rails db:preparebundle exec sidekiq(background jobs)bundle exec bin/dev(main application)
- Username:
demo@dawarich.app - Password:
password
- Framework: RSpec
- System Tests: Capybara + Selenium WebDriver
- E2E Tests: Playwright
- Coverage: SimpleCov
- Factories: FactoryBot
- Mocking: WebMock
bundle exec rspec # Run all specs
bundle exec rspec spec/models/ # Model specs only
npx playwright test # E2E testsWhen writing or modifying tests, always test observable behavior (return values, state changes, side effects) rather than implementation details (which internal methods are called, in what order, with what exact arguments).
Anti-patterns to AVOID:
- Never mock the object under test —
allow(subject).to receive(:internal_method)makes the test a tautology - Never test private methods via
send()— test through the public interface instead; if creating a user triggers a trial, test by creating the user and checkinguser.trial?, not by callinguser.send(:start_trial) - Never use
receive_message_chain—allow(x).to receive_message_chain(:a, :b, :c)breaks on any scope reorder; use real data instead - Avoid over-stubbing — if every collaborator is mocked, the test proves nothing; mock only at external boundaries (HTTP, geocoder, external APIs)
- Don't test wiring without outcomes —
expect(Service).to receive(:new).with(args)only proves a method was called, not that it works; verify the returned data or state change instead - Prefer
have_enqueued_joboverexpect(Job).to receive(:perform_later)— the former tests real ActiveJob integration; the latter just tests a mock - Don't assert on cache key formats or internal metric JSON shapes — test that caching works (2nd call doesn't requery) or that metrics fire, not exact internal formats
- Use real factory data over
allow(user).to receive(:active?).and_return(true)— set the actual user state:create(:user, status: :active)
Good test pattern:
# Test behavior: creating an export enqueues processing
it 'enqueues processing job' do
expect { create(:export, file_type: :points) }.to have_enqueued_job(ExportJob)
endBad test pattern:
# Tests implementation: mocks the callback interaction
it 'enqueues processing job' do
expect(ExportJob).to receive(:perform_later) # mock, not real
build(:export).save!
end- Import Jobs: Process uploaded location data files
- Calculation Jobs: Generate statistics and analytics
- Notification Jobs: Send user notifications
- Photo Processing: Extract EXIF data from photos
Tracks::ParallelGeneratorJob- Generate track data in parallel- Various import jobs for different data sources
- Statistical calculation jobs
Dawarich includes a comprehensive public sharing system that allows users to share their monthly statistics with others without requiring authentication. This feature enables users to showcase their location data while maintaining privacy control through configurable expiration settings.
- Time-based expiration: Share links can expire after 1 hour, 12 hours, 24 hours, or be permanent
- UUID-based access: Each shared stat has a unique, unguessable UUID for security
- Public API endpoints: Hexagon map data can be accessed via API without authentication when sharing is enabled
- Automatic cleanup: Expired shares are automatically inaccessible
- Privacy controls: Users can enable/disable sharing and regenerate sharing URLs at any time
- Database:
sharing_settings(JSONB) andsharing_uuid(UUID) columns onstatstable - Routes:
/shared/month/:uuidfor public viewing,/stats/:year/:month/sharingfor management - API:
/api/v1/maps/hexagonssupports public access viauuidparameter - Controllers:
Shared::StatsControllerhandles public views, sharing management integrated into existing stats flow
- No authentication bypass: Public sharing only exposes specifically designed endpoints
- UUID-based access: Sharing URLs use unguessable UUIDs rather than sequential IDs
- Expiration enforcement: Automatic expiration checking prevents access to expired shares
- Limited data exposure: Only monthly statistics and hexagon data are publicly accessible
- Social sharing: Users can share interesting travel months with friends and family
- Portfolio/showcase: Travel bloggers and photographers can showcase location statistics
- Data collaboration: Researchers can share aggregated location data for analysis
- Public demonstrations: Demo instances can provide public examples without compromising user data
- Framework: rSwag (Swagger/OpenAPI)
- Location:
/api-docsendpoint - Authentication: API key (Bearer) for API access, UUID-based access for public shares
users- User accounts and settingspoints- Location points with PostGIS geometrytracks- Route collectionsareas- User-defined geographic areasvisits- Detected area visitstrips- Travel periodsimports/exports- Data transfer operationsstats- Calculated metrics with sharing capabilities (sharing_settings,sharing_uuid)
- Extensive use of PostGIS geometry types
- Spatial indexes for performance
- Geographic calculations and queries
See .env.template for available configuration options including:
- Database configuration
- Redis settings
- AWS S3 credentials
- External service integrations
- Feature flags
config/database.yml- Database configurationconfig/sidekiq.yml- Background job settingsconfig/schedule.yml- Cron job schedulesdocker/docker-compose.yml- Development environment
- Production:
docker/docker-compose.production.yml - Development:
docker/docker-compose.yml - Multi-stage Docker builds supported
Procfile- Production Heroku deploymentProcfile.dev- Development with ForemanProcfile.production- Production processes
- Ruby Linting: RuboCop with Rails extensions
- JS/CSS Linting: Biome (formatting, lint, import sorting)
- Security: Brakeman, bundler-audit
- Dependencies: Strong Migrations for safe database changes
- Performance: Stackprof for profiling
bundle exec rubocop # Ruby linting
npx @biomejs/biome check --write . # JS/CSS auto-fix (safe fixes)
npx @biomejs/biome check --write --unsafe . # JS/CSS auto-fix (all fixes)
npx @biomejs/biome ci . # JS/CSS CI check (read-only)
bundle exec brakeman # Security scan
bundle exec bundle-audit # Dependency security- Always run RuboCop on modified Ruby files before committing:
bundle exec rubocop <files> - Always run Biome on modified JS/CSS files before committing:
npx @biomejs/biome check --write <files> - If Biome
--writeleaves remaining errors, use--write --unsafeto apply fixes likeparseIntradix andNumber.isNaN - CI runs
biome ci --changed --since=dev— it compares against thedevbranch, notmaster - The
noStaticOnlyClasswarning is acceptable and does not fail CI - Tailwind CSS files (
*.tailwind.css) have@importposition rules disabled inbiome.jsonbecause@tailwinddirectives must come first
Always prefer Turbo + Stimulus over custom JavaScript. This project uses the Hotwire stack (Turbo Drive, Turbo Frames, Turbo Streams, Stimulus) as its primary frontend architecture. Direct fetch() calls, manual DOM manipulation, and standalone JS modules should only be used when Hotwire cannot handle the use case (e.g., map rendering with Leaflet/MapLibre).
When adding frontend behavior, follow this order of preference:
- Turbo Drive — Default. Links and forms work as SPAs with zero JS.
- Turbo Frames — Partial page updates. Wrap a section in
<turbo-frame>and target it from links/forms. - Turbo Streams — Server-pushed DOM updates. Use for CRUD operations that need to update multiple page sections. Respond with
turbo_streamformat from controllers. - Stimulus controller — Client-side behavior that Turbo can't handle (toggles, form validation, UI interactions). Keep controllers thin.
- Direct JS — Last resort. Only for complex map interactions, canvas rendering, or third-party library integration (Leaflet, MapLibre, Chartkick).
For CRUD actions (create, update, destroy), respond with Turbo Streams instead of redirects or JSON:
# Controller
def create
@area = current_user.areas.new(area_params)
if @area.save
respond_to do |format|
format.turbo_stream
format.html { redirect_to areas_path }
end
end
end
# app/views/areas/create.turbo_stream.erb
<%= turbo_stream.prepend "areas-list", partial: "areas/area", locals: { area: @area } %>
<%= stream_flash(:notice, "Area created successfully") %>Use the FlashStreamable concern (included in controllers) to send flash messages via Turbo Streams:
include FlashStreamable
# In turbo_stream responses:
stream_flash(:notice, "Success message")
stream_flash(:error, "Error message")- Server-side (Turbo Stream): Use
stream_flashfrom theFlashStreamableconcern. This appends a flash partial to the#flash-messagescontainer. - Client-side (Stimulus/JS): Import
Flashfromflash_controller.jsand callFlash.show(type, message):import Flash from "./flash_controller" Flash.show("notice", "Operation completed") Flash.show("error", "Something went wrong")
- Never use raw
alert(),console.logfor user-facing messages, or create ad-hoc notification DOM elements.
- Location:
app/javascript/controllers/ - Naming:
<name>_controller.jsmaps todata-controller="<name>"in HTML - Use
static targetsfor DOM references,static valuesfor data from HTML attributes - Always clean up in
disconnect()(event listeners, timers, subscriptions) - Prefer
data-actionattributes in HTML overaddEventListenerin JS - For forms, prefer
this.formTarget.requestSubmit()over manualfetch()calls — this preserves Turbo form handling, CSRF tokens, and Turbo Stream responses
Use the unified upload controller (upload_controller.js) for all file upload forms. Configure via data-upload-*-value attributes:
<%= form_with data: {
controller: "upload",
upload_url_value: rails_direct_uploads_url,
upload_field_name_value: "import[files][]",
upload_multiple_value: true,
upload_target: "form"
} do |f| %>- No
fetch()for form submissions — Useform_withwith Turbo. If you need custom headers (API key), use Stimulus to submit the form viarequestSubmit(). - No
document.getElementById()for updates — Use Turbo Frames/Streams to replace DOM sections server-side. - No
showFlashMessage()or ad-hoc flash functions — UseFlash.show()(client) orstream_flash(server). - No ActionCable subscriptions for CRUD updates — Use Turbo Stream broadcasts from models/controllers instead.
- No separate upload controllers per form — Use the unified
uploadcontroller with value attributes for configuration.
- Map rendering: Leaflet (Maps v1) and MapLibre GL JS (Maps v2) require imperative JS for layers, markers, and interactions.
- Chart rendering: Chartkick handles its own DOM.
- Third-party integrations: Libraries that don't have Hotwire adapters.
- Complex client-side computation: Haversine distance, coordinate transforms, etc.
Even in these cases, wrap the integration in a Stimulus controller and connect it to the DOM via data-controller.
- Location Data: Always handle location data with appropriate precision and privacy considerations
- PostGIS: Leverage PostGIS features for geographic calculations rather than Ruby-based solutions
2.1 Coordinates: Use
lonlatcolumn inpointstable for geographic calculations - Background Jobs: Use Sidekiq for any potentially long-running operations
- Testing: Include both unit and integration tests for location-based features
- Performance: Consider database indexes for geographic queries
- Security: Never log or expose user location data inappropriately
- Migrations: Put all migrations (schema and data) in
db/migrate/, notdb/data/. Data manipulation migrations use the sameActiveRecord::Migrationclass and should run in the standard migration sequence. - Public Sharing: When implementing features that interact with stats, consider public sharing access patterns:
- Use
public_accessible?method to check if a stat can be publicly accessed - Support UUID-based access in API endpoints when appropriate
- Respect expiration settings and disable sharing when expired
- Only expose minimal necessary data in public sharing contexts
- Use
Both Map v1 (Leaflet) and Map v2 (MapLibre) contain an intentional unit mismatch in route drawing that must be preserved for consistency:
The Issue:
haversineDistance()function returns distance in kilometers (e.g., 0.5 km)- Route splitting threshold is stored and compared as meters (e.g., 500)
- The code compares them directly:
0.5 > 500= always FALSE
Result:
- The distance threshold (
meters_between_routessetting) is effectively disabled - Routes only split on time gaps (default: 60 minutes between points)
- This creates longer, more continuous routes that users expect
Code Locations:
-
Map v1:
app/javascript/maps/polylines.js:390- Uses
haversineDistance()frommaps/helpers.js(returns km) - Compares to
distanceThresholdMetersvariable (value in meters)
- Uses
-
Map v2:
app/javascript/maps_maplibre/layers/routes_layer.js:82-104- Has built-in
haversineDistance()method (returns km) - Intentionally skips
/1000conversion to replicate v1 behavior - Comment explains this is matching v1's unit mismatch
- Has built-in
Critical Rules:
- ❌ DO NOT "fix" the unit mismatch - this would break user expectations
- ✅ Keep both versions synchronized - they must behave identically
- ✅ Document any changes - route drawing changes affect all users
⚠️ If you ever fix this bug:- You MUST update both v1 and v2 simultaneously
- You MUST migrate user settings (multiply existing values by 1000 or divide by 1000 depending on direction)
- You MUST communicate the breaking change to users
Additional Route Drawing Details:
- Time threshold: 60 minutes (default) - actually functional
- Distance threshold: 500 meters (default) - currently non-functional due to unit bug
- Sorting: Map v2 sorts points by timestamp client-side; v1 relies on backend ASC order
- API ordering: Map v2 must request
order: 'asc'to match v1's chronological data flow
Dawarich Cloud has a two-tier plan system. Self-hosted instances bypass all plan restrictions (DawarichSettings.self_hosted? returns true, all users effectively have Pro).
- Pro (
plan: :pro, enum value1) — Full access to all features, no data window - Lite (
plan: :lite, enum value0) — Free tier with restricted feature set
Plan is stored as an integer enum on the users table. New cloud users start on Lite via trial flow.
Data visibility window (12 months):
- Lite users only see data from the last 12 months (
DawarichSettings::LITE_DATA_WINDOW) - Implemented as a query-time filter in
PlanScopableconcern (app/models/concerns/plan_scopable.rb) - Scoped methods:
scoped_points,scoped_tracks,scoped_visits,scoped_stats - Data is never deleted — only filtered from UI and API reads. Export uses unscoped
user.pointsetc. plan_restricted?returnstrueonly when!self_hosted? && lite?
Disabled map layers (Pro-only):
- Heatmap, Fog of War, Scratch Map, Globe View
- Lite users get a 20-second timed preview, then auto-hide with upgrade prompt
- Gating logic:
app/javascript/maps_maplibre/utils/layer_gate.js - UI components:
Toast(countdown) andUpgradeBanner(post-preview CTA)
API restrictions:
- Write API returns 403 (
require_write_api!inApiController) - Read API scopes results to 12-month window (
apply_plan_scopeinApiController) - Rate limit: 200 req/hr (Lite) vs 1,000 req/hr (Pro) via
rack-attack(config/initializers/rack_attack.rb)
Disabled features:
- Integrations (Immich, Photoprism)
- Public sharing of stats
- Full digest view
Plan endpoint: GET /api/v1/plan returns current plan and feature flags (Api::V1::PlanController)
Lite::ArchivalWarningJob runs daily for Lite users and sends warnings at three thresholds:
- 11 months — In-app notification warning data will archive in 30 days
- 11.5 months — Email notification
- 12 months — In-app notification that data has been archived (hidden from view)
Warnings are deduped via settings['archival_warnings'] JSONB on the user record.
- Use
user.plan_restricted?to check if restrictions apply (returns false for self-hosted) - Use
user.scoped_*methods instead ofuser.points/user.tracksetc. for plan-aware queries - Use
require_pro_api!orrequire_write_api!before_actions in API controllers - Use
apply_plan_scope(relation)when scoping points that don't start fromuser.points - Frontend: use
isGatedPlan(userPlan)andgatedToggle()fromlayer_gate.jsfor map layer toggling - Export must always use unscoped relations — users can export all their data regardless of plan
- Main Branch:
master - Development:
devbranch for pull requests - Issues: GitHub Issues for bug reports
- Discussions: GitHub Discussions for feature requests
- Community: Discord server for questions
- Documentation: https://dawarich.app/docs/
- Repository: https://github.com/Freika/dawarich
- Discord: https://discord.gg/pHsBjpt5J8
- Changelog: See CHANGELOG.md for version history
- Development Setup: See DEVELOPMENT.md