Commit 71190c41e60f62b244e91deebb3a1d13e9b7852d

Authored by Antonio Terceiro
1 parent 13e2837d

Efficiently listing image galleries

(ActionItem1666)
app/models/environment.rb
@@ -653,5 +653,9 @@ class Environment < ActiveRecord::Base @@ -653,5 +653,9 @@ class Environment < ActiveRecord::Base
653 settings_items :general_cache_in_minutes, :type => :integer, :default => 15 653 settings_items :general_cache_in_minutes, :type => :integer, :default => 15
654 settings_items :profile_cache_in_minutes, :type => :integer, :default => 15 654 settings_items :profile_cache_in_minutes, :type => :integer, :default => 15
655 655
  656 + def image_galleries
  657 + portal_community ? portal_community.image_galleries : []
  658 + end
  659 +
656 end 660 end
657 661
app/models/highlights_block.rb
@@ -35,4 +35,8 @@ class HighlightsBlock < Block @@ -35,4 +35,8 @@ class HighlightsBlock < Block
35 end 35 end
36 end 36 end
37 37
  38 + def folder_choices
  39 + owner.image_galleries
  40 + end
  41 +
38 end 42 end
app/models/slideshow_block.rb
@@ -48,4 +48,8 @@ class SlideshowBlock < Block @@ -48,4 +48,8 @@ class SlideshowBlock < Block
48 end 48 end
49 end 49 end
50 50
  51 + def folder_choices
  52 + owner.image_galleries
  53 + end
  54 +
