posts: refactor modules to use concerning.

This commit is contained in:
evazion
2022-04-06 19:31:09 -05:00
parent a4d43ae72a
commit c707190bc1

View File

@@ -108,9 +108,7 @@ class Post < ApplicationRecord
)
end
module FileMethods
extend ActiveSupport::Concern
concerning :FileMethods do
def seo_tags
presenter.humanized_essential_tag_string.gsub(/[^a-z0-9]+/, "_").gsub(/(?:^_+)|(?:_+$)/, "").gsub(/_{2,}/, "_")
end
@@ -191,7 +189,7 @@ class Post < ApplicationRecord
end
end
module ImageMethods
concerning :ImageMethods do
def twitter_card_supported?
image_width.to_i >= 280 && image_height.to_i >= 150
end
@@ -248,7 +246,7 @@ class Post < ApplicationRecord
end
end
module ApprovalMethods
concerning :ApprovalMethods do
def in_modqueue?
is_pending? || is_flagged? || is_appealed?
end
@@ -280,7 +278,7 @@ class Post < ApplicationRecord
end
end
module PresenterMethods
concerning :PresenterMethods do
def presenter
@presenter ||= PostPresenter.new(self)
end
@@ -320,7 +318,7 @@ class Post < ApplicationRecord
end
end
module TagMethods
concerning :TagMethods do
def tag_array
tag_string.split
end
@@ -683,7 +681,7 @@ class Post < ApplicationRecord
end
end
module FavoriteMethods
concerning :FavoriteMethods do
def favorited_by?(user)
return false if user.is_anonymous?
Favorite.exists?(post: self, user: user)
@@ -700,7 +698,7 @@ class Post < ApplicationRecord
end
end
module PoolMethods
concerning :PoolMethods do
def pools
Pool.where("pools.post_ids && array[?]", id)
end
@@ -716,7 +714,7 @@ class Post < ApplicationRecord
end
end
module VoteMethods
concerning :VoteMethods do
def vote!(score, voter)
# Ignore vote if user doesn't have permission to vote.
return unless Pundit.policy!(voter, PostVote).create?
@@ -728,7 +726,7 @@ class Post < ApplicationRecord
end
end
module ParentMethods
concerning :ParentMethods do
# A parent has many children. A child belongs to a parent.
# A parent cannot have a parent.
#
@@ -799,7 +797,7 @@ class Post < ApplicationRecord
end
end
module DeletionMethods
concerning :DeletionMethods do
def expunge!
transaction do
Post.without_timeout do
@@ -850,7 +848,7 @@ class Post < ApplicationRecord
end
end
module VersionMethods
concerning :VersionMethods do
def create_version(force = false)
if new_record? || saved_change_to_watched_attributes? || force
create_new_version
@@ -888,7 +886,7 @@ class Post < ApplicationRecord
end
end
module NoteMethods
concerning :NoteMethods do
def has_notes?
last_noted_at.present?
end
@@ -930,7 +928,7 @@ class Post < ApplicationRecord
end
end
module ApiMethods
concerning :ApiMethods do
def legacy_attributes
hash = {
"has_comments" => last_commented_at.present?,
@@ -973,7 +971,8 @@ class Post < ApplicationRecord
end
end
module SearchMethods
concerning :SearchMethods do
class_methods do
# Return a set of up to N random posts. May return less if there aren't
# enough posts.
#
@@ -1151,8 +1150,9 @@ class Post < ApplicationRecord
q
end
end
end
module PixivMethods
concerning :PixivMethods do
def parse_pixiv_id
self.pixiv_id = nil
return unless web_source?
@@ -1221,7 +1221,7 @@ class Post < ApplicationRecord
end
end
module ValidationMethods
concerning :ValidationMethods do
def post_is_not_its_own_parent
if !new_record? && id == parent_id
errors.add(:base, "Post cannot have itself as a parent")
@@ -1287,23 +1287,6 @@ class Post < ApplicationRecord
end
end
include FileMethods
include ImageMethods
include ApprovalMethods
include PresenterMethods
include TagMethods
include FavoriteMethods
include PoolMethods
include VoteMethods
include ParentMethods
include DeletionMethods
include VersionMethods
include NoteMethods
include ApiMethods
extend SearchMethods
include PixivMethods
include ValidationMethods
has_bit_flags ["has_embedded_notes"]
def safeblocked?