FIX: make TopicEmbed trashable

This commit is contained in:
Arpit Jalan 2017-04-24 23:59:04 +05:30
parent 6bafb74e67
commit aeead60036
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?
super(trashed_by)
update_flagged_posts_count
self.topic_embed.trash! if has_topic_embed?
end
def recover!
update_category_topic_count_by(1) unless deleted_at.nil?
super
update_flagged_posts_count
unless (topic_embed = TopicEmbed.with_deleted.find_by_topic_id(id)).nil?
topic_embed.recover!
end
end
rate_limit :default_rate_limiter
@ -121,6 +125,8 @@ class Topic < ActiveRecord::Base
has_one :user_warning
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)
attr_accessor :user_data

View File

@ -1,11 +1,19 @@
require_dependency 'nokogiri'
class TopicEmbed < ActiveRecord::Base
include Trashable
belongs_to :topic
belongs_to :post
validates_presence_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
attr_accessor :title, :body, :author
end
@ -203,13 +211,15 @@ end
#
# Table name: topic_embeds
#
# id :integer not null, primary key
# topic_id :integer not null
# post_id :integer not null
# embed_url :string(1000) not null
# content_sha1 :string(40)
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# topic_id :integer not null
# post_id :integer not null
# embed_url :string(1000) not null
# content_sha1 :string(40)
# created_at :datetime not null
# updated_at :datetime not null
# deleted_at :datetime
# deleted_by_id :integer
#
# 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 }
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
describe 'recover!' do
@ -1509,6 +1518,15 @@ describe Topic do
expect { topic.recover! }.to_not change { category.reload.topic_count }
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
context "new user limits" do