Add posts, tags, tagging, to database. Create references to align with new DB model. Add image of mermaid template model.

This commit is contained in:
hannah.dagemark 2025-05-15 16:35:29 +02:00 committed by Hannah Dagemark
commit 8ee5dd9322
17 changed files with 154 additions and 2 deletions

View file

@ -1,5 +1,8 @@
class Image < ApplicationRecord
has_one_attached :file
has_many :taggings, as: :taggable
has_many :tags, through: :taggings
validates :title, :file, presence: true
end

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

@ -0,0 +1,6 @@
class Post < ApplicationRecord
belongs_to :user
belongs_to :image
has_many :taggings, as: :taggable
has_many :tags, through: :taggings
end

5
app/models/tag.rb Normal file
View file

@ -0,0 +1,5 @@
class Tag < ApplicationRecord
has_many :taggings
has_many :posts, through: :taggings, source: :taggable, source_type: "Post"
has_many :images, through: :taggings, source: :taggable, source_type: "Image"
end

4
app/models/tagging.rb Normal file
View file

@ -0,0 +1,4 @@
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :taggable, polymorphic: true
end

View file

@ -1,3 +1,6 @@
class User < ApplicationRecord
has_secure_password
has_many :posts
validates :name, :status, :email, :password_digest, presence: true
end

View file

@ -0,0 +1,14 @@
class CreatePosts < ActiveRecord::Migration[8.0]
def change
create_table :posts do |t|
t.string :title
t.text :content
t.boolean :pinned
t.references :user, null: false, foreign_key: true
t.references :image, null: false, foreign_key: true
t.string :type
t.timestamps
end
end
end

View file

@ -0,0 +1,10 @@
class CreateTags < ActiveRecord::Migration[8.0]
def change
create_table :tags do |t|
t.string :title
t.string :color
t.timestamps
end
end
end

View file

@ -0,0 +1,10 @@
class CreateTaggings < ActiveRecord::Migration[8.0]
def change
create_table :taggings do |t|
t.references :tag, null: false, foreign_key: true
t.references :taggable, polymorphic: true, null: false
t.timestamps
end
end
end

35
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[8.0].define(version: 2025_05_08_131043) do
ActiveRecord::Schema[8.0].define(version: 2025_05_15_135623) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@ -45,6 +45,36 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_08_131043) do
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "content"
t.boolean "pinned"
t.integer "user_id", null: false
t.integer "image_id", null: false
t.string "type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["image_id"], name: "index_posts_on_image_id"
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id", null: false
t.string "taggable_type", null: false
t.integer "taggable_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable"
end
create_table "tags", force: :cascade do |t|
t.string "title"
t.string "color"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "status"
@ -56,4 +86,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_08_131043) do
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 "posts", "images"
add_foreign_key "posts", "users"
add_foreign_key "taggings", "tags"
end

BIN
test/fixtures/files/mermaid_template.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

17
test/fixtures/posts.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
content: MyText
pinned: false
user: one
image: one
type:
two:
title: MyString
content: MyText
pinned: false
user: two
image: two
type:

11
test/fixtures/taggings.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
tag: one
taggable: one
taggable_type: Taggable
two:
tag: two
taggable: two
taggable_type: Taggable

9
test/fixtures/tags.yml vendored Normal file
View file

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

View file

@ -4,4 +4,10 @@ one:
name: testuser
status: test
email: test@email.com
password_digest: <%= BCrypt::Password.create('testpassword') %>
password_digest: <%= BCrypt::Password.create('testpassword') %>
two:
name: testuser2
status: dead
email: tester2@email.com
password_digest: <%= BCrypt::Password.create('tpass2') %>

7
test/models/post_test.rb Normal file
View file

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

7
test/models/tag_test.rb Normal file
View file

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

View file

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