From 553f852ef8639e0b62cb928c9fcc3a9fd72435db Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Tue, 29 Jun 2010 16:28:57 -0300 Subject: [PATCH] Creating a block to make some images highlighted on environment and enterprises --- app/controllers/admin/environment_design_controller.rb | 2 +- app/controllers/my_profile/profile_design_controller.rb | 1 + app/models/highlights_block.rb | 38 ++++++++++++++++++++++++++++++++++++++ app/views/blocks/highlights.rhtml | 29 +++++++++++++++++++++++++++++ app/views/box_organizer/_highlights_block.rhtml | 32 ++++++++++++++++++++++++++++++++ public/stylesheets/application.css | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/functional/environment_design_controller_test.rb | 2 +- test/functional/profile_design_controller_test.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/highlights_block_test.rb | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 app/models/highlights_block.rb create mode 100644 app/views/blocks/highlights.rhtml create mode 100644 app/views/box_organizer/_highlights_block.rhtml create mode 100644 test/unit/highlights_block_test.rb diff --git a/app/controllers/admin/environment_design_controller.rb b/app/controllers/admin/environment_design_controller.rb index 7dac755..6544329 100644 --- a/app/controllers/admin/environment_design_controller.rb +++ b/app/controllers/admin/environment_design_controller.rb @@ -3,7 +3,7 @@ class EnvironmentDesignController < BoxOrganizerController protect 'edit_environment_design', :environment def available_blocks - @available_blocks ||= [ ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock ] + @available_blocks ||= [ ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock ] end end diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index e3e0bfe..dda3257 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -23,6 +23,7 @@ class ProfileDesignController < BoxOrganizerController # blocks exclusive for enterprises if profile.enterprise? blocks << DisabledEnterpriseMessageBlock + blocks << HighlightsBlock end # product block exclusive for enterprises in environments that permits it diff --git a/app/models/highlights_block.rb b/app/models/highlights_block.rb new file mode 100644 index 0000000..b78ec36 --- /dev/null +++ b/app/models/highlights_block.rb @@ -0,0 +1,38 @@ +class HighlightsBlock < Block + + settings_items :images, :type => Array, :default => [] + settings_items :interval, :type => 'integer', :default => 4 + settings_items :shuffle, :type => 'boolean', :default => false + settings_items :navigation, :type => 'boolean', :default => false + + before_save do |block| + block.images = block.images.delete_if { |i| i[:image_id].blank? and i[:address].blank? and i[:position].blank? and i[:title].blank? } + block.images.each do |i| + i[:image_id] = i[:image_id].to_i + i[:position] = i[:position].to_i + begin + file = UploadedFile.find(i[:image_id]) + i[:image_src] = file.public_filename + rescue + i[:image_src] = nil + end + end + end + + def self.description + _('Highlights') + end + + def featured_images + block_images = images.select{|i| !i[:image_src].nil? }.sort { |x, y| x[:position] <=> y[:position] } + shuffle ? block_images.shuffle : block_images + end + + def content + block = self + lambda do + render :file => 'blocks/highlights', :locals => { :block => block } + end + end + +end diff --git a/app/views/blocks/highlights.rhtml b/app/views/blocks/highlights.rhtml new file mode 100644 index 0000000..05bea3f --- /dev/null +++ b/app/views/blocks/highlights.rhtml @@ -0,0 +1,29 @@ +<%= block_title(block.title) %> +<% if !block.featured_images.empty? %> +
+ + <% if block.navigation %> +
+
+ <% end %> +
+ + +<% else %> + <%= _('Please, edit this block and choose some images') %> +<% end %> diff --git a/app/views/box_organizer/_highlights_block.rhtml b/app/views/box_organizer/_highlights_block.rhtml new file mode 100644 index 0000000..5a4e23a --- /dev/null +++ b/app/views/box_organizer/_highlights_block.rhtml @@ -0,0 +1,32 @@ +<%= _('Highlights') %> +
+ + + <% for image in @block.images do %> + + + + + + + <% end %> +
<%= _('Image') %><%= _('Address') %><%= _('Position') %><%= _('Title') %>
+ <%= 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" %>

