diff --git a/app/models/track.rb b/app/models/track.rb new file mode 100644 index 00000000..41e673b4 --- /dev/null +++ b/app/models/track.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Track < ApplicationRecord + belongs_to :user + + validates :path, :started_at, :ended_at, presence: true +end diff --git a/app/models/user.rb b/app/models/user.rb index b3112130..90ff2fb0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/config/database.yml b/config/database.yml index fca7a51c..79ad2b3b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,5 +1,5 @@ default: &default - adapter: postgresql + adapter: postgis encoding: unicode database: <%= ENV['DATABASE_NAME'] %> username: <%= ENV['DATABASE_USERNAME'] %> diff --git a/db/migrate/20250123145954_create_tracks.rb b/db/migrate/20250123145954_create_tracks.rb new file mode 100644 index 00000000..168d2c12 --- /dev/null +++ b/db/migrate/20250123145954_create_tracks.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f865b48d..a85c60bd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/factories/tracks.rb b/spec/factories/tracks.rb new file mode 100644 index 00000000..32603460 --- /dev/null +++ b/spec/factories/tracks.rb @@ -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 diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb new file mode 100644 index 00000000..051b8ae8 --- /dev/null +++ b/spec/models/track_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 398e436f..a9ce1d1e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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