files are automatically categorized when uploaded through the widget
Oleg
committed Sep 20, 2011
commit c9d7360535931a197e1a91e9a9c686153b26e07a
Showing 8
changed files with
89 additions
and 4 deletions
app/controllers/cms_admin/files_controller.rb
+3
-1
| @@ | @@ -36,7 +36,9 @@ class CmsAdmin::FilesController < CmsAdmin::BaseController |
| io.class.class_eval { attr_accessor :original_filename, :content_type } | |
| io.original_filename = request.env['HTTP_X_FILE_NAME'] | |
| io.content_type = request.env['CONTENT_TYPE'] | |
| - | @file = @site.files.create!(:file => io) |
| + | @file = @site.files.create!( |
| + | (params[:file] || { }).merge(:file => io) |
| + | ) |
| end | |
| end | |
| rescue ActiveRecord::RecordInvalid | |
app/models/cms/file.rb
+17
-1
| @@ | @@ -6,6 +6,10 @@ class Cms::File < ActiveRecord::Base |
| cms_is_categorized | |
| + | attr_accessor :layout_id, |
| + | :page_id, |
| + | :snippet_id |
| + | |
| # -- AR Extensions -------------------------------------------------------- | |
| has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options | |
| @@ | @@ -20,7 +24,8 @@ class Cms::File < ActiveRecord::Base |
| :scope => :site_id | |
| # -- Callbacks ------------------------------------------------------------ | |
| - | before_save :assign_label |
| + | before_save :assign_label, |
| + | :categorize_file |
| protected | |
| @@ | @@ -28,4 +33,15 @@ protected |
| self.label = self.label.blank?? self.file_file_name.gsub(/\.[^\.]*?$/, '').titleize : self.label | |
| end | |
| + | def categorize_file |
| + | category = if layout_id && layout = site.layouts.find_by_id(layout_id) |
| + | Cms::Category.find_or_create_by_label_and_categorized_type("[layout] #{layout.slug}", 'Cms::File') |
| + | elsif page_id && page = site.pages.find_by_id(page_id) |
| + | Cms::Category.find_or_create_by_label_and_categorized_type("[page] #{page.full_path}", 'Cms::File') |
| + | elsif snippet_id && snippet = site.snippets.find_by_id(snippet_id) |
| + | Cms::Category.find_or_create_by_label_and_categorized_type("[snippet] #{snippet.slug}", 'Cms::File') |
| + | end |
| + | self.category_ids = { category.id => 1 } if category |
| + | end |
| + | |
| end | |
app/views/cms_admin/files/_index.html.erb
+9
-2
| @@ | @@ -1,6 +1,13 @@ |
| <div id='file_uploads' class='box'> | |
| - | <%= form_for :file, :url => cms_admin_site_files_path(@site), :html => {:multipart => true} do |form| %> |
| + | <% |
| + | url_param = %w(layout page snippet).inject({}) do |c, type| |
| + | c["#{type}_id"] = @_assigns[type].id if @_assigns[type] && @_assigns[type].id |
| + | c |
| + | end |
| + | url_param = url_param.present?? { :file => url_param } : nil |
| + | %> |
| + | <%= form_for :file, :url => cms_admin_site_files_path(@site, url_param), :html => {:multipart => true} do |form| %> |
| <a id='uploader_button' href='#' class='big button'><%= t('.button') %></a> | |
| <%= form.file_field :file, :multiple => true %> | |
| <% end %> | |
| @@ | @@ -10,4 +17,4 @@ |
| <%= render :partial => 'cms_admin/files/file', :object => file %> | |
| <% end %> | |
| </div> | |
| - | </div> |
| + | </div> |
| \ No newline at end of file | |
test/functional/cms_admin/files_controller_test.rb
+18
-0
| @@ | @@ -123,6 +123,24 @@ class CmsAdmin::FilesControllerTest < ActionController::TestCase |
| end | |
| end | |
| + | def test_create_as_xhr_with_page_id |
| + | request.env['HTTP_X_FILE_NAME'] = 'test.pdf' |
| + | request.env['CONTENT_TYPE'] = 'application/pdf' |
| + | |
| + | assert_difference ['Cms::File.count', 'Cms::Category.count', 'Cms::Categorization.count'] do |
| + | xhr :post, :create, :site_id => cms_sites(:default), :file => { |
| + | :page_id => cms_pages(:default).id |
| + | } |
| + | assert_response :success |
| + | |
| + | file = Cms::File.last |
| + | assert_equal 'test.pdf', file.file_file_name |
| + | assert_equal 1, file.categories.count |
| + | assert_equal '[page] /', file.categories.first.label |
| + | assert_equal 'Cms::File', file.categories.first.categorized_type |
| + | end |
| + | end |
| + | |
| def test_update | |
| file = cms_files(:default) | |
| put :update, :site_id => file.site, :id => file, :file => { | |
test/functional/cms_admin/layouts_controller_test.rb
+2
-0
| @@ | @@ -24,6 +24,7 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase |
| assert_equal '{{ cms:page:content:text }}', assigns(:layout).content | |
| assert_template :new | |
| assert_select "form[action=/cms-admin/sites/#{site.id}/layouts]" | |
| + | assert_select "form[action='/cms-admin/sites/#{site.id}/files']" |
| end | |
| def test_get_edit | |
| @@ | @@ -33,6 +34,7 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase |
| assert assigns(:layout) | |
| assert_template :edit | |
| assert_select "form[action=/cms-admin/sites/#{layout.site.id}/layouts/#{layout.id}]" | |
| + | assert_select "form[action='/cms-admin/sites/#{layout.site.id}/files?file%5Blayout_id%5D=#{layout.id}']" |
| end | |
| def test_get_edit_failure | |
test/functional/cms_admin/pages_controller_test.rb
+2
-0
| @@ | @@ -43,6 +43,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| assert_template :new | |
| assert_select "form[action=/cms-admin/sites/#{site.id}/pages]" | |
| assert_select "select[data-url=/cms-admin/sites/#{site.id}/pages/0/form_blocks]" | |
| + | assert_select "form[action='/cms-admin/sites/#{site.id}/files']" |
| end | |
| def test_get_new_with_field_datetime | |
| @@ | @@ -124,6 +125,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| assert_template :edit | |
| assert_select "form[action=/cms-admin/sites/#{page.site.id}/pages/#{page.id}]" | |
| assert_select "select[data-url=/cms-admin/sites/#{page.site.id}/pages/#{page.id}/form_blocks]" | |
| + | assert_select "form[action='/cms-admin/sites/#{page.site.id}/files?file%5Bpage_id%5D=#{page.id}']" |
| end | |
| def test_get_edit_failure | |
test/functional/cms_admin/snippets_controller_test.rb
+2
-0
| @@ | @@ -41,6 +41,7 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| assert assigns(:snippet) | |
| assert_template :new | |
| assert_select "form[action=/cms-admin/sites/#{site.id}/snippets]" | |
| + | assert_select "form[action='/cms-admin/sites/#{site.id}/files']" |
| end | |
| def test_get_edit | |
| @@ | @@ -50,6 +51,7 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| assert assigns(:snippet) | |
| assert_template :edit | |
| assert_select "form[action=/cms-admin/sites/#{snippet.site.id}/snippets/#{snippet.id}]" | |
| + | assert_select "form[action='/cms-admin/sites/#{snippet.site.id}/files?file%5Bsnippet_id%5D=#{snippet.id}']" |
| end | |
| def test_get_edit_failure | |
test/unit/models/file_test.rb
+36
-0
| @@ | @@ -25,6 +25,42 @@ class CmsFileTest < ActiveSupport::TestCase |
| end | |
| end | |
| + | def test_creation_with_layout_link |
| + | assert_difference ['Cms::File.count', 'Cms::Category.count', 'Cms::Categorization.count'] do |
| + | file = cms_sites(:default).files.create( |
| + | :file => fixture_file_upload('files/valid_image.jpg'), |
| + | :layout_id => cms_layouts(:default) |
| + | ) |
| + | assert_equal 1, file.categories.count |
| + | assert_equal '[layout] default', file.categories.first.label |
| + | assert_equal 'Cms::File', file.categories.first.categorized_type |
| + | end |
| + | end |
| + | |
| + | def test_creation_with_page_link |
| + | assert_difference ['Cms::File.count', 'Cms::Category.count', 'Cms::Categorization.count'] do |
| + | file = cms_sites(:default).files.create( |
| + | :file => fixture_file_upload('files/valid_image.jpg'), |
| + | :page_id => cms_pages(:default) |
| + | ) |
| + | assert_equal 1, file.categories.count |
| + | assert_equal '[page] /', file.categories.first.label |
| + | assert_equal 'Cms::File', file.categories.first.categorized_type |
| + | end |
| + | end |
| + | |
| + | def test_creation_with_snippet_link |
| + | assert_difference ['Cms::File.count', 'Cms::Category.count', 'Cms::Categorization.count'] do |
| + | file = cms_sites(:default).files.create( |
| + | :file => fixture_file_upload('files/valid_image.jpg'), |
| + | :snippet_id => cms_snippets(:default) |
| + | ) |
| + | assert_equal 1, file.categories.count |
| + | assert_equal '[snippet] default', file.categories.first.label |
| + | assert_equal 'Cms::File', file.categories.first.categorized_type |
| + | end |
| + | end |
| + | |
| def test_create_failure | |
| assert_no_difference 'Cms::File.count' do | |
| cms_sites(:default).files.create(:file => '') | |