This commit is contained in:
2025-07-11 02:05:16 +09:00
parent b83fc6369a
commit 3c89d14636
9 changed files with 191 additions and 66 deletions
@@ -0,0 +1,24 @@
class NicoTagsController < ApplicationController
def index
limit = (params[:limit] || 20).to_i
cursor = params[:cursor].presence
q = Tag.nico_tags.includes(:linked_tags)
q = q.where('tags.updated_at < ?', Time.iso8601(cursor)) if cursor
tags = q.limit(limit + 1)
next_cursor = nil
if tags.size > limit
next_cursor = tags.last.updated_at.iso8601(6)
tags = tags.first(limit)
end
render json: { tags: tags.map { |tag|
tag.as_json(include: :linked_tags)
}, next_cursor: }
end
def upload
end
end
+2 -6
View File
@@ -6,14 +6,10 @@ class PostsController < ApplicationController
# GET /posts
def index
limit = (params[:limit] || 20).to_i
cursor = params[:cursor]
cursor = params[:cursor].presence
q = filtered_posts.order(created_at: :desc)
next_cursor = nil
if cursor.present?
q = q.where('posts.created_at < ?', Time.iso8601(cursor))
end
q = q.where('posts.created_at < ?', Time.iso8601(cursor)) if cursor
posts = q.limit(limit + 1)
+8 -2
View File
@@ -1,6 +1,6 @@
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'
belongs_to :nico_tag, class_name: 'Tag'
belongs_to :tag, class_name: 'Tag'
validates :nico_tag_id, presence: true
validates :tag_id, presence: true
@@ -14,4 +14,10 @@ class NicoTagRelation < ApplicationRecord
errors.add :nico_tag_id, 'タグのカテゴリがニコニコである必要があります.'
end
end
def tag_mustnt_be_nico
if tag && tag.category == 'nico'
errors.add :tag_id, '連携先タグのカテゴリはニコニコであってはなりません.'
end
end
end
+11 -3
View File
@@ -2,7 +2,14 @@ class Tag < ApplicationRecord
has_many :post_tags, dependent: :destroy
has_many :posts, through: :post_tags
has_many :tag_aliases, dependent: :destroy
has_many :wiki_pages, dependent: :nullify
has_many :nico_tag_relations, foreign_key: :nico_tag_id, dependent: :destroy
has_many :linked_tags, through: :nico_tag_relations, source: :tag
has_many :reversed_nico_tag_relations, class_name: 'NicoTagRelation',
foreign_key: :tag_id,
dependent: :destroy
has_many :linked_nico_tags, through: :reversed_nico_tag_relations, source: :nico_tag
enum :category, { deerjikist: 'deerjikist',
meme: 'meme',
@@ -17,7 +24,7 @@ class Tag < ApplicationRecord
validate :nico_tag_name_must_start_with_nico
scope :nico_tags, -> { where category: :nico }
scope :nico_tags, -> { where(category: :nico) }
def self.tagme
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
@@ -34,7 +41,8 @@ class Tag < ApplicationRecord
private
def nico_tag_name_must_start_with_nico
if category == 'nico' && name&.[](0, 5) != 'nico:'
if ((category == 'nico' && name&.[](0, 5) != 'nico:') ||
(category != 'nico' && name&.[](0, 5) == 'nico:'))
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
end
end