eBooru/app/models/forum_category.rb
Earlopain fc7d84affd
[RuboCop] Enable Style/FrozenStringLiteralComment
This reduces allocations on the posts page by about 5%, from basic testing
2024-02-25 18:15:55 +01:00

29 lines
659 B
Ruby

# frozen_string_literal: true
class ForumCategory < ApplicationRecord
has_many :forum_topics, -> { order(id: :desc) }, foreign_key: :category
validates :name, uniqueness: { case_sensitive: false }
after_destroy :reassign_topics
def reassign_topics
ForumTopic.where(category: id).update_all(category_id: 0)
end
def can_create_within?(user = CurrentUser.user)
user.level >= can_create
end
def self.reverse_mapping
order(:cat_order).all.map { |rec| [rec.name, rec.id] }
end
def self.ordered_categories
order(:cat_order)
end
def self.visible(user = CurrentUser.user)
where('can_view <= ?', user.level)
end
end