diff --git a/app/models/post.rb b/app/models/post.rb index 8fc89f682c6..84566d5a8a2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -719,6 +719,7 @@ end # raw_email :text # public_version :integer default(1), not null # action_code :string +# image_url :string # # Indexes # diff --git a/db/migrate/20161029181306_add_image_url_to_posts.rb b/db/migrate/20161029181306_add_image_url_to_posts.rb new file mode 100644 index 00000000000..ed9e1ab8597 --- /dev/null +++ b/db/migrate/20161029181306_add_image_url_to_posts.rb @@ -0,0 +1,5 @@ +class AddImageUrlToPosts < ActiveRecord::Migration + def change + add_column :posts, :image_url, :string + end +end diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 5b1fa3a4749..5f06261c1b7 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -84,7 +84,7 @@ class CookedPostProcessor convert_to_link!(img) end - update_topic_image + update_post_image end def extract_images @@ -100,7 +100,7 @@ class CookedPostProcessor @doc.css(".quote img") end - def extract_images_for_topic + def extract_images_for_post # all image with a src attribute @doc.css("img[src]") - # minus, emojis @@ -285,10 +285,11 @@ class CookedPostProcessor span end - def update_topic_image - if @post.is_first_post? - img = extract_images_for_topic.first - @post.topic.update_column(:image_url, img["src"][0...255]) if img["src"].present? + def update_post_image + img = extract_images_for_post.first + if img["src"].present? + @post.update_column(:image_url, img["src"][0...255]) # post + @post.topic.update_column(:image_url, img["src"][0...255]) if @post.is_first_post? # topic end end diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 7c93adb8698..122e2bace63 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -180,8 +180,12 @@ class TopicView def image_url if @post_number.present? && @post_number.to_i != 1 && @desired_post.present? - # show poster avatar - @desired_post.user.avatar_template_url.gsub("{size}", "100") if @desired_post.user + if @desired_post.image_url.present? + @desired_post.image_url + elsif @desired_post.user + # show poster avatar + @desired_post.user.avatar_template_url.gsub("{size}", "100") + end else @topic.image_url end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index cd7eddf1b72..86810e5e597 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -242,19 +242,30 @@ describe CookedPostProcessor do end context "topic image" do - let(:topic) { build(:topic, id: 1) } let(:post) { Fabricate(:post_with_uploaded_image, topic: topic) } let(:cpp) { CookedPostProcessor.new(post) } - it "adds a topic image if there's one in the post" do + it "adds a topic image if there's one in the first post" do FastImage.stubs(:size) expect(post.topic.image_url).to eq(nil) cpp.post_process_images post.topic.reload expect(post.topic.image_url).to be_present end + end + context "post image" do + let(:reply) { Fabricate(:post_with_uploaded_image, post_number: 2) } + let(:cpp) { CookedPostProcessor.new(reply) } + + it "adds a post image if there's one in the post" do + FastImage.stubs(:size) + expect(reply.image_url).to eq(nil) + cpp.post_process_images + reply.reload + expect(reply.image_url).to be_present + end end end