add models for Project, Task, Note, Category

This commit is contained in:
2025-06-11 19:56:30 +02:00
parent 2a458f74d6
commit 9906b291f1
29 changed files with 345 additions and 1 deletions

View File

@@ -103,6 +103,9 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
dotenv (3.1.8)
dotenv-rails (3.1.8)
dotenv (= 3.1.8)
railties (>= 6.1)
drb (2.2.3)
ed25519 (1.4.0)
erb (5.0.1)
@@ -360,6 +363,7 @@ DEPENDENCIES
brakeman
capybara
debug
dotenv-rails
importmap-rails
jbuilder
kamal

View File

@@ -13,6 +13,7 @@ postgres=# ALTER USER <username> CREATEDB;
```
6. `rails db:migrate`, then create a new org:
`rails tenant:create['Demo Corp','demo']` and `rails tenant:migrate`
```sh
rails tenant:create[name,subdomain] # Create a new tenant (organization)

7
app/models/contact.rb Normal file
View File

@@ -0,0 +1,7 @@
class Contact < ApplicationRecord
has_many :phone_numbers
belongs_to :user
validates :name, presence: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
end

3
app/models/note.rb Normal file
View File

@@ -0,0 +1,3 @@
class Note < ApplicationRecord
belongs_to :project
end

View File

@@ -6,6 +6,8 @@ class Organization < ApplicationRecord
after_create :create_schema
before_destroy :drop_schema
has_many :users, dependent: :destroy
has_many :projects, through: :users
def schema_name
"org_#{id.to_s.gsub('-', '_')}"

View File

@@ -0,0 +1,11 @@
class PhoneNumber < ApplicationRecord
belongs_to :contact
enum :type, {
mobile: 0,
home: 1,
work: 2,
fax: 3,
other: 4
}, _prefix: true
validates :number, presence: true, format: { with: /\A\+?[0-9\s\-()]+\z/, message: "must be a valid phone number" }
end

8
app/models/project.rb Normal file
View File

@@ -0,0 +1,8 @@
class Project < ApplicationRecord
belongs_to :user
belongs_to :project_category
has_many :contacts, through: :user
has_many :tasks
has_many :notes
enum business_type: { type1: 0, type2: 1 }
end

View File

@@ -0,0 +1,3 @@
class ProjectCategory < ApplicationRecord
has_many :projects
end

9
app/models/task.rb Normal file
View File

@@ -0,0 +1,9 @@
class Task < ApplicationRecord
belongs_to :creator, class_name: 'User'
belongs_to :reporter, class_name: 'User', optional: true
belongs_to :reportee, class_name: 'User', optional: true
belongs_to :project
enum priority: { low: 0, medium: 1, high: 2 }
enum status: { open: 0, in_progress: 1, closed: 2 }
enum type: { bug: 0, feature: 1, chore: 2 }
end

View File

@@ -1,6 +1,9 @@
class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
belongs_to :organization, optional: false
has_many :projects
has_many :contacts
normalizes :email_address, with: ->(e) { e.strip.downcase }
end

View File

@@ -0,0 +1,11 @@
class CreateProjectCategories < ActiveRecord::Migration[8.0]
def change
create_table :project_categories do |t|
t.string :title
t.text :description
t.integer :order
t.timestamps
end
end
end

View File

