Commit 256b8bf266b47dc782a2a72ba1b6401628e0f4ba
Exists in
master
and in
29 other branches
Merge commit 'refs/merge-requests/275' of git://gitorious.org/noosfero/noosfero …
…into merge-requests/275 Conflicts: test/unit/application_helper_test.rb
Showing
5 changed files
with
76 additions
and
38 deletions
Show diff stats
app/helpers/application_helper.rb
... | ... | @@ -1402,4 +1402,14 @@ module ApplicationHelper |
1402 | 1402 | @no_design_blocks = true |
1403 | 1403 | end |
1404 | 1404 | |
1405 | + def default_folder_for_image_upload(profile) | |
1406 | + default_folder = profile.folders.find_by_type('Gallery') | |
1407 | + default_folder = profile.folders.find_by_type('Folder') if default_folder.nil? | |
1408 | + default_folder | |
1409 | + end | |
1410 | + | |
1411 | + def content_id_to_str(content) | |
1412 | + content.nil? ? '' : content.id.to_s | |
1413 | + end | |
1414 | + | |
1405 | 1415 | end | ... | ... |
app/helpers/forms_helper.rb
... | ... | @@ -142,38 +142,6 @@ module FormsHelper |
142 | 142 | content_tag('table',rows.join("\n")) |
143 | 143 | end |
144 | 144 | |
145 | - def select_folder(label_text, field_id, collection, default_value=nil, html_options = {}, js_options = {}) | |
146 | - root = profile ? profile.identifier : _("root") | |
147 | - labelled_form_field( | |
148 | - label_text, | |
149 | - select_tag( | |
150 | - field_id, | |
151 | - options_for_select( | |
152 | - [[root, '']] + | |
153 | - collection.collect {|f| [ root + '/' + f.full_name, f.id ] }, | |
154 | - default_value | |
155 | - ), | |
156 | - html_options.merge(js_options) | |
157 | - ) | |
158 | - ) | |
159 | - end | |
160 | - | |
161 | - def select_profile_folder(label_text, field_id, profile, default_value='', html_options = {}, js_options = {}) | |
162 | - result = labelled_form_field( | |
163 | - label_text, | |
164 | - select_tag( | |
165 | - field_id, | |
166 | - options_for_select( | |
167 | - [[profile.identifier, '']] + | |
168 | - profile.folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id ] }, | |
169 | - default_value | |
170 | - ), | |
171 | - html_options.merge(js_options) | |
172 | - ) | |
173 | - ) | |
174 | - return result | |
175 | - end | |
176 | - | |
177 | 145 | def date_field(name, value, format = '%Y-%m-%d', datepicker_options = {}, html_options = {}) |
178 | 146 | datepicker_options[:disabled] ||= false |
179 | 147 | datepicker_options[:alt_field] ||= '' |
... | ... | @@ -295,23 +263,28 @@ module FormsHelper |
295 | 263 | field_id, |
296 | 264 | options_for_select( |
297 | 265 | [[root, '']] + |
298 | - collection.collect {|f| [ root + '/' + f.full_name, f.id ] }, | |
299 | - default_value | |
266 | + collection.collect {|f| [ root + '/' + f.full_name, f.id.to_s ] }, | |
267 | + default_value.to_s | |
300 | 268 | ), |
301 | 269 | html_options.merge(js_options) |
302 | 270 | ) |
303 | 271 | ) |
304 | 272 | end |
305 | 273 | |
306 | - 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 | |
307 | 280 | result = labelled_form_field( |
308 | 281 | label_text, |
309 | 282 | select_tag( |
310 | 283 | field_id, |
311 | 284 | options_for_select( |
312 | 285 | [[profile.identifier, '']] + |
313 | - profile.folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id ] }, | |
314 | - default_value | |
286 | + folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id.to_s ] }, | |
287 | + default_value.to_s | |
315 | 288 | ), |
316 | 289 | html_options.merge(js_options) |
317 | 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 < 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
... | ... | @@ -694,6 +694,38 @@ class ApplicationHelperTest < ActiveSupport::TestCase |
694 | 694 | reference_to_article('x', a) ) |
695 | 695 | end |
696 | 696 | |
697 | + should 'content_id_to_str return the content id as string' do | |
698 | + article = fast_create(Article, :name => 'my article') | |
699 | + response = content_id_to_str(article) | |
700 | + assert_equal String, response.class | |
701 | + assert !response.empty? | |
702 | + end | |
703 | + | |
704 | + should 'content_id_to_str return empty string when receiving nil' do | |
705 | + assert_equal '', content_id_to_str(nil) | |
706 | + end | |
707 | + | |
708 | + should 'select gallery as default folder for image upload' do | |
709 | + profile = create_user('testuser').person | |
710 | + folder = fast_create(Folder, :profile_id => profile.id) | |
711 | + gallery = fast_create(Gallery, :profile_id => profile.id) | |
712 | + blog = fast_create(Blog, :profile_id => profile.id) | |
713 | + assert_equal gallery, default_folder_for_image_upload(profile) | |
714 | + end | |
715 | + | |
716 | + should 'select generic folder as default folder for image upload when no gallery' do | |
717 | + profile = create_user('testuser').person | |
718 | + folder = fast_create(Folder, :profile_id => profile.id) | |
719 | + blog = fast_create(Blog, :profile_id => profile.id) | |
720 | + assert_equal folder, default_folder_for_image_upload(profile) | |
721 | + end | |
722 | + | |
723 | + should 'return nil as default folder for image upload when no gallery or generic folder' do | |
724 | + profile = create_user('testuser').person | |
725 | + blog = fast_create(Blog, :profile_id => profile.id) | |
726 | + assert_nil default_folder_for_image_upload(profile) | |
727 | + end | |
728 | + | |
697 | 729 | protected |
698 | 730 | include NoosferoTestHelper |
699 | 731 | ... | ... |