diff --git a/app/models/area.rb b/app/models/area.rb index 748a3866..49758f5a 100644 --- a/app/models/area.rb +++ b/app/models/area.rb @@ -2,6 +2,7 @@ class Area < ApplicationRecord belongs_to :user + has_many :visits, dependent: :destroy validates :name, :latitude, :longitude, :radius, presence: true end diff --git a/app/models/point.rb b/app/models/point.rb index 9bd22f52..63328b9f 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -4,6 +4,7 @@ class Point < ApplicationRecord reverse_geocoded_by :latitude, :longitude belongs_to :import, optional: true + belongs_to :visit, optional: true belongs_to :user validates :latitude, :longitude, :timestamp, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index b4ab7a85..4e9fd7f7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,7 @@ class User < ApplicationRecord has_many :exports, dependent: :destroy has_many :notifications, dependent: :destroy has_many :areas, dependent: :destroy + has_many :visits, dependent: :destroy after_create :create_api_key diff --git a/app/models/visit.rb b/app/models/visit.rb new file mode 100644 index 00000000..053efa37 --- /dev/null +++ b/app/models/visit.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Visit < ApplicationRecord + belongs_to :area + belongs_to :user +end diff --git a/db/migrate/20240721183005_create_visits.rb b/db/migrate/20240721183005_create_visits.rb new file mode 100644 index 00000000..2f97cda1 --- /dev/null +++ b/db/migrate/20240721183005_create_visits.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CreateVisits < ActiveRecord::Migration[7.1] + def change + create_table :visits do |t| + t.references :area, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20240721183116_add_visit_id_to_points.rb b/db/migrate/20240721183116_add_visit_id_to_points.rb new file mode 100644 index 00000000..2cefd507 --- /dev/null +++ b/db/migrate/20240721183116_add_visit_id_to_points.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddVisitIdToPoints < ActiveRecord::Migration[7.1] + def change + add_reference :points, :visit, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index c0c24ba3..3a774e33 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[7.1].define(version: 2024_07_21_165313) do +ActiveRecord::Schema[7.1].define(version: 2024_07_21_183116) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -121,6 +121,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_21_165313) do t.datetime "updated_at", null: false t.bigint "user_id" t.jsonb "geodata", default: {}, null: false + t.bigint "visit_id" t.index ["altitude"], name: "index_points_on_altitude" t.index ["battery"], name: "index_points_on_battery" t.index ["battery_status"], name: "index_points_on_battery_status" @@ -133,6 +134,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_21_165313) do t.index ["timestamp"], name: "index_points_on_timestamp" t.index ["trigger"], name: "index_points_on_trigger" t.index ["user_id"], name: "index_points_on_user_id" + t.index ["visit_id"], name: "index_points_on_visit_id" end create_table "stats", force: :cascade do |t| @@ -166,10 +168,22 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_21_165313) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + create_table "visits", force: :cascade do |t| + t.bigint "area_id", null: false + t.bigint "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["area_id"], name: "index_visits_on_area_id" + t.index ["user_id"], name: "index_visits_on_user_id" + end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "areas", "users" add_foreign_key "notifications", "users" add_foreign_key "points", "users" + add_foreign_key "points", "visits" add_foreign_key "stats", "users" + add_foreign_key "visits", "areas" + add_foreign_key "visits", "users" end diff --git a/spec/factories/visits.rb b/spec/factories/visits.rb new file mode 100644 index 00000000..4c94fb26 --- /dev/null +++ b/spec/factories/visits.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :visit do + area { nil } + user { nil } + end +end diff --git a/spec/models/area_spec.rb b/spec/models/area_spec.rb index 128e0550..2a6346bc 100644 --- a/spec/models/area_spec.rb +++ b/spec/models/area_spec.rb @@ -5,6 +5,7 @@ require 'rails_helper' RSpec.describe Area, type: :model do describe 'associations' do it { is_expected.to belong_to(:user) } + it { is_expected.to have_many(:visits).dependent(:destroy) } end describe 'validations' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e2bdab6a..0dcd1c97 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -11,6 +11,7 @@ RSpec.describe User, type: :model do it { is_expected.to have_many(:exports).dependent(:destroy) } it { is_expected.to have_many(:notifications).dependent(:destroy) } it { is_expected.to have_many(:areas).dependent(:destroy) } + it { is_expected.to have_many(:visits).dependent(:destroy) } end describe 'callbacks' do diff --git a/spec/models/visit_spec.rb b/spec/models/visit_spec.rb new file mode 100644 index 00000000..423d7e89 --- /dev/null +++ b/spec/models/visit_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Visit, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end