models: add deletable concern.

This commit is contained in:
evazion
2020-03-06 17:06:29 -06:00
parent 32bad825e1
commit 5bc0ab446b
13 changed files with 30 additions and 37 deletions

View File

@@ -61,14 +61,14 @@ class CommentsController < ApplicationController
def destroy
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.delete!
@comment.update(is_deleted: true)
respond_with(@comment)
end
def undelete
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.undelete!
@comment.update(is_deleted: false)
respond_with(@comment)
end

View File

@@ -67,7 +67,7 @@ class ForumTopicsController < ApplicationController
def destroy
check_privilege(@forum_topic)
@forum_topic.delete!
@forum_topic.update(is_deleted: true)
@forum_topic.create_mod_action_for_delete
flash[:notice] = "Topic deleted"
respond_with(@forum_topic)
@@ -75,7 +75,7 @@ class ForumTopicsController < ApplicationController
def undelete
check_privilege(@forum_topic)
@forum_topic.undelete!
@forum_topic.update(is_deleted: false)
@forum_topic.create_mod_action_for_undelete
flash[:notice] = "Topic undeleted"
respond_with(@forum_topic)

View File

@@ -0,0 +1,11 @@
module Deletable
extend ActiveSupport::Concern
class_methods do
def deletable
scope :active, -> { where(is_deleted: false) }
scope :deleted, -> { where(is_deleted: true) }
scope :undeleted, -> { where(is_deleted: false) }
end
end
end

View File

@@ -1,6 +1,7 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
include Deletable
include Mentionable
extend HasBitFlags
extend Searchable

View File

@@ -4,6 +4,7 @@ class Artist < ApplicationRecord
attr_accessor :url_string_changed
array_attribute :other_names
deletable
before_validation :normalize_name
before_validation :normalize_other_names
@@ -20,8 +21,6 @@ class Artist < ApplicationRecord
accepts_nested_attributes_for :wiki_page, update_only: true, reject_if: :all_blank
scope :active, -> { where(is_deleted: false) }
scope :deleted, -> { where(is_deleted: true) }
scope :banned, -> { where(is_banned: true) }
scope :unbanned, -> { where(is_banned: false) }

View File

@@ -16,15 +16,14 @@ class Comment < ApplicationRecord
after_save(:if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted? && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.name}", :comment_delete)
end
deletable
mentionable(
:message_field => :body,
:title => ->(user_name) {"#{creator.name} mentioned you in a comment on post ##{post_id}"},
: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 search(params)
q = super
@@ -147,14 +146,6 @@ class Comment < ApplicationRecord
select { |comment| comment.visibility(user) == :visible }
end
def delete!
update(is_deleted: true)
end
def undelete!
update(is_deleted: false)
end
def quoted_response
DText.quote(body, creator.name)
end

View File

@@ -15,9 +15,8 @@ class Dmail < ApplicationRecord
after_commit :send_email, on: :create
api_attributes including: [:key]
deletable
scope :active, -> { where(is_deleted: false) }
scope :deleted, -> { where(is_deleted: true) }
scope :read, -> { where(is_read: true) }
scope :unread, -> { where(is_read: false) }
scope :sent, -> { where("dmails.owner_id = dmails.from_id") }

View File

@@ -25,14 +25,14 @@ class ForumPost < ApplicationRecord
after_destroy(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec|
ModAction.log("#{CurrentUser.name} deleted forum ##{rec.id}", :forum_post_delete)
end
deletable
mentionable(
:message_field => :body,
:title => ->(user_name) {%{#{creator.name} mentioned you in topic ##{topic_id} (#{topic.title})}},
: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))

View File

@@ -30,7 +30,7 @@ class ForumTopic < ApplicationRecord
ModAction.log("locked forum topic ##{id} (title: #{title})", :forum_topic_lock)
end
scope :active, -> { where(is_deleted: false) }
deletable
module CategoryMethods
extend ActiveSupport::Concern
@@ -158,14 +158,6 @@ class ForumTopic < ApplicationRecord
(response_count / Danbooru.config.posts_per_page.to_f).ceil
end
def delete!
update(is_deleted: true)
end
def undelete!
update(is_deleted: false)
end
def update_orignal_post
original_post&.update_columns(:updater_id => updater.id, :updated_at => Time.now)
end

View File

@@ -15,9 +15,8 @@ class Pool < ApplicationRecord
after_create :synchronize!
api_attributes including: [:post_count]
deletable
scope :deleted, -> { where(is_deleted: true) }
scope :undeleted, -> { where(is_deleted: false) }
scope :series, -> { where(category: "series") }
scope :collection, -> { where(category: "collection") }

View File

@@ -9,6 +9,8 @@ class Post < ApplicationRecord
# Tags to copy when copying notes.
NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation]
deletable
before_validation :merge_old_changes
before_validation :normalize_tags
before_validation :strip_source
@@ -60,9 +62,7 @@ class Post < ApplicationRecord
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) }

View File

@@ -1,5 +1,6 @@
class UserFeedback < ApplicationRecord
self.table_name = "user_feedback"
belongs_to :user
belongs_to :creator, class_name: "User"
attr_accessor :disable_dmail_notification
@@ -15,10 +16,11 @@ class UserFeedback < ApplicationRecord
ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user.name}":/users/#{rec.user_id}}, :user_feedback_delete)
end
deletable
scope :positive, -> { where(category: "positive") }
scope :neutral, -> { where(category: "neutral") }
scope :negative, -> { where(category: "negative") }
scope :undeleted, -> { where(is_deleted: false) }
module SearchMethods
def visible(viewer)

View File

@@ -20,8 +20,7 @@ class WikiPage < ApplicationRecord
has_many :dtext_links, as: :model, dependent: :destroy
api_attributes including: [:category_name]
scope :active, -> { where(is_deleted: false) }
deletable
module SearchMethods
def find_by_id_or_title(id)