51 end 55 end
app/views/box_organizer/_highlights_block.rhtml
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 <% for image in @block.images do %> 5 <% for image in @block.images do %>
6 <tr> 6 <tr>
7 <td> 7 <td>
8 - <%= select_tag 'block[images][][image_id]', content_tag(:option) + option_groups_from_collection_for_select(@block.box.owner.articles.select{|article| article.folder? && article.display_as_gallery? } , :images, :name, :id, :name, image[:image_id].to_i), :style => "width: 100px" %></p> 8 + <%= select_tag 'block[images][][image_id]', content_tag(:option) + option_groups_from_collection_for_select(@block.folder_choices, :images, :name, :id, :name, image[:image_id].to_i), :style => "width: 100px" %></p>
9 </td> 9 </td>
10 <td><%= text_field_tag 'block[images][][address]', image[:address], :class => 'highlight-address', :size => 10 %></td> 10 <td><%= text_field_tag 'block[images][][address]', image[:address], :class => 'highlight-address', :size => 10 %></td>
11 <td><%= text_field_tag 'block[images][][position]', image[:position], :class => 'highlight-position', :size => 3 %></td> 11 <td><%= text_field_tag 'block[images][][position]', image[:position], :class => 'highlight-position', :size => 3 %></td>
@@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
17 17
18 <%= link_to_function(_('New highlight'), nil, :class => 'button icon-add with-text') do |page| 18 <%= link_to_function(_('New highlight'), nil, :class => 'button icon-add with-text') do |page|
19 page.insert_html :bottom, 'highlights', content_tag('tr', 19 page.insert_html :bottom, 'highlights', content_tag('tr',
20 - content_tag('td', select_tag('block[images][][image_id]', content_tag(:option) + option_groups_from_collection_for_select(@block.box.owner.articles.select{|article| article.folder? && article.display_as_gallery? } , :images, :name, :id, :name), :style => "width: 100px")) + 20 + content_tag('td', select_tag('block[images][][image_id]', content_tag(:option) + option_groups_from_collection_for_select(@block.folder_choices, :images, :name, :id, :name), :style => "width: 100px")) +
21 content_tag('td', text_field_tag('block[images][][address]', nil, :class => 'highlight-address', :size => 10)) + 21 content_tag('td', text_field_tag('block[images][][address]', nil, :class => 'highlight-address', :size => 10)) +
22 content_tag('td', text_field_tag('block[images][][position]', nil, :class => 'highlight-position', :size => 3)) + 22 content_tag('td', text_field_tag('block[images][][position]', nil, :class => 'highlight-position', :size => 3)) +
23 content_tag('td', text_field_tag('block[images][][title]', nil, :class => 'highlight-position', :size => 10)) 23 content_tag('td', text_field_tag('block[images][][title]', nil, :class => 'highlight-position', :size => 10))
app/views/box_organizer/_slideshow_block.rhtml
1 -<% galleries = @block.box.owner.articles.select {|article| article.folder? && article.display_as_gallery? } %>  
2 -<%= labelled_form_field _('Choose a gallery'), select('block', 'gallery_id', galleries.map { |item| 1 +<%= labelled_form_field _('Choose a gallery'), select('block', 'gallery_id', @block.folder_choices.map { |item|
3 [ _('%{gallery} (%{count} images)') % {:gallery => item.path, :count => item.images.reject{|image| image.folder?}.count}, item.id ] 2 [ _('%{gallery} (%{count} images)') % {:gallery => item.path, :count => item.images.reject{|image| image.folder?}.count}, item.id ]
4 }) %> 3 }) %>
5 4
test/unit/environment_test.rb
@@ -1093,4 +1093,21 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -1093,4 +1093,21 @@ class EnvironmentTest &lt; Test::Unit::TestCase
1093 1093
1094 assert_equal ['trusted.site.org'], Environment.default.trusted_sites_for_iframe 1094 assert_equal ['trusted.site.org'], Environment.default.trusted_sites_for_iframe
1095 end 1095 end
  1096 +
  1097 + should 'provide list of galleries' do
  1098 + env = Environment.new
  1099 + portal = Community.new
  1100 + env.stubs(:portal_community).returns(portal)
  1101 + list = []
  1102 + portal.expects(:image_galleries).returns(list)
  1103 +
  1104 + assert_same list, env.image_galleries
  1105 + end
  1106 +
  1107 + should 'profile empty list of image galleries when there is no portal community' do
  1108 + p = Environment.new
  1109 + p.stubs(:portal_community).returns(nil)
  1110 + assert_equal [], p.image_galleries
  1111 + end
  1112 +
1096 end 1113 end
test/unit/highlights_block_test.rb
@@ -129,4 +129,15 @@ class HighlightsBlockTest &lt; ActiveSupport::TestCase @@ -129,4 +129,15 @@ class HighlightsBlockTest &lt; ActiveSupport::TestCase
129 assert_not_equal [i2,i3,i1,i4,i5], block.featured_images 129 assert_not_equal [i2,i3,i1,i4,i5], block.featured_images
130 end 130 end
131 131
  132 + [Environment, Profile].each do |klass|
  133 + should "choose between owner galleries when owner is #{klass.name}" do
  134 + owner = fast_create(klass)
  135 +
  136 + block = HighlightsBlock.new
  137 + block.stubs(:owner).returns(owner)
  138 +
  139 + assert_kind_of Array, block.folder_choices
  140 + end
  141 + end
  142 +
132 end 143 end
test/unit/profile_test.rb
@@ -1785,6 +1785,14 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -1785,6 +1785,14 @@ class ProfileTest &lt; Test::Unit::TestCase
1785 assert_equal "3 members", c.more_popular_label 1785 assert_equal "3 members", c.more_popular_label
1786 end 1786 end
1787 1787
  1788 + should 'provide list of galleries' do
  1789 + p = fast_create(Profile)
  1790 + f1 = Folder.create(:profile => p, :name => "folder1", :view_as => 'image_gallery')
  1791 + f2 = Folder.create(:profile => p, :name => "folder2", :view_as => 'folder')
  1792 +
  1793 + assert_equal [f1], p.image_galleries
  1794 + end
  1795 +
1788 private 1796 private
1789 1797
1790 def assert_invalid_identifier(id) 1798 def assert_invalid_identifier(id)
test/unit/slideshow_block_test.rb
@@ -97,4 +97,14 @@ class SlideshowBlockTest &lt; ActiveSupport::TestCase @@ -97,4 +97,14 @@ class SlideshowBlockTest &lt; ActiveSupport::TestCase
97 assert_equal '/bli/thumb.png', block.public_filename_for(image) 97 assert_equal '/bli/thumb.png', block.public_filename_for(image)
98 end 98 end
99 99
  100 + should 'choose between owner image galleries' do
  101 + block = SlideshowBlock.new
  102 + owner = mock
  103 + block.stubs(:owner).returns(owner)
  104 +
  105 + list = []
  106 + owner.expects(:image_galleries).returns(list)
  107 + assert_same list, block.folder_choices
  108 + end
  109 +
100 end 110 end