Commit 4de6c173a9118a6968df462b8287dfd3c0e18f16

Authored by Aurélio A. Heckert
1 parent f89c00cc

Filter useful folders to upload images and pre-select a gallery

closes ActionItem2567
app/helpers/application_helper.rb
... ... @@ -1423,4 +1423,14 @@ module ApplicationHelper
1423 1423 @no_design_blocks = true
1424 1424 end
1425 1425  
  1426 + def default_folder_for_image_upload(profile)
  1427 + default_folder = profile.folders.find_by_type('Gallery')
  1428 + default_folder = profile.folders.find_by_type('Folder') if default_folder.nil?
  1429 + default_folder
  1430 + end
  1431 +
  1432 + def content_id_to_str(content)
  1433 + content.nil? ? '' : content.id.to_s
  1434 + end
  1435 +
1426 1436 end
... ...
app/helpers/forms_helper.rb
... ... @@ -263,23 +263,28 @@ module FormsHelper
263 263 field_id,
264 264 options_for_select(
265 265 [[root, '']] +
266   - collection.collect {|f| [ root + '/' + f.full_name, f.id ] },
267   - default_value
  266 + collection.collect {|f| [ root + '/' + f.full_name, f.id.to_s ] },
  267 + default_value.to_s
268 268 ),
269 269 html_options.merge(js_options)
270 270 )
271 271 )
272 272 end
273 273  
274   - def select_profile_folder(label_text, field_id, profile, default_value='', html_options = {}, js_options = {})
  274 + def select_profile_folder(label_text, field_id, profile, default_value='', html_options = {}, js_options = {}, find_options = {})
  275 + if find_options.empty?
  276 + folders = profile.folders
  277 + else
  278 + folders = profile.folders.find :all, find_options
  279 + end
275 280 result = labelled_form_field(
276 281 label_text,
277 282 select_tag(
278 283 field_id,
279 284 options_for_select(
280 285 [[profile.identifier, '']] +
281   - profile.folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id ] },
282   - default_value
  286 + folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id.to_s ] },
  287 + default_value.to_s
283 288 ),
284 289 html_options.merge(js_options)
285 290 )
... ...
app/views/cms/_text_editor_sidebar.rhtml
... ... @@ -9,7 +9,12 @@
9 9 <div id='media-upload-form'>
10 10 <% form_tag({ :action => 'media_upload' }, :multipart => true) do %>
11 11 <div class='formfield'>
12   - <%= select_profile_folder(_('Choose folder to upload files:'), :parent_id, profile) %>
  12 + <% default_folder = content_id_to_str default_folder_for_image_upload(profile) %>
  13 + <%= select_profile_folder(
  14 + _('Choose folder to upload files:'),
  15 + :parent_id, profile, default_folder, {}, {},
  16 + {:conditions => 'type="Folder" or type="Gallery"'}
  17 + ) %>
13 18 </div>
14 19 <p><%= file_field_tag('file1') %></p>
15 20 <p><%= file_field_tag('file2') %></p>
... ...
test/functional/cms_controller_test.rb
... ... @@ -1612,6 +1612,24 @@ class CmsControllerTest &lt; ActionController::TestCase
1612 1612 assert_template 'access_denied.rhtml'
1613 1613 end
1614 1614  
  1615 + should 'filter profile folders to select' do
  1616 + env = Environment.default
  1617 + env.enable 'media_panel'
  1618 + env.save!
  1619 + folder = fast_create(Folder, :name=>'a', :profile_id => profile.id)
  1620 + gallery = fast_create(Gallery, :name=>'b', :profile_id => profile.id)
  1621 + blog = fast_create(Blog, :name=>'c', :profile_id => profile.id)
  1622 + article = fast_create(TinyMceArticle, :profile_id => profile.id)
  1623 + get :edit, :profile => profile.identifier, :id => article.id
  1624 + assert_template 'edit'
  1625 + assert_tag :tag => 'select', :attributes => { :name => "parent_id" },
  1626 + :descendant => { :tag => "option",
  1627 + :attributes => { :selected => 'selected', :value => gallery.id.to_s }}
  1628 + assert_no_tag :tag => 'select', :attributes => { :name => "parent_id" },
  1629 + :descendant => { :tag => "option",
  1630 + :attributes => { :value => blog.id.to_s }}
  1631 + end
  1632 +
1615 1633 protected
1616 1634  
1617 1635 # FIXME this is to avoid adding an extra dependency for a proper JSON parser.
... ...
test/unit/application_helper_test.rb
... ... @@ -657,6 +657,38 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
657 657 assert_not_nil add_zoom_to_images
658 658 end
659 659  
  660 + should 'content_id_to_str return the content id as string' do
  661 + article = fast_create(Article, :name => 'my article')
  662 + response = content_id_to_str(article)
  663 + assert_equal String, response.class
  664 + assert !response.empty?
  665 + end
  666 +
  667 + should 'content_id_to_str return empty string when receiving nil' do
  668 + assert_equal '', content_id_to_str(nil)
  669 + end
  670 +
  671 + should 'select gallery as default folder for image upload' do
  672 + profile = create_user('testuser').person
  673 + folder = fast_create(Folder, :profile_id => profile.id)
  674 + gallery = fast_create(Gallery, :profile_id => profile.id)
  675 + blog = fast_create(Blog, :profile_id => profile.id)
  676 + assert_equal gallery, default_folder_for_image_upload(profile)
  677 + end
  678 +
  679 + should 'select generic folder as default folder for image upload when no gallery' do
  680 + profile = create_user('testuser').person
  681 + folder = fast_create(Folder, :profile_id => profile.id)
  682 + blog = fast_create(Blog, :profile_id => profile.id)
  683 + assert_equal folder, default_folder_for_image_upload(profile)
  684 + end
  685 +
  686 + should 'return nil as default folder for image upload when no gallery or generic folder' do
  687 + profile = create_user('testuser').person
  688 + blog = fast_create(Blog, :profile_id => profile.id)
  689 + assert_nil default_folder_for_image_upload(profile)
  690 + end
  691 +
660 692 protected
661 693 include NoosferoTestHelper
662 694  
... ...