Commit 331e4e98e82b754672fd671fb3e79732b29eaa9e

Authored by Aurélio A. Heckert
1 parent 37096d03

Makes FilePresenter to work with all noosfero tests

app/controllers/application_controller.rb
1 1 class ApplicationController < ActionController::Base
2 2  
3   - # Preload FilePresenters to allow `FilePresenter.for()` to work
4   - FilePresenter::Generic
5   - FilePresenter::Image
6   -
7 3 before_filter :setup_multitenancy
8 4 before_filter :detect_stuff_by_domain
9 5 before_filter :init_noosfero_plugins
... ...
app/helpers/cms_helper.rb
... ... @@ -33,7 +33,7 @@ module CmsHelper
33 33 link_to article_name, {:action => 'view', :id => article.id}, :class => icon_for_article(article)
34 34 else
35 35 if article.image?
36   - image_tag(icon_for_article(article)) + link_to(article_name, article.url)
  36 + image_tag(icon_for_article(article)) + link_to(article_name, article.url)
37 37 else
38 38 link_to article_name, article.url, :class => icon_for_article(article)
39 39 end
... ...
app/helpers/folder_helper.rb
... ... @@ -41,7 +41,10 @@ module FolderHelper
41 41 end
42 42  
43 43 def icon_for_article(article)
44   - icon = article.icon_name || article.class.icon_name(article)
  44 + article = FilePresenter.for article
  45 + icon = article.respond_to?(:icon_name) ?
  46 + article.icon_name :
  47 + article.class.icon_name(article)
45 48 if (icon =~ /\//)
46 49 icon
47 50 else
... ...
app/models/uploaded_file.rb
... ... @@ -60,13 +60,19 @@ class UploadedFile &lt; Article
60 60  
61 61 postgresql_attachment_fu
62 62  
  63 + # Use this method only to get the generic icon for this kind of content.
  64 + # If you want the specific icon for a file type or the iconified version
  65 + # of an image, use FilePresenter.for(uploaded_file).icon_name
63 66 def self.icon_name(article = nil)
64   - warn = ('='*80) + "\n" +
65   - 'The method `UploadedFile.icon_name(obj)` is deprecated. ' +
66   - 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' +
67   - "\n" + ('='*80)
68   - Rails.logger.warn warn if Rails.logger
69   - puts warn
  67 + unless article.nil?
  68 + warn = ('='*80) + "\n" +
  69 + 'The method `UploadedFile.icon_name(obj)` is deprecated. ' +
  70 + 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' +
  71 + "\n" + ('='*80)
  72 + raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test'
  73 + Rails.logger.warn warn if Rails.logger
  74 + puts warn if ENV['RAILS_ENV'] == 'development'
  75 + end
70 76 'upload-file'
71 77 end
72 78  
... ... @@ -94,11 +100,12 @@ class UploadedFile &lt; Article
94 100  
95 101 def to_html(options = {})
96 102 warn = ('='*80) + "\n" +
97   - 'The method `UploadedFile::to_html()` is deprecated. ' +
  103 + 'The method `UploadedFile#to_html()` is deprecated. ' +
98 104 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' +
99 105 "\n" + ('='*80)
  106 + raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test'
100 107 Rails.logger.warn warn if Rails.logger
101   - puts warn
  108 + puts warn if ENV['RAILS_ENV'] == 'development'
102 109 article = self
103 110 if image?
104 111 lambda do
... ...
app/views/cms/view.rhtml
... ... @@ -40,13 +40,15 @@
40 40 </tr>
41 41 <% end %>
42 42  
43   - <% @articles.each do |article| %>
  43 + <% @articles.each do |article| article = FilePresenter.for article %>
44 44 <tr>
45 45 <td>
46 46 <%= link_to_article(article) %>
47 47 </td>
48 48 <td>
49   - <%= article.class.short_description %>
  49 + <%= article.respond_to?(:short_description) ?
  50 + article.short_description :
  51 + article.class.short_description %>
50 52 </td>
51 53 <td class="article-controls">
52 54 <%= expirable_button article, :edit, _('Edit'), {:action => 'edit', :id => article.id} if !remove_content_button(:edit) %>
... ...
app/views/file_presenter/_image.html.erb
... ... @@ -31,7 +31,6 @@
31 31 <img src="<%=image.public_filename(:display)%>" class="<%=image.css_class_name%>">
32 32  
33 33 <div class="uploaded-file-description <%= 'empty' if image.abstract.blank? %>">
34   - <%= image.class %>
35 34 <%= image.abstract %>
36 35 </div>
37 36  
... ...
lib/file_presenter.rb
... ... @@ -22,6 +22,15 @@ class FilePresenter
22 22 @file
23 23 end
24 24  
  25 + def id
  26 + @file.id
  27 + end
  28 +
  29 + def reload
  30 + @file.reload
  31 + self
  32 + end
  33 +
25 34 # This method must be overridden in subclasses.
26 35 #
27 36 # If the class accepts the file, return a number that represents the
... ... @@ -33,6 +42,10 @@ class FilePresenter
33 42 nil
34 43 end
35 44  
  45 + def short_description
  46 + _("File (%s)") % content_type
  47 + end
  48 +
36 49 # Define the css classes to style the page fragment with the file related
37 50 # content. If you want other classes to identify this area to your
38 51 # customized presenter, so do this:
... ... @@ -88,3 +101,7 @@ class FilePresenter
88 101 end
89 102  
90 103 end
  104 +
  105 +# Preload FilePresenters to allow `FilePresenter.for()` to work
  106 +FilePresenter::Generic
  107 +FilePresenter::Image
... ...
lib/file_presenter/image.rb
... ... @@ -8,6 +8,10 @@ class FilePresenter::Image &lt; FilePresenter
8 8 end
9 9  
10 10 def icon_name
11   - article.public_filename :icon
  11 + public_filename :icon
  12 + end
  13 +
  14 + def short_description
  15 + _('Image (%s)') % content_type.split('/')[1].upcase
12 16 end
13 17 end
... ...
plugins/html5_video/lib/file_presenter/video.rb
... ... @@ -7,4 +7,8 @@ class FilePresenter::Video &lt; FilePresenter
7 7 return nil if f.content_type.nil?
8 8 ( f.content_type[0..4] == 'video' ) ? 10 : nil
9 9 end
  10 +
  11 + def short_description
  12 + _('Video (%s)') % content_type.split('/')[1].upcase
  13 + end
10 14 end
... ...
plugins/html5_video/test/functional/content_viewer_controler_test.rb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require 'content_viewer_controller'
  3 +
  4 +class ContentViewerController
  5 + # Re-raise errors caught by the controller.
  6 + def rescue_action(e) raise e end
  7 + append_view_path File.join(File.dirname(__FILE__) + '/../../views')
  8 +end
  9 +
  10 +class ContentViewerControllerTest < ActionController::TestCase
  11 +
  12 + all_fixtures
  13 +
  14 + def setup
  15 + @controller = ContentViewerController.new
  16 + @request = ActionController::TestRequest.new
  17 + @response = ActionController::TestResponse.new
  18 +
  19 + @profile = create_user('testinguser').person
  20 + @environment = @profile.environment
  21 + end
  22 + attr_reader :profile, :environment
  23 +
  24 + should 'add html5 video tag to the page of file type video' do
  25 + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'video/ogg'), :profile => profile)
  26 + get :view_page, file.url.merge(:view=>:true)
  27 + assert_select '#article video'
  28 + end
  29 +
  30 +end
