diff --git a/app/models/image.rb b/app/models/image.rb index d6ecd36..6443fa1 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..80800c8 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,6 @@ +class Post < ApplicationRecord + belongs_to :user + belongs_to :image + has_many :taggings, as: :taggable + has_many :tags, through: :taggings +end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..c4d1c12 --- /dev/null +++ b/app/models/tag.rb @@ -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 diff --git a/app/models/tagging.rb b/app/models/tagging.rb new file mode 100644 index 0000000..e608dcd --- /dev/null +++ b/app/models/tagging.rb @@ -0,0 +1,4 @@ +class Tagging < ApplicationRecord + belongs_to :tag + belongs_to :taggable, polymorphic: true +end diff --git a/app/models/user.rb b/app/models/user.rb index d67da20..15bd0f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,6 @@ class User < ApplicationRecord has_secure_password + has_many :posts + + validates :name, :status, :email, :password_digest, presence: true end diff --git a/db/migrate/20250515134517_create_posts.rb b/db/migrate/20250515134517_create_posts.rb new file mode 100644 index 0000000..a118eb8 --- /dev/null +++ b/db/migrate/20250515134517_create_posts.rb @@ -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 diff --git a/db/migrate/20250515134851_create_tags.rb b/db/migrate/20250515134851_create_tags.rb new file mode 100644 index 0000000..391a7fc --- /dev/null +++ b/db/migrate/20250515134851_create_tags.rb @@ -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 diff --git a/db/migrate/20250515135623_create_taggings.rb b/db/migrate/20250515135623_create_taggings.rb new file mode 100644 index 0000000..505d687 --- /dev/null +++ b/db/migrate/20250515135623_create_taggings.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index e9f3ea9..edbe99a 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[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 diff --git a/test/fixtures/files/mermaid_template.png b/test/fixtures/files/mermaid_template.png new file mode 100644 index 0000000..6028901 Binary files /dev/null and b/test/fixtures/files/mermaid_template.png differ diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..7f12185 --- /dev/null +++ b/test/fixtures/posts.yml @@ -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: diff --git a/test/fixtures/taggings.yml b/test/fixtures/taggings.yml new file mode 100644 index 0000000..b8346f1 --- /dev/null +++ b/test/fixtures/taggings.yml @@ -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 diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml new file mode 100644 index 0000000..73a1e08 --- /dev/null +++ b/test/fixtures/tags.yml @@ -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 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 6956558..687c436 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -4,4 +4,10 @@ one: name: testuser status: test email: test@email.com - password_digest: <%= BCrypt::Password.create('testpassword') %> \ No newline at end of file + password_digest: <%= BCrypt::Password.create('testpassword') %> + +two: + name: testuser2 + status: dead + email: tester2@email.com + password_digest: <%= BCrypt::Password.create('tpass2') %> \ No newline at end of file diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..ff155c4 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..1846cdb --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/tagging_test.rb b/test/models/tagging_test.rb new file mode 100644 index 0000000..cb5040f --- /dev/null +++ b/test/models/tagging_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TaggingTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end