models: refactor class methods into scopes.
This commit is contained in:
@@ -24,15 +24,10 @@ class Comment < ApplicationRecord
|
||||
:body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n"}
|
||||
)
|
||||
|
||||
scope :deleted, -> { where(is_deleted: true) }
|
||||
scope :undeleted, -> { where(is_deleted: false) }
|
||||
|
||||
module SearchMethods
|
||||
def deleted
|
||||
where("comments.is_deleted = true")
|
||||
end
|
||||
|
||||
def undeleted
|
||||
where("comments.is_deleted = false")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -35,15 +35,13 @@ class ForumPost < ApplicationRecord
|
||||
:body => ->(user_name) {%{@#{creator.name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n}}
|
||||
)
|
||||
|
||||
scope :active, -> { where(is_deleted: false) }
|
||||
|
||||
module SearchMethods
|
||||
def topic_title_matches(title)
|
||||
where(topic_id: ForumTopic.search(title_matches: title).select(:id))
|
||||
end
|
||||
|
||||
def active
|
||||
where("forum_posts.is_deleted = false")
|
||||
end
|
||||
|
||||
def permitted
|
||||
where(topic_id: ForumTopic.permitted)
|
||||
end
|
||||
|
||||
@@ -31,6 +31,8 @@ class ForumTopic < ApplicationRecord
|
||||
ModAction.log("locked forum topic ##{id} (title: #{title})", :forum_topic_lock)
|
||||
end
|
||||
|
||||
scope :active, -> { where(is_deleted: false) }
|
||||
|
||||
module CategoryMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
@@ -50,10 +52,6 @@ class ForumTopic < ApplicationRecord
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def active
|
||||
where(is_deleted: false)
|
||||
end
|
||||
|
||||
def permitted
|
||||
where("min_level <= ?", CurrentUser.level)
|
||||
end
|
||||
|
||||
@@ -10,11 +10,9 @@ class Note < ApplicationRecord
|
||||
after_save :create_version
|
||||
validate :validate_post_is_not_locked
|
||||
|
||||
module SearchMethods
|
||||
def active
|
||||
where("is_active = TRUE")
|
||||
end
|
||||
scope :active, -> { where(is_active: true) }
|
||||
|
||||
module SearchMethods
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -16,23 +16,12 @@ class Pool < ApplicationRecord
|
||||
|
||||
api_attributes including: [:post_count]
|
||||
|
||||
scope :deleted, -> { where(is_deleted: true) }
|
||||
scope :undeleted, -> { where(is_deleted: false) }
|
||||
scope :series, -> { where(category: "series") }
|
||||
scope :collection, -> { where(category: "collection") }
|
||||
|
||||
module SearchMethods
|
||||
def deleted
|
||||
where("pools.is_deleted = true")
|
||||
end
|
||||
|
||||
def undeleted
|
||||
where("pools.is_deleted = false")
|
||||
end
|
||||
|
||||
def series
|
||||
where("pools.category = ?", "series")
|
||||
end
|
||||
|
||||
def collection
|
||||
where("pools.category = ?", "collection")
|
||||
end
|
||||
|
||||
def name_matches(name)
|
||||
name = normalize_name_for_search(name)
|
||||
name = "*#{name}*" unless name =~ /\*/
|
||||
|
||||
@@ -59,6 +59,16 @@ class Post < ApplicationRecord
|
||||
|
||||
attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning, :view_count
|
||||
|
||||
scope :pending, -> { where(is_pending: true) }
|
||||
scope :flagged, -> { where(is_flagged: true) }
|
||||
scope :pending_or_flagged, -> { pending.or(flagged) }
|
||||
|
||||
scope :undeleted, -> { where(is_deleted: false) }
|
||||
scope :unflagged, -> { where(is_flagged: false) }
|
||||
scope :deleted, -> { where(is_deleted: true) }
|
||||
scope :has_notes, -> { where.not(last_noted_at: nil) }
|
||||
scope :for_user, ->(user_id) { where(uploader_id: user_id) }
|
||||
|
||||
if PostVersion.enabled?
|
||||
has_many :versions, -> { Rails.env.test? ? order("post_versions.updated_at ASC, post_versions.id ASC") : order("post_versions.updated_at ASC") }, class_name: "PostVersion", dependent: :destroy
|
||||
end
|
||||
@@ -1520,34 +1530,6 @@ class Post < ApplicationRecord
|
||||
from(relation.arel.as("posts"))
|
||||
end
|
||||
|
||||
def pending
|
||||
where(is_pending: true)
|
||||
end
|
||||
|
||||
def flagged
|
||||
where(is_flagged: true)
|
||||
end
|
||||
|
||||
def pending_or_flagged
|
||||
pending.or(flagged)
|
||||
end
|
||||
|
||||
def undeleted
|
||||
where("is_deleted = ?", false)
|
||||
end
|
||||
|
||||
def deleted
|
||||
where("is_deleted = ?", true)
|
||||
end
|
||||
|
||||
def has_notes
|
||||
where("last_noted_at is not null")
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("uploader_id = ?", user_id)
|
||||
end
|
||||
|
||||
def available_for_moderation(hidden = false, user = CurrentUser.user)
|
||||
return none if user.is_anonymous?
|
||||
|
||||
|
||||
@@ -10,19 +10,11 @@ class PostAppeal < ApplicationRecord
|
||||
validate :validate_creator_is_not_limited
|
||||
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
|
||||
|
||||
scope :resolved, -> { where(post: Post.undeleted.unflagged) }
|
||||
scope :unresolved, -> { where(post: Post.deleted.or(Post.flagged)) }
|
||||
scope :recent, -> { where("post_appeals.created_at >= ?", 1.day.ago) }
|
||||
|
||||
module SearchMethods
|
||||
def resolved
|
||||
joins(:post).where("posts.is_deleted = false and posts.is_flagged = false")
|
||||
end
|
||||
|
||||
def unresolved
|
||||
joins(:post).where("posts.is_deleted = true or posts.is_flagged = true")
|
||||
end
|
||||
|
||||
def recent
|
||||
where("created_at >= ?", 1.day.ago)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
q = q.search_attributes(params, :creator, :post, :reason)
|
||||
|
||||
@@ -21,6 +21,10 @@ class PostFlag < ApplicationRecord
|
||||
scope :by_users, -> { where.not(creator: User.system) }
|
||||
scope :by_system, -> { where(creator: User.system) }
|
||||
scope :in_cooldown, -> { by_users.where("created_at >= ?", COOLDOWN_PERIOD.ago) }
|
||||
scope :resolved, -> { where(is_resolved: true) }
|
||||
scope :unresolved, -> { where(is_resolved: false) }
|
||||
scope :recent, -> { where("post_flags.created_at >= ?", 1.day.ago) }
|
||||
scope :old, -> { where("post_flags.created_at <= ?", 3.days.ago) }
|
||||
|
||||
module SearchMethods
|
||||
def duplicate
|
||||
@@ -31,22 +35,6 @@ class PostFlag < ApplicationRecord
|
||||
where("to_tsvector('english', post_flags.reason) @@ to_tsquery('!dup & !duplicate & !sample & !smaller')")
|
||||
end
|
||||
|
||||
def resolved
|
||||
where("is_resolved = ?", true)
|
||||
end
|
||||
|
||||
def unresolved
|
||||
where("is_resolved = ?", false)
|
||||
end
|
||||
|
||||
def recent
|
||||
where("created_at >= ?", 1.day.ago)
|
||||
end
|
||||
|
||||
def old
|
||||
where("created_at <= ?", 3.days.ago)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -58,6 +58,9 @@ class Tag < ApplicationRecord
|
||||
before_save :update_category_cache, if: :category_changed?
|
||||
before_save :update_category_post_counts, if: :category_changed?
|
||||
|
||||
scope :empty, -> { where("tags.post_count <= 0") }
|
||||
scope :nonempty, -> { where("tags.post_count > 0") }
|
||||
|
||||
module ApiMethods
|
||||
def to_legacy_json
|
||||
return {
|
||||
@@ -797,14 +800,6 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def empty
|
||||
where("tags.post_count <= 0")
|
||||
end
|
||||
|
||||
def nonempty
|
||||
where("tags.post_count > 0")
|
||||
end
|
||||
|
||||
# ref: https://www.postgresql.org/docs/current/static/pgtrgm.html#idm46428634524336
|
||||
def order_similarity(name)
|
||||
# trunc(3 * sim) reduces the similarity score from a range of 0.0 -> 1.0 to just 0, 1, or 2.
|
||||
|
||||
@@ -68,7 +68,9 @@ class Upload < ApplicationRecord
|
||||
|
||||
after_destroy_commit :delete_files
|
||||
|
||||
scope :pending, -> { where(status: "pending") }
|
||||
scope :preprocessed, -> { where(status: "preprocessed") }
|
||||
scope :uploaded_by, ->(user_id) { where(uploader_id: user_id) }
|
||||
|
||||
def initialize_attributes
|
||||
self.uploader_id = CurrentUser.id
|
||||
@@ -180,14 +182,6 @@ class Upload < ApplicationRecord
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def uploaded_by(user_id)
|
||||
where("uploader_id = ?", user_id)
|
||||
end
|
||||
|
||||
def pending
|
||||
where(:status => "pending")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = super
|
||||
|
||||
|
||||
@@ -129,6 +129,12 @@ class User < ApplicationRecord
|
||||
|
||||
enum theme: { light: 0, dark: 100 }, _suffix: true
|
||||
|
||||
# UserDeletion#rename renames deleted users to `user_<1234>~`. Tildes
|
||||
# are appended if the username is taken.
|
||||
scope :deleted, -> { where("name ~ 'user_[0-9]+~*'") }
|
||||
scope :undeleted, -> { where("name !~ 'user_[0-9]+~*'") }
|
||||
scope :admins, -> { where(level: Levels::ADMIN) }
|
||||
|
||||
module BanMethods
|
||||
def validate_ip_addr_is_not_banned
|
||||
if IpBan.is_banned?(CurrentUser.ip_addr)
|
||||
@@ -635,20 +641,6 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def admins
|
||||
where("level = ?", Levels::ADMIN)
|
||||
end
|
||||
|
||||
# UserDeletion#rename renames deleted users to `user_<1234>~`. Tildes
|
||||
# are appended if the username is taken.
|
||||
def deleted
|
||||
where("name ~ 'user_[0-9]+~*'")
|
||||
end
|
||||
|
||||
def undeleted
|
||||
where("name !~ 'user_[0-9]+~*'")
|
||||
end
|
||||
|
||||
def with_email(email)
|
||||
if email.blank?
|
||||
where("FALSE")
|
||||
|
||||
@@ -21,6 +21,8 @@ class WikiPage < ApplicationRecord
|
||||
|
||||
api_attributes including: [:category_name]
|
||||
|
||||
scope :active, -> { where(is_deleted: false) }
|
||||
|
||||
module SearchMethods
|
||||
def find_by_id_or_title(id)
|
||||
if id =~ /\A\d+\z/
|
||||
@@ -34,10 +36,6 @@ class WikiPage < ApplicationRecord
|
||||
where(title: normalize_title(title))
|
||||
end
|
||||
|
||||
def active
|
||||
where("is_deleted = false")
|
||||
end
|
||||
|
||||
def other_names_include(name)
|
||||
name = normalize_other_name(name)
|
||||
subquery = WikiPage.from("unnest(other_names) AS other_name").where_iequals("other_name", name)
|
||||
|
||||
Reference in New Issue
Block a user