Add tracks model

This commit is contained in:
Eugene Burmakin 2025-01-23 16:03:21 +01:00
parent 63b92f695f
commit 774de9991b
8 changed files with 62 additions and 11 deletions

7
app/models/track.rb Normal file
View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Track < ApplicationRecord
belongs_to :user
validates :path, :started_at, :ended_at, presence: true
end

View file

@ -13,7 +13,8 @@ class User < ApplicationRecord
has_many :visits, dependent: :destroy
has_many :points, through: :imports
has_many :places, through: :visits
has_many :trips, dependent: :destroy
has_many :trips, dependent: :destroy
has_many :tracks, dependent: :destroy
after_create :create_api_key
before_save :strip_trailing_slashes

View file

@ -1,5 +1,5 @@
default: &default
adapter: postgresql
adapter: postgis
encoding: unicode
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USERNAME'] %>

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
class CreateTracks < ActiveRecord::Migration[8.0]
def change
create_table :tracks do |t|
t.datetime :started_at, null: false
t.datetime :ended_at, null: false
t.references :user, null: false, foreign_key: true
t.line_string :path, srid: 3785, null: false
t.timestamps
end
end
end

21
db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_01_23_145155) do
ActiveRecord::Schema[8.0].define(version: 2025_01_23_145954) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "postgis"
@ -178,14 +178,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_145155) do
t.index ["visit_id"], name: "index_points_on_visit_id"
end
create_table "spatial_ref_sys", primary_key: "srid", id: :integer, default: nil, force: :cascade do |t|
t.string "auth_name", limit: 256
t.integer "auth_srid"
t.string "srtext", limit: 2048
t.string "proj4text", limit: 2048
t.check_constraint "srid > 0 AND srid <= 998999", name: "spatial_ref_sys_srid_check"
end
create_table "stats", force: :cascade do |t|
t.integer "year", null: false
t.integer "month", null: false
@ -201,6 +193,16 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_145155) do
t.index ["year"], name: "index_stats_on_year"
end
create_table "tracks", force: :cascade do |t|
t.datetime "started_at", null: false
t.datetime "ended_at", null: false
t.bigint "user_id", null: false
t.geometry "path", limit: {:srid=>3785, :type=>"line_string"}, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_tracks_on_user_id"
end
create_table "trips", force: :cascade do |t|
t.string "name", null: false
t.datetime "started_at", null: false
@ -261,6 +263,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_145155) do
add_foreign_key "points", "users"
add_foreign_key "points", "visits"
add_foreign_key "stats", "users"
add_foreign_key "tracks", "users"
add_foreign_key "trips", "users"
add_foreign_key "visits", "areas"
add_foreign_key "visits", "places"

10
spec/factories/tracks.rb Normal file
View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :track do
started_at { DateTime.new(2025, 1, 23, 15, 59, 55) }
ended_at { DateTime.new(2025, 1, 23, 16, 0, 0) }
user
path { 'LINESTRING(0 0, 1 1, 2 2)' }
end
end

15
spec/models/track_spec.rb Normal file
View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Track, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:path) }
it { is_expected.to validate_presence_of(:started_at) }
it { is_expected.to validate_presence_of(:ended_at) }
end
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
end

View file

@ -14,6 +14,7 @@ RSpec.describe User, type: :model do
it { is_expected.to have_many(:visits).dependent(:destroy) }
it { is_expected.to have_many(:places).through(:visits) }
it { is_expected.to have_many(:trips).dependent(:destroy) }
it { is_expected.to have_many(:tracks).dependent(:destroy) }
end
describe 'callbacks' do