Browse Source

#2 ぼちぼち

main
みてるぞ 4 months ago
parent
commit
137c86bba7
18 changed files with 156 additions and 31 deletions
  1. +6
    -0
      backend/app/models/ip_address.rb
  2. +17
    -0
      backend/app/models/nico_tag_relation.rb
  3. +2
    -0
      backend/app/models/post.rb
  4. +14
    -0
      backend/app/models/tag.rb
  5. +12
    -0
      backend/app/models/user.rb
  6. +2
    -2
      backend/db/migrate/20250227211400_create_posts.rb
  7. +2
    -2
      backend/db/migrate/20250227211500_create_post_tags.rb
  8. +2
    -2
      backend/db/migrate/20250227214100_create_wiki_pages.rb
  9. +18
    -18
      backend/db/schema.rb
  10. +11
    -0
      backend/test/fixtures/ip_addresses.yml
  11. +11
    -0
      backend/test/fixtures/nico_tag_relations.yml
  12. +9
    -7
      backend/test/fixtures/posts.yml
  13. +11
    -0
      backend/test/fixtures/tags.yml
  14. +11
    -0
      backend/test/fixtures/users.yml
  15. +7
    -0
      backend/test/models/ip_address_test.rb
  16. +7
    -0
      backend/test/models/nico_tag_relation_test.rb
  17. +7
    -0
      backend/test/models/tag_test.rb
  18. +7
    -0
      backend/test/models/user_test.rb

+ 6
- 0
backend/app/models/ip_address.rb View File

@@ -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

+ 17
- 0
backend/app/models/nico_tag_relation.rb View File

@@ -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

+ 2
- 0
backend/app/models/post.rb View File

@@ -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

+ 14
- 0
backend/app/models/tag.rb View File

@@ -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

+ 12
- 0
backend/app/models/user.rb View File

@@ -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

+ 2
- 2
backend/db/migrate/20250227211400_create_posts.rb View File

@@ -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


+ 2
- 2
backend/db/migrate/20250227211500_create_post_tags.rb View File

@@ -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


+ 2
- 2
backend/db/migrate/20250227214100_create_wiki_pages.rb View File

@@ -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


+ 18
- 18
backend/db/schema.rb View File

@@ -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

+ 11
- 0
backend/test/fixtures/ip_addresses.yml View File

@@ -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

+ 11
- 0
backend/test/fixtures/nico_tag_relations.yml View File

@@ -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

+ 9
- 7
backend/test/fixtures/posts.yml View File

@@ -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

+ 11
- 0
backend/test/fixtures/tags.yml View File

@@ -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

+ 11
- 0
backend/test/fixtures/users.yml View File

@@ -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

+ 7
- 0
backend/test/models/ip_address_test.rb View File

@@ -0,0 +1,7 @@
require "test_helper"

class IpAddressTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

+ 7
- 0
backend/test/models/nico_tag_relation_test.rb View File

@@ -0,0 +1,7 @@
require "test_helper"

class NicoTagRelationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

+ 7
- 0
backend/test/models/tag_test.rb View File

@@ -0,0 +1,7 @@
require "test_helper"

class TagTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

+ 7
- 0
backend/test/models/user_test.rb View File

@@ -0,0 +1,7 @@
require "test_helper"

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

Loading…
Cancel
Save