... ...
plugins/html5_video/views/file_presenter/_video.html.erb
1 1 <video class="video-js vjs-default-skin" controls poster="video.jpg" preload="auto" data-setup="{}">
2   - <source type="video/ogg" src="<%= video.public_filename %>">
  2 + <source type="video/ogg" src="<%= video.public_filename %>"/>
3 3 </video>
4 4  
5 5 <div class="uploaded-file-description <%= 'empty' if video.abstract.blank? %>">
... ...
test/functional/cms_controller_test.rb
... ... @@ -931,8 +931,8 @@ class CmsControllerTest &lt; ActionController::TestCase
931 931 :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
932 932  
933 933 process_delayed_job_queue
934   - file = profile.articles.find_by_name('rails.png')
935   - assert File.exists?(file.class.icon_name(file))
  934 + file = FilePresenter.for profile.articles.find_by_name('rails.png')
  935 + assert File.exists?(file.icon_name)
936 936 file.destroy
937 937 end
938 938  
... ... @@ -942,8 +942,8 @@ class CmsControllerTest &lt; ActionController::TestCase
942 942 :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
943 943  
944 944 process_delayed_job_queue
945   - file = profile.articles.find_by_name('rails.png')
946   - assert File.exists?(file.class.icon_name(file))
  945 + file = FilePresenter.for profile.articles.find_by_name('rails.png')
  946 + assert File.exists?(file.icon_name)
947 947 file.destroy
948 948 end
949 949  
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -1502,4 +1502,12 @@ end
1502 1502 assert_response 404
1503 1503 end
1504 1504  
  1505 + should 'display link to download of non-recognized file types on its page' do
  1506 + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'bin/unknown'), :profile => profile)
  1507 + get :view_page, file.url.merge(:view=>:true)
  1508 + assert_tag :tag => 'a',
  1509 + :content => file.filename,
  1510 + :attributes => { :href => file.public_filename }
  1511 + end
  1512 +
