From c09a02fa98978a01c84d67b92b29501e6c9e571e Mon Sep 17 00:00:00 2001 From: Jeffrey Hardy Date: Mon, 16 Sep 2024 15:39:23 -0400 Subject: [PATCH] Introduce projects and accesses --- app/models/access.rb | 4 ++++ app/models/bubble.rb | 1 + app/models/project.rb | 6 +++++ app/models/user.rb | 6 ++++- db/migrate/20240916191616_create_projects.rb | 13 +++++++++++ db/migrate/20240916191638_create_accesses.rb | 11 +++++++++ .../20240916193936_add_project_to_bubbles.rb | 10 ++++++++ db/schema.rb | 23 +++++++++++++++++++ test/fixtures/accesses.yml | 11 +++++++++ test/fixtures/bubbles.yml | 20 ++++++++-------- test/fixtures/projects.yml | 4 ++++ test/fixtures/taggings.yml | 10 ++++---- test/models/access_test.rb | 7 ++++++ test/models/project_test.rb | 7 ++++++ 14 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 app/models/access.rb create mode 100644 app/models/project.rb create mode 100644 db/migrate/20240916191616_create_projects.rb create mode 100644 db/migrate/20240916191638_create_accesses.rb create mode 100644 db/migrate/20240916193936_add_project_to_bubbles.rb create mode 100644 test/fixtures/accesses.yml create mode 100644 test/fixtures/projects.yml create mode 100644 test/models/access_test.rb create mode 100644 test/models/project_test.rb diff --git a/app/models/access.rb b/app/models/access.rb new file mode 100644 index 000000000..014c5caee --- /dev/null +++ b/app/models/access.rb @@ -0,0 +1,4 @@ +class Access < ApplicationRecord + belongs_to :project + belongs_to :user +end diff --git a/app/models/bubble.rb b/app/models/bubble.rb index f0d47395d..64fd11e3a 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -1,4 +1,5 @@ class Bubble < ApplicationRecord + belongs_to :project belongs_to :creator, class_name: "User", default: -> { Current.user } has_many :comments, dependent: :destroy diff --git a/app/models/project.rb b/app/models/project.rb new file mode 100644 index 000000000..010b99a08 --- /dev/null +++ b/app/models/project.rb @@ -0,0 +1,6 @@ +class Project < ApplicationRecord + belongs_to :creator, class_name: "User" + belongs_to :account, default: -> { creator&.account } + + has_many :accesses, dependent: :destroy +end diff --git a/app/models/user.rb b/app/models/user.rb index 9a96b31c8..36ce5cb71 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,9 @@ class User < ApplicationRecord has_many :sessions, dependent: :destroy has_secure_password validations: false + has_many :accesses, dependent: :destroy + has_many :projects, through: :accesses + normalizes :email_address, with: ->(value) { value.strip.downcase } def initials @@ -15,7 +18,8 @@ class User < ApplicationRecord def deactivate transaction do - sessions.delete_all + sessions.destroy_all + accesses.destroy_all update! active: false, email_address: deactived_email_address end end diff --git a/db/migrate/20240916191616_create_projects.rb b/db/migrate/20240916191616_create_projects.rb new file mode 100644 index 000000000..6bed67b48 --- /dev/null +++ b/db/migrate/20240916191616_create_projects.rb @@ -0,0 +1,13 @@ +class CreateProjects < ActiveRecord::Migration[8.0] + def change + create_table :projects do |t| + t.references :account, null: false + t.references :creator, null: false + t.string :name, null: false + + t.timestamps + end + + Project.create!(name: "Writebook", creator: User.first) + end +end diff --git a/db/migrate/20240916191638_create_accesses.rb b/db/migrate/20240916191638_create_accesses.rb new file mode 100644 index 000000000..00529d84d --- /dev/null +++ b/db/migrate/20240916191638_create_accesses.rb @@ -0,0 +1,11 @@ +class CreateAccesses < ActiveRecord::Migration[8.0] + def change + create_table :accesses do |t| + t.references :project, null: false + t.references :user, null: false + t.timestamps + + t.index [ :project_id, :user_id ], unique: true + end + end +end diff --git a/db/migrate/20240916193936_add_project_to_bubbles.rb b/db/migrate/20240916193936_add_project_to_bubbles.rb new file mode 100644 index 000000000..58069b98f --- /dev/null +++ b/db/migrate/20240916193936_add_project_to_bubbles.rb @@ -0,0 +1,10 @@ +class AddProjectToBubbles < ActiveRecord::Migration[8.0] + def change + change_table :bubbles do |t| + t.references :project, null: true + end + + Bubble.update_all project_id: Project.first.id + change_column_null :bubbles, :project_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 16ec490fa..53fe3a639 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,17 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema[8.0].define(version: 2024_09_17_174301) do + create_table "accesses", force: :cascade do |t| + t.integer "project_id", null: false + t.integer "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["project_id", "user_id"], name: "index_accesses_on_project_id_and_user_id", unique: true + t.index ["project_id"], name: "index_accesses_on_project_id" + t.index ["user_id"], name: "index_accesses_on_user_id" + end + +>>>>>>> d17053b (Introduce projects and accesses) create_table "accounts", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false @@ -72,6 +83,8 @@ ActiveRecord::Schema[8.0].define(version: 2024_09_17_174301) do t.datetime "updated_at", null: false t.integer "creator_id", null: false t.date "due_on" + t.integer "project_id", null: false + t.index ["project_id"], name: "index_bubbles_on_project_id" end create_table "comments", force: :cascade do |t| @@ -82,6 +95,16 @@ ActiveRecord::Schema[8.0].define(version: 2024_09_17_174301) do t.datetime "updated_at", null: false end + create_table "projects", force: :cascade do |t| + t.integer "account_id", null: false + t.integer "creator_id", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id"], name: "index_projects_on_account_id" + t.index ["creator_id"], name: "index_projects_on_creator_id" + end + create_table "sessions", force: :cascade do |t| t.integer "user_id", null: false t.string "ip_address" diff --git a/test/fixtures/accesses.yml b/test/fixtures/accesses.yml new file mode 100644 index 000000000..cad84c386 --- /dev/null +++ b/test/fixtures/accesses.yml @@ -0,0 +1,11 @@ +writebook_david: + project: writebook + user: david + +writebook_jz: + project: writebook + user: jz + +writebook_kevin: + project: writebook + user: kevin diff --git a/test/fixtures/bubbles.yml b/test/fixtures/bubbles.yml index 8fa2699d2..8b36e466e 100644 --- a/test/fixtures/bubbles.yml +++ b/test/fixtures/bubbles.yml @@ -1,20 +1,22 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: +logo: + project: writebook + creator: david title: The logo isn't big enough body: Make the logo bigger. color: '#ED8008' - creator: :david due_on: <%= 3.days.from_now %> -two: +layout: + project: writebook + creator: david + creator: jz title: Layout is broken body: The page scrolls horizontally on mobile devices. color: '#698F9C' - creator: :david -three: +text: + project: writebook + creator: kevin title: The text is too small body: Increase the font size. - color: '#3B4B59' - creator: :kevin + color: "#3B4B59" diff --git a/test/fixtures/projects.yml b/test/fixtures/projects.yml new file mode 100644 index 000000000..9c837864e --- /dev/null +++ b/test/fixtures/projects.yml @@ -0,0 +1,4 @@ +writebook: + name: Writebook + account: 37signals + creator: david diff --git a/test/fixtures/taggings.yml b/test/fixtures/taggings.yml index 74cb5f482..bd5c2ee6d 100644 --- a/test/fixtures/taggings.yml +++ b/test/fixtures/taggings.yml @@ -1,17 +1,15 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - one: - bubble: one + bubble: logo tag: one two: - bubble: two + bubble: layout tag: two three: - bubble: three + bubble: layout tag: one four: - bubble: three + bubble: text tag: two diff --git a/test/models/access_test.rb b/test/models/access_test.rb new file mode 100644 index 000000000..291f62169 --- /dev/null +++ b/test/models/access_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class AccessTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/project_test.rb b/test/models/project_test.rb new file mode 100644 index 000000000..5df4ca498 --- /dev/null +++ b/test/models/project_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ProjectTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end