From aeead600363b8e80d75bbff0bdca2234064d895e Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 24 Apr 2017 23:59:04 +0530 Subject: [PATCH] FIX: make TopicEmbed trashable --- app/models/topic.rb | 6 +++++ app/models/topic_embed.rb | 24 +++++++++++++------ ...25083011_add_deleted_at_to_topic_embeds.rb | 6 +++++ spec/models/topic_spec.rb | 18 ++++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20170425083011_add_deleted_at_to_topic_embeds.rb diff --git a/app/models/topic.rb b/app/models/topic.rb index 604c491284a..a63100fd13c 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -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 diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index 8e45c1d60ea..15ca33e40f4 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -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 # diff --git a/db/migrate/20170425083011_add_deleted_at_to_topic_embeds.rb b/db/migrate/20170425083011_add_deleted_at_to_topic_embeds.rb new file mode 100644 index 00000000000..5aaf8ed8365 --- /dev/null +++ b/db/migrate/20170425083011_add_deleted_at_to_topic_embeds.rb @@ -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 diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 8fe1d46d0bd..8a8db5e1744 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -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