1505 1513 end
... ...
test/unit/cms_helper_test.rb
... ... @@ -41,6 +41,7 @@ class CmsHelperTest &lt; ActiveSupport::TestCase
41 41 should 'display image and link if article is an image' do
42 42 profile = fast_create(Profile)
43 43 file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
  44 + file = FilePresenter.for file
44 45 icon = icon_for_article(file)
45 46 expects(:image_tag).with(icon).returns('icon')
46 47  
... ...
test/unit/file_presenter_test.rb 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class FilePresenterTest < ActiveSupport::TestCase
  4 +
  5 + should 'notify about deprecated method UploadedFile.icon_name' do
  6 + profile = fast_create(Profile)
  7 + file = UploadedFile.create!(
  8 + :profile => profile,
  9 + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
  10 + )
  11 + assert_raise NoMethodError do
  12 + UploadedFile.icon_name file
  13 + end
  14 + ENV.stubs('[]').with('RAILS_ENV').returns('other')
  15 + Rails.logger.expects(:warn) # must warn on any other RAILS_ENV
  16 + stubs(:puts)
  17 + UploadedFile.icon_name file
  18 + end
  19 +
  20 + should 'notify about deprecated method UploadedFile#to_html' do
  21 + profile = fast_create(Profile)
  22 + file = UploadedFile.create!(
  23 + :profile => profile,
  24 + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
  25 + )
  26 + assert_raise NoMethodError do
  27 + file.to_html
  28 + end
  29 + ENV.stubs('[]').with('RAILS_ENV').returns('other')
  30 + Rails.logger.expects(:warn) # must warn on any other RAILS_ENV
  31 + stubs(:puts)
  32 + file.to_html
  33 + end
  34 +
  35 + should 'return a thumbnail as icon for images ' do
  36 + f = UploadedFile.new
  37 + f.stubs(:image?).returns(true)
  38 + p = FilePresenter.for f
  39 + p.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
  40 + assert_equal '/path/to/file.xyz', p.icon_name
  41 + end
  42 +
  43 +end
... ...
test/unit/folder_helper_test.rb
... ... @@ -26,6 +26,7 @@ class FolderHelperTest &lt; ActiveSupport::TestCase
26 26 should 'display icon for images' do
27 27 profile = fast_create(Profile)
28 28 file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile)
  29 + file = FilePresenter.for file
29 30 process_delayed_job_queue
30 31  
31 32 assert_match /rails_icon\.png/, icon_for_article(file.reload)
... ...
test/unit/uploaded_file_test.rb
... ... @@ -7,14 +7,10 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
7 7 end
8 8 attr_reader :profile
9 9  
10   - should 'return a thumbnail as icon for images ' do
11   - f = UploadedFile.new
12   - f.expects(:image?).returns(true)
13   - f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
14   - assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f)
15   - end
16   -
17 10 should 'return a default icon for uploaded files' do
  11 + ENV.stubs('[]').with('RAILS_ENV').returns('other')
  12 + Rails.logger.expects(:warn) # warn about deprecatede usage of UploadedFile.icon_name
  13 + stubs(:puts)
18 14 assert_equal 'upload-file', UploadedFile.icon_name
19 15 end
20 16  
... ... @@ -113,8 +109,11 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
113 109 p = create_user('test_user').person
114 110 file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => p)
115 111  
  112 + ENV.stubs('[]').with('RAILS_ENV').returns('other')
  113 + Rails.logger.expects(:warn) # warn about deprecatede usage of UploadedFile#to_html
  114 + stubs(:puts)
116 115 stubs(:content_tag).returns('link')
117   - expects(:link_to).with(file.name, file.url, :class => file.css_class_name)
  116 + expects(:link_to).with(file.name, file.url)
118 117  
119 118 instance_eval(&file.to_html)
120 119 end
... ... @@ -206,13 +205,6 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
206 205 file.destroy
207 206 end
208 207  
209   - should 'return the default thumbnail image as icon for images ' do
210   - f = UploadedFile.new
211   - f.expects(:image?).returns(true)
212   - f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
213   - assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f)
214   - end
215   -
216 208 should 'store width and height after processing' do
217 209 file = UploadedFile.create!(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
218 210 file.create_thumbnails
... ... @@ -287,12 +279,6 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
287 279 assert_equal '', f.lead
288 280 end
289 281  
290   - should 'survive when try to get icon_name from a file with mime_type nil' do
291   - f = UploadedFile.new
292   - f.expects(:mime_type).returns(nil)
293   - assert_equal 'upload-file', UploadedFile.icon_name(f)
294   - end
295   -
296 282 should 'upload to a folder with same name as the schema if database is postgresql' do
297 283 uses_postgresql 'image_schema_one'
298 284 file1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile)
... ...