Commit 8cea790d4686bbaf5c8299edec23391e3d56a05c

Authored by Larissa Reis
1 parent 1eb7f902

Includes article image to body images paths listing

The article image is the main image that represents the article so it
should be included in the listing, at the beginning. This is specially
useful for metadata plugin usage.
app/models/article.rb
@@ -806,11 +806,13 @@ class Article < ApplicationRecord @@ -806,11 +806,13 @@ class Article < ApplicationRecord
806 end 806 end
807 807
808 def body_images_paths 808 def body_images_paths
809 - Nokogiri::HTML.fragment(self.body.to_s).css('img[src]').collect do |i| 809 + paths = Nokogiri::HTML.fragment(self.body.to_s).css('img[src]').collect do |i|
810 src = i['src'] 810 src = i['src']
811 src = URI.escape src if self.new_record? # xss_terminate runs on save 811 src = URI.escape src if self.new_record? # xss_terminate runs on save
812 (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, src).to_s : src 812 (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, src).to_s : src
813 end 813 end
  814 + paths.unshift(URI.join(self.profile.environment.top_url, self.image.public_filename).to_s) if self.image.present?
  815 + paths
814 end 816 end
815 817
816 def more_comments_label 818 def more_comments_label
test/unit/article_test.rb
@@ -1483,6 +1483,17 @@ class ArticleTest < ActiveSupport::TestCase @@ -1483,6 +1483,17 @@ class ArticleTest < ActiveSupport::TestCase
1483 assert_includes a.body_images_paths, 'http://test.com/noosfero.png' 1483 assert_includes a.body_images_paths, 'http://test.com/noosfero.png'
1484 end 1484 end
1485 1485
  1486 + should 'always put article image first in images paths list in article body' do
  1487 + Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
  1488 + a = create(TinyMceArticle, :name => 'test', :image_builder => {
  1489 + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
  1490 + }, :profile_id => @profile.id)
  1491 + a.save!
  1492 + a.body = 'Noosfero <img src="http://noosfero.com/test.png" /> test <img src="http://test.com/noosfero.png" />'
  1493 + a.image.stubs(:public_filename).returns('/files/rails.png')
  1494 + assert_equal 'http://noosfero.org/files/rails.png', a.body_images_paths[0]
  1495 + end
  1496 +
1486 should 'escape utf8 characters correctly' do 1497 should 'escape utf8 characters correctly' do
1487 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') 1498 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
1488 a = build TinyMceArticle, profile: @profile 1499 a = build TinyMceArticle, profile: @profile