diff --git a/backend/app/models/ip_address.rb b/backend/app/models/ip_address.rb new file mode 100644 index 0000000..e5e01a3 --- /dev/null +++ b/backend/app/models/ip_address.rb @@ -0,0 +1,6 @@ +class IpAddress < ApplicationRecord + validates :ip_address, presence: true, length: { maximum: 16 } + validates :banned, inclusion: { in: [true, false] } + + has_many :users +end diff --git a/backend/app/models/nico_tag_relation.rb b/backend/app/models/nico_tag_relation.rb new file mode 100644 index 0000000..63ab362 --- /dev/null +++ b/backend/app/models/nico_tag_relation.rb @@ -0,0 +1,17 @@ +class NicoTagRelation < ApplicationRecord + belongs_to :nico_tag, class_name: 'Tag', foreign_key: 'nico_tag_id' + belongs_to :tag, class_name: 'Tag', foreign_key: 'tag_id' + + validates :nico_tag_id, presence: true + validates :tag_id, prosence: true + + validate :nico_tag_must_be_nico + + private + + def nico_tag_must_be_nico + if nico_tag && nico_tag.category != :nico + errors.add :nico_tag_id, 'タグのカテゴリがニコニコである必要があります.' + end + end +end diff --git a/backend/app/models/post.rb b/backend/app/models/post.rb index b2a8b46..11adf44 100644 --- a/backend/app/models/post.rb +++ b/backend/app/models/post.rb @@ -1,2 +1,4 @@ class Post < ApplicationRecord + belongs_to :parent, class_name: 'Post', optional: true, foreign_key: 'parent_id' + belongs_to :user, foreign_key: '' end diff --git a/backend/app/models/tag.rb b/backend/app/models/tag.rb new file mode 100644 index 0000000..6560449 --- /dev/null +++ b/backend/app/models/tag.rb @@ -0,0 +1,14 @@ +class Tag < ApplicationRecord + validates :name, presence: true, length: { maximum: 255 } + validates :category, presence: true, inclusion: { in: categories.keys } + + enum category: { deerjikist: 'deerjikist', + meme: 'meme', + character: 'character', + general: 'general', + material: 'material', + nico: 'nico', + meta: 'meta' } + + scope :nico_tags, -> { where(category: :nico) } +end diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb new file mode 100644 index 0000000..97d8f3b --- /dev/null +++ b/backend/app/models/user.rb @@ -0,0 +1,12 @@ +class User < ApplicationRecord + validates :name, length: { maximum: 255 } + validates :inheritance_code, presence: true, length: { maximum: 64 } + validates :role, presence: true, inclusion: { in: roles.keys } + validates :banned, inclusion: { in: [true, false] } + + enum role: { guest: 'guest', member: 'member', admin: 'admin' } + + has_many :posts + has_many :settings + has_many :ip_addresses +end diff --git a/backend/db/migrate/20250227211400_create_posts.rb b/backend/db/migrate/20250227211400_create_posts.rb index 7c083fa..7866325 100644 --- a/backend/db/migrate/20250227211400_create_posts.rb +++ b/backend/db/migrate/20250227211400_create_posts.rb @@ -4,8 +4,8 @@ class CreatePosts < ActiveRecord::Migration[7.0] t.string :url, limit: 2000, null: false t.string :thumbnail, limit: 255, null: false t.string :thumbnail_base, limit: 2000, null: false - t.references :post, foreign_key: { to_table: :posts } - t.references :uploaded_by, foreign_key: { to_table: :users } + t.references :parent, foreign_key: { to_table: :posts } + t.references :uploaded_user, foreign_key: { to_table: :users } t.timestamps end end diff --git a/backend/db/migrate/20250227211500_create_post_tags.rb b/backend/db/migrate/20250227211500_create_post_tags.rb index 4b2eab1..35b796c 100644 --- a/backend/db/migrate/20250227211500_create_post_tags.rb +++ b/backend/db/migrate/20250227211500_create_post_tags.rb @@ -3,8 +3,8 @@ class CreatePostTags < ActiveRecord::Migration[7.0] create_table :post_tags do |t| t.references :post, null: false, foreign_key: { to_table: :posts } t.references :tag, null: false, foreign_key: { to_table: :tags } - t.references :created_by, foreign_key: { to_table: :users } - t.references :deleted_by, foreign_key: { to_table: :users } + t.references :created_user, foreign_key: { to_table: :users } + t.references :deleted_user, foreign_key: { to_table: :users } t.timestamps end end diff --git a/backend/db/migrate/20250227214100_create_wiki_pages.rb b/backend/db/migrate/20250227214100_create_wiki_pages.rb index b11cba5..9b6ddb8 100644 --- a/backend/db/migrate/20250227214100_create_wiki_pages.rb +++ b/backend/db/migrate/20250227214100_create_wiki_pages.rb @@ -3,8 +3,8 @@ class CreateWikiPages < ActiveRecord::Migration[7.0] create_table :wiki_pages do |t| t.string :title, limit: 255, null: false t.references :tag, foreign_key: { to_table: :tags } - t.references :created_by, null: false, foreign_key: { to_table: :users } - t.references :updated_by, null: false, foreign_key: { to_table: :users } + t.references :created_user, null: false, foreign_key: { to_table: :users } + t.references :updated_user, null: false, foreign_key: { to_table: :users } t.timestamps end end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 04a4b54..c05d7ec 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -30,12 +30,12 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_27_214100) do create_table "post_tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "post_id", null: false t.bigint "tag_id", null: false - t.bigint "created_by_id" - t.bigint "deleted_by_id" + t.bigint "created_user_id" + t.bigint "deleted_user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["created_by_id"], name: "index_post_tags_on_created_by_id" - t.index ["deleted_by_id"], name: "index_post_tags_on_deleted_by_id" + t.index ["created_user_id"], name: "index_post_tags_on_created_user_id" + t.index ["deleted_user_id"], name: "index_post_tags_on_deleted_user_id" t.index ["post_id"], name: "index_post_tags_on_post_id" t.index ["tag_id"], name: "index_post_tags_on_tag_id" end @@ -44,12 +44,12 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_27_214100) do t.string "url", limit: 2000, null: false t.string "thumbnail", null: false t.string "thumbnail_base", limit: 2000, null: false - t.bigint "post_id" - t.bigint "uploaded_by_id" + t.bigint "parent_id" + t.bigint "uploaded_user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["post_id"], name: "index_posts_on_post_id" - t.index ["uploaded_by_id"], name: "index_posts_on_uploaded_by_id" + t.index ["parent_id"], name: "index_posts_on_parent_id" + t.index ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id" end create_table "settings", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| @@ -107,23 +107,23 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_27_214100) do create_table "wiki_pages", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "title", null: false t.bigint "tag_id" - t.bigint "created_by_id", null: false - t.bigint "updated_by_id", null: false + t.bigint "created_user_id", null: false + t.bigint "updated_user_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["created_by_id"], name: "index_wiki_pages_on_created_by_id" + t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id" t.index ["tag_id"], name: "index_wiki_pages_on_tag_id" - t.index ["updated_by_id"], name: "index_wiki_pages_on_updated_by_id" + t.index ["updated_user_id"], name: "index_wiki_pages_on_updated_user_id" end add_foreign_key "nico_tag_relations", "tags" add_foreign_key "nico_tag_relations", "tags", column: "nico_tag_id" add_foreign_key "post_tags", "posts" add_foreign_key "post_tags", "tags" - add_foreign_key "post_tags", "users", column: "created_by_id" - add_foreign_key "post_tags", "users", column: "deleted_by_id" - add_foreign_key "posts", "posts" - add_foreign_key "posts", "users", column: "uploaded_by_id" + add_foreign_key "post_tags", "users", column: "created_user_id" + add_foreign_key "post_tags", "users", column: "deleted_user_id" + add_foreign_key "posts", "posts", column: "parent_id" + add_foreign_key "posts", "users", column: "uploaded_user_id" add_foreign_key "settings", "users" add_foreign_key "tag_aliases", "tags" add_foreign_key "user_ips", "ip_addresses" @@ -131,6 +131,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_27_214100) do add_foreign_key "user_post_views", "posts" add_foreign_key "user_post_views", "users" add_foreign_key "wiki_pages", "tags" - add_foreign_key "wiki_pages", "users", column: "created_by_id" - add_foreign_key "wiki_pages", "users", column: "updated_by_id" + add_foreign_key "wiki_pages", "users", column: "created_user_id" + add_foreign_key "wiki_pages", "users", column: "updated_user_id" end diff --git a/backend/test/fixtures/ip_addresses.yml b/backend/test/fixtures/ip_addresses.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/backend/test/fixtures/ip_addresses.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/backend/test/fixtures/nico_tag_relations.yml b/backend/test/fixtures/nico_tag_relations.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/backend/test/fixtures/nico_tag_relations.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/backend/test/fixtures/posts.yml b/backend/test/fixtures/posts.yml index 6028a9c..d7a3329 100644 --- a/backend/test/fixtures/posts.yml +++ b/backend/test/fixtures/posts.yml @@ -1,9 +1,11 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - title: MyString - body: MyText - -two: - title: MyString - body: MyText +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/backend/test/fixtures/tags.yml b/backend/test/fixtures/tags.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/backend/test/fixtures/tags.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/backend/test/fixtures/users.yml b/backend/test/fixtures/users.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/backend/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/backend/test/models/ip_address_test.rb b/backend/test/models/ip_address_test.rb new file mode 100644 index 0000000..ffd3980 --- /dev/null +++ b/backend/test/models/ip_address_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class IpAddressTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/backend/test/models/nico_tag_relation_test.rb b/backend/test/models/nico_tag_relation_test.rb new file mode 100644 index 0000000..668fea3 --- /dev/null +++ b/backend/test/models/nico_tag_relation_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class NicoTagRelationTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/backend/test/models/tag_test.rb b/backend/test/models/tag_test.rb new file mode 100644 index 0000000..1846cdb --- /dev/null +++ b/backend/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/backend/test/models/user_test.rb b/backend/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/backend/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end