From 71190c41e60f62b244e91deebb3a1d13e9b7852d Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Tue, 31 Aug 2010 17:19:17 -0300 Subject: [PATCH] Efficiently listing image galleries --- app/models/environment.rb | 4 ++++ app/models/highlights_block.rb | 4 ++++ app/models/slideshow_block.rb | 4 ++++ app/views/box_organizer/_highlights_block.rhtml | 4 ++-- app/views/box_organizer/_slideshow_block.rhtml | 3 +-- test/unit/environment_test.rb | 17 +++++++++++++++++ test/unit/highlights_block_test.rb | 11 +++++++++++ test/unit/profile_test.rb | 8 ++++++++ test/unit/slideshow_block_test.rb | 10 ++++++++++ 9 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/models/environment.rb b/app/models/environment.rb index aec4402..1ae4c7d 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -653,5 +653,9 @@ class Environment < ActiveRecord::Base settings_items :general_cache_in_minutes, :type => :integer, :default => 15 settings_items :profile_cache_in_minutes, :type => :integer, :default => 15 + def image_galleries + portal_community ? portal_community.image_galleries : [] + end + end diff --git a/app/models/highlights_block.rb b/app/models/highlights_block.rb index b78ec36..8fa4570 100644 --- a/app/models/highlights_block.rb +++ b/app/models/highlights_block.rb @@ -35,4 +35,8 @@ class HighlightsBlock < Block end end + def folder_choices + owner.image_galleries + end + end diff --git a/app/models/slideshow_block.rb b/app/models/slideshow_block.rb index e47aa73..c169cf7 100644 --- a/app/models/slideshow_block.rb +++ b/app/models/slideshow_block.rb @@ -48,4 +48,8 @@ class SlideshowBlock < Block end end + def folder_choices + owner.image_galleries + end + end diff --git a/app/views/box_organizer/_highlights_block.rhtml b/app/views/box_organizer/_highlights_block.rhtml index 5a4e23a..ddc74fa 100644 --- a/app/views/box_organizer/_highlights_block.rhtml +++ b/app/views/box_organizer/_highlights_block.rhtml @@ -5,7 +5,7 @@ <% for image in @block.images do %> - <%= 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" %>

+ <%= 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" %>

<%= text_field_tag 'block[images][][address]', image[:address], :class => 'highlight-address', :size => 10 %> <%= text_field_tag 'block[images][][position]', image[:position], :class => 'highlight-position', :size => 3 %> @@ -17,7 +17,7 @@ <%= link_to_function(_('New highlight'), nil, :class => 'button icon-add with-text') do |page| page.insert_html :bottom, 'highlights', content_tag('tr', - 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")) + + 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")) + content_tag('td', text_field_tag('block[images][][address]', nil, :class => 'highlight-address', :size => 10)) + content_tag('td', text_field_tag('block[images][][position]', nil, :class => 'highlight-position', :size => 3)) + content_tag('td', text_field_tag('block[images][][title]', nil, :class => 'highlight-position', :size => 10)) diff --git a/app/views/box_organizer/_slideshow_block.rhtml b/app/views/box_organizer/_slideshow_block.rhtml index 46fbb2d..fb007f7 100644 --- a/app/views/box_organizer/_slideshow_block.rhtml +++ b/app/views/box_organizer/_slideshow_block.rhtml @@ -1,5 +1,4 @@ -<% galleries = @block.box.owner.articles.select {|article| article.folder? && article.display_as_gallery? } %> -<%= labelled_form_field _('Choose a gallery'), select('block', 'gallery_id', galleries.map { |item| +<%= labelled_form_field _('Choose a gallery'), select('block', 'gallery_id', @block.folder_choices.map { |item| [ _('%{gallery} (%{count} images)') % {:gallery => item.path, :count => item.images.reject{|image| image.folder?}.count}, item.id ] }) %> diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index e73a054..7fa5b4d 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -1093,4 +1093,21 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal ['trusted.site.org'], Environment.default.trusted_sites_for_iframe end + + should 'provide list of galleries' do + env = Environment.new + portal = Community.new + env.stubs(:portal_community).returns(portal) + list = [] + portal.expects(:image_galleries).returns(list) + + assert_same list, env.image_galleries + end + + should 'profile empty list of image galleries when there is no portal community' do + p = Environment.new + p.stubs(:portal_community).returns(nil) + assert_equal [], p.image_galleries + end + end diff --git a/test/unit/highlights_block_test.rb b/test/unit/highlights_block_test.rb index 31fc29c..120afc0 100644 --- a/test/unit/highlights_block_test.rb +++ b/test/unit/highlights_block_test.rb @@ -129,4 +129,15 @@ class HighlightsBlockTest < ActiveSupport::TestCase assert_not_equal [i2,i3,i1,i4,i5], block.featured_images end + [Environment, Profile].each do |klass| + should "choose between owner galleries when owner is #{klass.name}" do + owner = fast_create(klass) + + block = HighlightsBlock.new + block.stubs(:owner).returns(owner) + + assert_kind_of Array, block.folder_choices + end + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 60db6b9..eb52f9e 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1785,6 +1785,14 @@ class ProfileTest < Test::Unit::TestCase assert_equal "3 members", c.more_popular_label end + should 'provide list of galleries' do + p = fast_create(Profile) + f1 = Folder.create(:profile => p, :name => "folder1", :view_as => 'image_gallery') + f2 = Folder.create(:profile => p, :name => "folder2", :view_as => 'folder') + + assert_equal [f1], p.image_galleries + end + private def assert_invalid_identifier(id) diff --git a/test/unit/slideshow_block_test.rb b/test/unit/slideshow_block_test.rb index a274592..82c29c1 100644 --- a/test/unit/slideshow_block_test.rb +++ b/test/unit/slideshow_block_test.rb @@ -97,4 +97,14 @@ class SlideshowBlockTest < ActiveSupport::TestCase assert_equal '/bli/thumb.png', block.public_filename_for(image) end + should 'choose between owner image galleries' do + block = SlideshowBlock.new + owner = mock + block.stubs(:owner).returns(owner) + + list = [] + owner.expects(:image_galleries).returns(list) + assert_same list, block.folder_choices + end + end -- libgit2 0.21.2