Merge pull request #4833 from techAPJ/trashable-topic-embed

FIX: make TopicEmbed trashable
This commit is contained in:
Arpit Jalan 2017-04-25 20:12:38 +05:30 committed by GitHub
commit 5493dc8b81
4 changed files with 47 additions and 7 deletions

View File

@ -40,12 +40,16 @@ class Topic < ActiveRecord::Base
update_category_topic_count_by(-1) if deleted_at.nil? update_category_topic_count_by(-1) if deleted_at.nil?
super(trashed_by) super(trashed_by)
update_flagged_posts_count update_flagged_posts_count
self.topic_embed.trash! if has_topic_embed?
end end
def recover! def recover!
update_category_topic_count_by(1) unless deleted_at.nil? update_category_topic_count_by(1) unless deleted_at.nil?
super super
update_flagged_posts_count update_flagged_posts_count
unless (topic_embed = TopicEmbed.with_deleted.find_by_topic_id(id)).nil?
topic_embed.recover!
end
end end
rate_limit :default_rate_limiter rate_limit :default_rate_limiter
@ -121,6 +125,8 @@ class Topic < ActiveRecord::Base
has_one :user_warning has_one :user_warning
has_one :first_post, -> {where post_number: 1}, class_name: Post has_one :first_post, -> {where post_number: 1}, class_name: Post
has_one :topic_embed, dependent: :destroy
# When we want to temporarily attach some data to a forum topic (usually before serialization) # When we want to temporarily attach some data to a forum topic (usually before serialization)
attr_accessor :user_data attr_accessor :user_data

View File

@ -1,11 +1,19 @@
require_dependency 'nokogiri' require_dependency 'nokogiri'
class TopicEmbed < ActiveRecord::Base class TopicEmbed < ActiveRecord::Base
include Trashable
belongs_to :topic belongs_to :topic
belongs_to :post belongs_to :post
validates_presence_of :embed_url validates_presence_of :embed_url
validates_uniqueness_of :embed_url validates_uniqueness_of :embed_url
before_validation(on: :create) do
unless (topic_embed = TopicEmbed.with_deleted.where('deleted_at IS NOT NULL AND embed_url = ?', embed_url).first).nil?
topic_embed.destroy!
end
end
class FetchResponse class FetchResponse
attr_accessor :title, :body, :author attr_accessor :title, :body, :author
end end
@ -210,6 +218,8 @@ end
# content_sha1 :string(40) # content_sha1 :string(40)
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# deleted_at :datetime
# deleted_by_id :integer
# #
# Indexes # Indexes
# #

View File

@ -0,0 +1,6 @@
class AddDeletedAtToTopicEmbeds < ActiveRecord::Migration
def change
add_column :topic_embeds, :deleted_at, :datetime
add_column :topic_embeds, :deleted_by_id, :integer, null: true
end
end

View File

@ -1493,6 +1493,15 @@ describe Topic do
expect { topic.trash!(moderator) }.to_not change { category.reload.topic_count } expect { topic.trash!(moderator) }.to_not change { category.reload.topic_count }
end end
end end
it "trashes topic embed record" do
topic = Fabricate(:topic)
post = Fabricate(:post, topic: topic, post_number: 1)
topic_embed = TopicEmbed.create!(topic_id: topic.id, embed_url: "https://blog.codinghorror.com/password-rules-are-bullshit", post_id: post.id)
topic.trash!
topic_embed.reload
expect(topic_embed.deleted_at).not_to eq(nil)
end
end end
describe 'recover!' do describe 'recover!' do
@ -1509,6 +1518,15 @@ describe Topic do
expect { topic.recover! }.to_not change { category.reload.topic_count } expect { topic.recover! }.to_not change { category.reload.topic_count }
end end
end end
it "recovers topic embed record" do
topic = Fabricate(:topic, deleted_at: 1.day.ago)
post = Fabricate(:post, topic: topic, post_number: 1)
topic_embed = TopicEmbed.create!(topic_id: topic.id, embed_url: "https://blog.codinghorror.com/password-rules-are-bullshit", post_id: post.id, deleted_at: 1.day.ago)
topic.recover!
topic_embed.reload
expect(topic_embed.deleted_at).to eq(nil)
end
end end
context "new user limits" do context "new user limits" do