@@ -0,0 +1,13 @@
class CreateContacts < ActiveRecord::Migration[8.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.string :address
t.float :geo_long
t.float :geo_lat
t.timestamps
end
end
end

View File

@@ -0,0 +1,11 @@
class CreatePhoneNumbers < ActiveRecord::Migration[8.0]
def change
create_table :phone_numbers do |t|
t.string :phone_number
t.integer :type
t.references :contact, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@@ -0,0 +1,14 @@
class CreateProjects < ActiveRecord::Migration[8.0]
def change
create_table :projects do |t|
t.string :title
t.string :website
t.string :email
t.integer :business_type
t.references :user, null: false, foreign_key: true
t.references :project_category, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@@ -0,0 +1,15 @@
class CreateTasks < ActiveRecord::Migration[8.0]
def change
create_table :tasks do |t|
t.string :title
t.text :description
t.integer :priority
t.integer :status
t.integer :type
t.date :due_date
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@@ -0,0 +1,13 @@
class CreateNotes < ActiveRecord::Migration[8.0]
def change
create_table :notes do |t|
t.string :title
t.text :description
t.boolean :pinned
t.boolean :archived
t.references :project, null: false, foreign_key: true
t.timestamps
end
end
end

88
db/schema.rb generated
View File

@@ -10,9 +10,74 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_06_11_155737) do
ActiveRecord::Schema[8.0].define(version: 2025_06_11_174428) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
create_table "contacts", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "address"
t.float "geo_long"
t.float "geo_lat"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "notes", force: :cascade do |t|
t.string "title"
t.text "description"
t.boolean "pinned"
t.boolean "archived"
t.bigint "project_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_notes_on_project_id"
end
create_table "organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.string "subdomain"
t.string "email_domain"
t.string "logo_url"
t.string "address"
t.string "website"
t.string "phone_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["subdomain"], name: "index_organizations_on_subdomain", unique: true
end
create_table "phone_numbers", force: :cascade do |t|
t.string "phone_number"
t.integer "type"
t.bigint "contact_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["contact_id"], name: "index_phone_numbers_on_contact_id"
end
create_table "project_categories", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "order"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "projects", force: :cascade do |t|
t.string "title"
t.string "website"
t.string "email"
t.integer "business_type"
t.bigint "user_id", null: false
t.bigint "project_category_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_category_id"], name: "index_projects_on_project_category_id"
t.index ["user_id"], name: "index_projects_on_user_id"
end
create_table "sessions", force: :cascade do |t|
t.bigint "user_id", null: false
@@ -23,13 +88,34 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_11_155737) do
t.index ["user_id"], name: "index_sessions_on_user_id"
end
create_table "tasks", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "priority"
t.integer "status"
t.integer "type"
t.date "due_date"
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_tasks_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email_address", null: false
t.string "password_digest", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "organization_id", null: false
t.index ["email_address"], name: "index_users_on_email_address", unique: true
t.index ["organization_id"], name: "index_users_on_organization_id"
end
add_foreign_key "notes", "projects"
add_foreign_key "phone_numbers", "contacts"
add_foreign_key "projects", "project_categories"
add_foreign_key "projects", "users"
add_foreign_key "sessions", "users"
add_foreign_key "tasks", "users"
add_foreign_key "users", "organizations"
end

15
test/fixtures/contacts.yml vendored Normal file
View File

@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
email: MyString
address: MyString
geo_long: 1.5
geo_lat: 1.5
two:
name: MyString
email: MyString
address: MyString
geo_long: 1.5
geo_lat: 1.5

15
test/fixtures/notes.yml vendored Normal file
View File

@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
description: MyText
pinned: false
archived: false
project: one
two:
title: MyString
description: MyText
pinned: false
archived: false
project: two

11
test/fixtures/phone_numbers.yml vendored Normal file
View File

@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
phone_number: MyString
type: 1
contact: one
two:
phone_number: MyString
type: 1
contact: two

11
test/fixtures/project_categories.yml vendored Normal file
View File

@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
description: MyText
order: 1
two:
title: MyString
description: MyText
order: 1

17
test/fixtures/projects.yml vendored Normal file
View File

@@ -0,0 +1,17 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
website: MyString
email: MyString
business_type: 1
user: one
project_category: one
two:
title: MyString
website: MyString
email: MyString
business_type: 1
user: two
project_category: two

19
test/fixtures/tasks.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
description: MyText
priority: 1
status: 1
type: 1
due_date: 2025-06-11
user: one
two:
title: MyString
description: MyText
priority: 1
status: 1
type: 1
due_date: 2025-06-11
user: two

View File

@@ -0,0 +1,7 @@
require "test_helper"
class ContactTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/models/note_test.rb Normal file
View File

@@ -0,0 +1,7 @@
require "test_helper"
class NoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -0,0 +1,7 @@
require "test_helper"
class PhoneNumberTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -0,0 +1,7 @@
require "test_helper"
class ProjectCategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -0,0 +1,7 @@
require "test_helper"
class ProjectTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/models/task_test.rb Normal file
View File

@@ -0,0 +1,7 @@
require "test_helper"
class TaskTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end