Add Visit model and associations

This commit is contained in:
Eugene Burmakin 2024-07-21 20:32:29 +02:00
parent 254c28ae1d
commit 3fd176ad6e
11 changed files with 56 additions and 1 deletions

View file

@ -2,6 +2,7 @@
class Area < ApplicationRecord
belongs_to :user
has_many :visits, dependent: :destroy
validates :name, :latitude, :longitude, :radius, presence: true
end

View file

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

View file

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

6
app/models/visit.rb Normal file
View file

@ -0,0 +1,6 @@
# frozen_string_literal: true
class Visit < ApplicationRecord
belongs_to :area
belongs_to :user
end

View file

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

View file

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

16
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[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

6
spec/factories/visits.rb Normal file
View file

@ -0,0 +1,6 @@
FactoryBot.define do
factory :visit do
area { nil }
user { nil }
end
end

View file

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

View file

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

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Visit, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end