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