+
<%= 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 %><%= text_field_tag 'block[images][][title]', image[:title], :class => 'highlight-title', :size => 10 %>
+
+ +<%= 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', 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)) + ) + + javascript_tag("$('edit-link-list-block').scrollTop = $('edit-link-list-block').scrollHeight") +end %> + +<%= labelled_form_field _('Image transition:'), select('block', 'interval', [[_('No automatic transition'), 0]] + [1, 2, 3, 4, 5, 10, 20, 30, 60].map {|item| [n_('Every 1 second', 'Every %d seconds', item) % item, item]}) %> + +<%= labelled_form_field check_box(:block, :shuffle) + _('Show images in random order'), '' %> + +<%= labelled_form_field check_box(:block, :navigation) + _('Display navigation buttons'), '' %> diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index b5cb703..f01f9f3 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -3639,3 +3639,56 @@ h1#agenda-title { width: 500px; } +/* highlights block stuff */ + +.highlights-block { + float: left; +} + +.highlights-block-pager { + border: 1px solid #ccc; + border-top: 0; + display: inline; + padding-right: 5px; +} + +.highlights-block-pager a { + text-decoration: none; + margin-left: 5px; +} + +.highlights-block-pager a.activeSlide:visited, +.highlights-block-pager a.activeSlide { + color: #000; +} + +.highlights-block a, +.highlights-block a:visited { + text-decoration: none; +} + +.highlights-image-link { + height: 150px; + width: 400px; + overflow: hidden; +} + +.highlights-label { + border: 0; + position: absolute; + bottom: 0; + z-index: 9999; + background-color: #000; + color: #fff; + text-decoration: none; + display: block; + width: 100%; + height: 30px; + overflow: hidden; + margin: 0; + padding: 4px 0; +} + +.highlights-image-link { + border: 1px solid #ccc; +} diff --git a/test/functional/environment_design_controller_test.rb b/test/functional/environment_design_controller_test.rb index a70d40c..90c1f5c 100644 --- a/test/functional/environment_design_controller_test.rb +++ b/test/functional/environment_design_controller_test.rb @@ -6,7 +6,7 @@ class EnvironmentDesignController; def rescue_action(e) raise e end; end class EnvironmentDesignControllerTest < Test::Unit::TestCase - ALL_BLOCKS = [ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock ] + ALL_BLOCKS = [ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock ] def setup @controller = EnvironmentDesignController.new diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index c2af2f7..cd5e9f8 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -5,6 +5,14 @@ class ProfileDesignController; def rescue_action(e) raise e end; end class ProfileDesignControllerTest < Test::Unit::TestCase + COMMOM_BLOCKS = [ ArticleBlock, TagsBlock, RecentDocumentsBlock, ProfileInfoBlock, LinkListBlock, MyNetworkBlock, FeedReaderBlock, ProfileImageBlock, LocationBlock, SlideshowBlock] + PERSON_BLOCKS = COMMOM_BLOCKS + [FriendsBlock, FavoriteEnterprisesBlock, CommunitiesBlock, EnterprisesBlock ] + PERSON_BLOCKS_WITH_MEMBERS = PERSON_BLOCKS + [MembersBlock] + PERSON_BLOCKS_WITH_BLOG = PERSON_BLOCKS + [BlogArchivesBlock] + + ENTERPRISE_BLOCKS = COMMOM_BLOCKS + [DisabledEnterpriseMessageBlock, HighlightsBlock] + ENTERPRISE_BLOCKS_WITH_PRODUCTS_ENABLE = ENTERPRISE_BLOCKS + [ProductsBlock] + attr_reader :holder def setup @controller = ProfileDesignController.new @@ -324,4 +332,69 @@ class ProfileDesignControllerTest < Test::Unit::TestCase assert_tag :tag => 'div', :attributes => {:id => 'access-denied'} end + should 'the person blocks are all available' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(true) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + assert_equal PERSON_BLOCKS, @controller.available_blocks + end + + should 'the person with members blocks are all available' do + profile = mock + profile.stubs(:has_members?).returns(true) + profile.stubs(:person?).returns(true) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + assert_equal [], @controller.available_blocks - PERSON_BLOCKS_WITH_MEMBERS + end + + should 'the person with blog blocks are all available' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(true) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(true) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + assert_equal [], @controller.available_blocks - PERSON_BLOCKS_WITH_BLOG + end + + should 'the enterprise blocks are all available' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(false) + profile.stubs(:enterprise?).returns(true) + profile.stubs(:has_blog?).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(true) + @controller.stubs(:profile).returns(profile) + assert_equal [], @controller.available_blocks - ENTERPRISE_BLOCKS + end + + should 'the enterprise with products for enterprise enable blocks are all available' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(false) + profile.stubs(:enterprise?).returns(true) + profile.stubs(:has_blog?).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + assert_equal [], @controller.available_blocks - ENTERPRISE_BLOCKS_WITH_PRODUCTS_ENABLE + end + end diff --git a/test/unit/highlights_block_test.rb b/test/unit/highlights_block_test.rb new file mode 100644 index 0000000..c403b18 --- /dev/null +++ b/test/unit/highlights_block_test.rb @@ -0,0 +1,130 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class HighlightsBlockTest < ActiveSupport::TestCase + + should 'default describe' do + assert_not_equal Block.description, HighlightsBlock.description + end + + should 'have field images' do + h = HighlightsBlock.new + assert_respond_to h, :images + end + + should 'have field interval' do + h = HighlightsBlock.new + assert_respond_to h, :interval + end + + should 'have field shuffle' do + h = HighlightsBlock.new + assert_respond_to h, :shuffle + end + + should 'have field navigation' do + h = HighlightsBlock.new + assert_respond_to h, :navigation + end + + should 'default value of images' do + h = HighlightsBlock.new + assert_equal [], h.images + end + + should 'default interval between transitions is 4 seconds' do + h = HighlightsBlock.new + assert_equal 4, h.interval + end + + should 'default value of shuffle' do + h = HighlightsBlock.new + assert_equal false, h.shuffle + end + + should 'default value of navigation' do + h = HighlightsBlock.new + assert_equal false, h.navigation + end + + should 'is editable' do + h = HighlightsBlock.new + assert h.editable? + end + + should 'remove images with blank fields' do + h = HighlightsBlock.new(:images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => '', :position => '', :title => ''}]) + h.save! + assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :image_src => nil}], h.images + end + + should 'be able to update display setting' do + user = create_user('testinguser').person + box = fast_create(Box, :owner_id => user.id) + block = HighlightsBlock.create!(:display => 'never', :box => box) + assert block.update_attributes!(:display => 'always') + block.reload + assert_equal 'always', block.display + end + + should 'display highlights block' do + block = HighlightsBlock.new + self.expects(:render).with(:file => 'blocks/highlights', :locals => { :block => block}) + + instance_eval(& block.content) + end + + should 'not list non existent image' do + file = mock() + UploadedFile.expects(:find).with(1).returns(file) + file.expects(:public_filename).returns('address') + block = HighlightsBlock.new(:images => [{:image_id => 1, :address => '/address', :position => 1, :title => 'address'}, {:image_id => '', :address => 'some', :position => '2', :title => 'Some'}]) + block.save! + block.reload + assert_equal 2, block.images.count + assert_equal [{:image_id => 1, :address => '/address', :position => 1, :title => 'address', :image_src => 'address'}], block.featured_images + end + + should 'list images in order' do + f1 = mock() + f1.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(1).returns(f1) + f2 = mock() + f2.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(2).returns(f2) + f3 = mock() + f3.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(3).returns(f3) + block = HighlightsBlock.new + i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'} + i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'} + i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'} + block.images = [i1,i2,i3] + block.save! + block.reload + assert_equal [i1,i2,i3], block.images + assert_equal [i2,i3,i1], block.featured_images + end + + should 'list images randomically' do + f1 = mock() + f1.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(1).returns(f1) + f2 = mock() + f2.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(2).returns(f2) + f3 = mock() + f3.expects(:public_filename).returns('address') + UploadedFile.expects(:find).with(3).returns(f3) + block = HighlightsBlock.new + i1 = {:image_id => 1, :address => '/address', :position => 3, :title => 'address'} + i2 = {:image_id => 2, :address => '/address', :position => 1, :title => 'address'} + i3 = {:image_id => 3, :address => '/address', :position => 2, :title => 'address'} + block.images = [i1,i2,i3] + block.shuffle = true + block.save! + block.reload + assert_equal [i1,i2,i3], block.images + assert_not_equal [i2,i3,i1], block.featured_images + end + +end -- libgit2 0.21.2