diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 665dbfc..7d51312 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,5 @@ class ApplicationController < ActionController::Base - # Preload FilePresenters to allow `FilePresenter.for()` to work - FilePresenter::Generic - FilePresenter::Image - before_filter :setup_multitenancy before_filter :detect_stuff_by_domain before_filter :init_noosfero_plugins diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index 7d84e6d..3e9c085 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -33,7 +33,7 @@ module CmsHelper link_to article_name, {:action => 'view', :id => article.id}, :class => icon_for_article(article) else if article.image? - image_tag(icon_for_article(article)) + link_to(article_name, article.url) + image_tag(icon_for_article(article)) + link_to(article_name, article.url) else link_to article_name, article.url, :class => icon_for_article(article) end diff --git a/app/helpers/folder_helper.rb b/app/helpers/folder_helper.rb index 6838894..9df1226 100644 --- a/app/helpers/folder_helper.rb +++ b/app/helpers/folder_helper.rb @@ -41,7 +41,10 @@ module FolderHelper end def icon_for_article(article) - icon = article.icon_name || article.class.icon_name(article) + article = FilePresenter.for article + icon = article.respond_to?(:icon_name) ? + article.icon_name : + article.class.icon_name(article) if (icon =~ /\//) icon else diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index e45d758..fb99994 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -60,13 +60,19 @@ class UploadedFile < Article postgresql_attachment_fu + # Use this method only to get the generic icon for this kind of content. + # If you want the specific icon for a file type or the iconified version + # of an image, use FilePresenter.for(uploaded_file).icon_name def self.icon_name(article = nil) - warn = ('='*80) + "\n" + - 'The method `UploadedFile.icon_name(obj)` is deprecated. ' + - 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' + - "\n" + ('='*80) - Rails.logger.warn warn if Rails.logger - puts warn + unless article.nil? + warn = ('='*80) + "\n" + + 'The method `UploadedFile.icon_name(obj)` is deprecated. ' + + 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' + + "\n" + ('='*80) + raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test' + Rails.logger.warn warn if Rails.logger + puts warn if ENV['RAILS_ENV'] == 'development' + end 'upload-file' end @@ -94,11 +100,12 @@ class UploadedFile < Article def to_html(options = {}) warn = ('='*80) + "\n" + - 'The method `UploadedFile::to_html()` is deprecated. ' + + 'The method `UploadedFile#to_html()` is deprecated. ' + 'You must to encapsulate UploadedFile with `FilePresenter.for()`.' + "\n" + ('='*80) + raise NoMethodError, warn if ENV['RAILS_ENV'] == 'test' Rails.logger.warn warn if Rails.logger - puts warn + puts warn if ENV['RAILS_ENV'] == 'development' article = self if image? lambda do diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index 8a32f89..cdb2405 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -40,13 +40,15 @@ <% end %> - <% @articles.each do |article| %> + <% @articles.each do |article| article = FilePresenter.for article %> <%= link_to_article(article) %> - <%= article.class.short_description %> + <%= article.respond_to?(:short_description) ? + article.short_description : + article.class.short_description %> <%= expirable_button article, :edit, _('Edit'), {:action => 'edit', :id => article.id} if !remove_content_button(:edit) %> diff --git a/app/views/file_presenter/_image.html.erb b/app/views/file_presenter/_image.html.erb index 10af690..6ac32d4 100644 --- a/app/views/file_presenter/_image.html.erb +++ b/app/views/file_presenter/_image.html.erb @@ -31,7 +31,6 @@
- <%= image.class %> <%= image.abstract %>
diff --git a/lib/file_presenter.rb b/lib/file_presenter.rb index 1554a8f..272db6a 100644 --- a/lib/file_presenter.rb +++ b/lib/file_presenter.rb @@ -22,6 +22,15 @@ class FilePresenter @file end + def id + @file.id + end + + def reload + @file.reload + self + end + # This method must be overridden in subclasses. # # If the class accepts the file, return a number that represents the @@ -33,6 +42,10 @@ class FilePresenter nil end + def short_description + _("File (%s)") % content_type + end + # Define the css classes to style the page fragment with the file related # content. If you want other classes to identify this area to your # customized presenter, so do this: @@ -88,3 +101,7 @@ class FilePresenter end end + +# Preload FilePresenters to allow `FilePresenter.for()` to work +FilePresenter::Generic +FilePresenter::Image diff --git a/lib/file_presenter/image.rb b/lib/file_presenter/image.rb index 1192715..c9488f6 100644 --- a/lib/file_presenter/image.rb +++ b/lib/file_presenter/image.rb @@ -8,6 +8,10 @@ class FilePresenter::Image < FilePresenter end def icon_name - article.public_filename :icon + public_filename :icon + end + + def short_description + _('Image (%s)') % content_type.split('/')[1].upcase end end diff --git a/plugins/html5_video/lib/file_presenter/video.rb b/plugins/html5_video/lib/file_presenter/video.rb index 53ad7d8..2dcdfd8 100644 --- a/plugins/html5_video/lib/file_presenter/video.rb +++ b/plugins/html5_video/lib/file_presenter/video.rb @@ -7,4 +7,8 @@ class FilePresenter::Video < FilePresenter return nil if f.content_type.nil? ( f.content_type[0..4] == 'video' ) ? 10 : nil end + + def short_description + _('Video (%s)') % content_type.split('/')[1].upcase + end end diff --git a/plugins/html5_video/test/functional/content_viewer_controler_test.rb b/plugins/html5_video/test/functional/content_viewer_controler_test.rb new file mode 100644 index 0000000..5a0f494 --- /dev/null +++ b/plugins/html5_video/test/functional/content_viewer_controler_test.rb @@ -0,0 +1,30 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require 'content_viewer_controller' + +class ContentViewerController + # Re-raise errors caught by the controller. + def rescue_action(e) raise e end + append_view_path File.join(File.dirname(__FILE__) + '/../../views') +end + +class ContentViewerControllerTest < ActionController::TestCase + + all_fixtures + + def setup + @controller = ContentViewerController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @profile = create_user('testinguser').person + @environment = @profile.environment + end + attr_reader :profile, :environment + + should 'add html5 video tag to the page of file type video' do + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'video/ogg'), :profile => profile) + get :view_page, file.url.merge(:view=>:true) + assert_select '#article video' + end + +end diff --git a/plugins/html5_video/views/file_presenter/_video.html.erb b/plugins/html5_video/views/file_presenter/_video.html.erb index abf9b0c..04a40b7 100644 --- a/plugins/html5_video/views/file_presenter/_video.html.erb +++ b/plugins/html5_video/views/file_presenter/_video.html.erb @@ -1,5 +1,5 @@
diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index f0f76c4..d195d03 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -931,8 +931,8 @@ class CmsControllerTest < ActionController::TestCase :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} process_delayed_job_queue - file = profile.articles.find_by_name('rails.png') - assert File.exists?(file.class.icon_name(file)) + file = FilePresenter.for profile.articles.find_by_name('rails.png') + assert File.exists?(file.icon_name) file.destroy end @@ -942,8 +942,8 @@ class CmsControllerTest < ActionController::TestCase :article => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')} process_delayed_job_queue - file = profile.articles.find_by_name('rails.png') - assert File.exists?(file.class.icon_name(file)) + file = FilePresenter.for profile.articles.find_by_name('rails.png') + assert File.exists?(file.icon_name) file.destroy end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 0e5a994..2ffcc7f 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1502,4 +1502,12 @@ end assert_response 404 end + should 'display link to download of non-recognized file types on its page' do + file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'bin/unknown'), :profile => profile) + get :view_page, file.url.merge(:view=>:true) + assert_tag :tag => 'a', + :content => file.filename, + :attributes => { :href => file.public_filename } + end + end diff --git a/test/unit/cms_helper_test.rb b/test/unit/cms_helper_test.rb index 9bc0626..f0872e5 100644 --- a/test/unit/cms_helper_test.rb +++ b/test/unit/cms_helper_test.rb @@ -41,6 +41,7 @@ class CmsHelperTest < ActiveSupport::TestCase should 'display image and link if article is an image' do profile = fast_create(Profile) file = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + file = FilePresenter.for file icon = icon_for_article(file) expects(:image_tag).with(icon).returns('icon') diff --git a/test/unit/file_presenter_test.rb b/test/unit/file_presenter_test.rb new file mode 100644 index 0000000..fe1e7a0 --- /dev/null +++ b/test/unit/file_presenter_test.rb @@ -0,0 +1,43 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class FilePresenterTest < ActiveSupport::TestCase + + should 'notify about deprecated method UploadedFile.icon_name' do + profile = fast_create(Profile) + file = UploadedFile.create!( + :profile => profile, + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') + ) + assert_raise NoMethodError do + UploadedFile.icon_name file + end + ENV.stubs('[]').with('RAILS_ENV').returns('other') + Rails.logger.expects(:warn) # must warn on any other RAILS_ENV + stubs(:puts) + UploadedFile.icon_name file + end + + should 'notify about deprecated method UploadedFile#to_html' do + profile = fast_create(Profile) + file = UploadedFile.create!( + :profile => profile, + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') + ) + assert_raise NoMethodError do + file.to_html + end + ENV.stubs('[]').with('RAILS_ENV').returns('other') + Rails.logger.expects(:warn) # must warn on any other RAILS_ENV + stubs(:puts) + file.to_html + end + + should 'return a thumbnail as icon for images ' do + f = UploadedFile.new + f.stubs(:image?).returns(true) + p = FilePresenter.for f + p.expects(:public_filename).with(:icon).returns('/path/to/file.xyz') + assert_equal '/path/to/file.xyz', p.icon_name + end + +end diff --git a/test/unit/folder_helper_test.rb b/test/unit/folder_helper_test.rb index c28aee1..54c4562 100644 --- a/test/unit/folder_helper_test.rb +++ b/test/unit/folder_helper_test.rb @@ -26,6 +26,7 @@ class FolderHelperTest < ActiveSupport::TestCase should 'display icon for images' do profile = fast_create(Profile) file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => profile) + file = FilePresenter.for file process_delayed_job_queue assert_match /rails_icon\.png/, icon_for_article(file.reload) diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb index 55f774f..c128b4b 100644 --- a/test/unit/uploaded_file_test.rb +++ b/test/unit/uploaded_file_test.rb @@ -7,14 +7,10 @@ class UploadedFileTest < ActiveSupport::TestCase end attr_reader :profile - should 'return a thumbnail as icon for images ' do - f = UploadedFile.new - f.expects(:image?).returns(true) - f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz') - assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f) - end - should 'return a default icon for uploaded files' do + ENV.stubs('[]').with('RAILS_ENV').returns('other') + Rails.logger.expects(:warn) # warn about deprecatede usage of UploadedFile.icon_name + stubs(:puts) assert_equal 'upload-file', UploadedFile.icon_name end @@ -113,8 +109,11 @@ class UploadedFileTest < ActiveSupport::TestCase p = create_user('test_user').person file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :profile => p) + ENV.stubs('[]').with('RAILS_ENV').returns('other') + Rails.logger.expects(:warn) # warn about deprecatede usage of UploadedFile#to_html + stubs(:puts) stubs(:content_tag).returns('link') - expects(:link_to).with(file.name, file.url, :class => file.css_class_name) + expects(:link_to).with(file.name, file.url) instance_eval(&file.to_html) end @@ -206,13 +205,6 @@ class UploadedFileTest < ActiveSupport::TestCase file.destroy end - should 'return the default thumbnail image as icon for images ' do - f = UploadedFile.new - f.expects(:image?).returns(true) - f.expects(:public_filename).with(:icon).returns('/path/to/file.xyz') - assert_equal '/path/to/file.xyz', UploadedFile.icon_name(f) - end - should 'store width and height after processing' do file = UploadedFile.create!(:profile => @profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) file.create_thumbnails @@ -287,12 +279,6 @@ class UploadedFileTest < ActiveSupport::TestCase assert_equal '', f.lead end - should 'survive when try to get icon_name from a file with mime_type nil' do - f = UploadedFile.new - f.expects(:mime_type).returns(nil) - assert_equal 'upload-file', UploadedFile.icon_name(f) - end - should 'upload to a folder with same name as the schema if database is postgresql' do uses_postgresql 'image_schema_one' file1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => @profile) -- libgit2 0.21.2