Commit e2fcf1a645e4b90ea5f8472e74df40c797e6dd01

Authored by Larissa Reis
Committed by Rodrigo Souto
1 parent 7d22740a

Making BlogArchiveBlock consider view permissions

  * Needed to rewrite the signature of all the method 'content' of all
  blocks.

  Pair-programmed with: Rodrigo Souto <rodrigo@colivre.coop.br>

(ActionItem1549)
app/helpers/boxes_helper.rb
@@ -81,8 +81,8 @@ module BoxesHelper @@ -81,8 +81,8 @@ module BoxesHelper
81 box_decorator == DontMoveBlocks 81 box_decorator == DontMoveBlocks
82 end 82 end
83 83
84 - def display_block_content(block, main_content = nil)  
85 - content = block.main? ? wrap_main_content(main_content) : block.content 84 + def display_block_content(block, person, main_content = nil)
  85 + content = block.main? ? wrap_main_content(main_content) : block.content({:person => person})
86 result = extract_block_content(content) 86 result = extract_block_content(content)
87 footer_content = extract_block_content(block.footer) 87 footer_content = extract_block_content(block.footer)
88 unless footer_content.blank? 88 unless footer_content.blank?
app/models/article_block.rb
@@ -8,7 +8,7 @@ class ArticleBlock &lt; Block @@ -8,7 +8,7 @@ class ArticleBlock &lt; Block
8 _('This block displays one of your articles. You can edit the block to select which one of your articles is going to be displayed in the block.') 8 _('This block displays one of your articles. You can edit the block to select which one of your articles is going to be displayed in the block.')
9 end 9 end
10 10
11 - def content 11 + def content(args={})
12 block = self 12 block = self
13 lambda do 13 lambda do
14 block_title(block.title) + 14 block_title(block.title) +
app/models/block.rb
@@ -81,7 +81,7 @@ class Block &lt; ActiveRecord::Base @@ -81,7 +81,7 @@ class Block &lt; ActiveRecord::Base
81 # The method can also return <tt>nil</tt>, which means "no content". 81 # The method can also return <tt>nil</tt>, which means "no content".
82 # 82 #
83 # See BoxesHelper#extract_block_content for implementation details. 83 # See BoxesHelper#extract_block_content for implementation details.
84 - def content 84 + def content(args={})
85 "This is block number %d" % self.id 85 "This is block number %d" % self.id
86 end 86 end
87 87
app/models/blog_archives_block.rb
@@ -20,11 +20,15 @@ class BlogArchivesBlock &lt; Block @@ -20,11 +20,15 @@ class BlogArchivesBlock &lt; Block
20 blog_id && owner.blogs.exists?(blog_id) ? owner.blogs.find(blog_id) : owner.blog 20 blog_id && owner.blogs.exists?(blog_id) ? owner.blogs.find(blog_id) : owner.blog
21 end 21 end
22 22
23 - def content 23 + def visible_posts(person)
  24 + blog.posts.native_translations.select {|post| post.display_to?(person)}
  25 + end
  26 +
  27 + def content(args={})
24 owner_blog = self.blog 28 owner_blog = self.blog
25 return nil unless owner_blog 29 return nil unless owner_blog
26 results = '' 30 results = ''
27 - owner_blog.posts.native_translations.group_by {|i| i.published_at.year }.sort_by { |year,count| -year }.each do |year, results_by_year| 31 + visible_posts(args[:person]).group_by {|i| i.published_at.year }.sort_by { |year,count| -year }.each do |year, results_by_year|
28 results << content_tag('li', content_tag('strong', "#{year} (#{results_by_year.size})")) 32 results << content_tag('li', content_tag('strong', "#{year} (#{results_by_year.size})"))
29 results << "<ul class='#{year}-archive'>" 33 results << "<ul class='#{year}-archive'>"
30 results_by_year.group_by{|i| [ ('%02d' % i.published_at.month()), gettext(MONTHS[i.published_at.month() - 1])]}.sort.reverse.each do |month, results_by_month| 34 results_by_year.group_by{|i| [ ('%02d' % i.published_at.month()), gettext(MONTHS[i.published_at.month() - 1])]}.sort.reverse.each do |month, results_by_month|
app/models/categories_block.rb
@@ -28,7 +28,7 @@ class CategoriesBlock &lt; Block @@ -28,7 +28,7 @@ class CategoriesBlock &lt; Block
28 Category.top_level_for(self.owner).from_types(self.category_types) 28 Category.top_level_for(self.owner).from_types(self.category_types)
29 end 29 end
30 30
31 - def content 31 + def content(args={})
32 block = self 32 block = self
33 lambda do 33 lambda do
34 render :file => 'blocks/categories', :locals => { :block => block } 34 render :file => 'blocks/categories', :locals => { :block => block }
app/models/disabled_enterprise_message_block.rb
@@ -12,7 +12,7 @@ class DisabledEnterpriseMessageBlock &lt; Block @@ -12,7 +12,7 @@ class DisabledEnterpriseMessageBlock &lt; Block
12 _('Message') 12 _('Message')
13 end 13 end
14 14
15 - def content 15 + def content(args={})
16 message = self.owner.environment.message_for_disabled_enterprise || '' 16 message = self.owner.environment.message_for_disabled_enterprise || ''
17 lambda do 17 lambda do
18 render :file => 'blocks/disabled_enterprise_message', :locals => {:message => message} 18 render :file => 'blocks/disabled_enterprise_message', :locals => {:message => message}
app/models/environment_statistics_block.rb
@@ -12,7 +12,7 @@ class EnvironmentStatisticsBlock &lt; Block @@ -12,7 +12,7 @@ class EnvironmentStatisticsBlock &lt; Block
12 _('This block presents some statistics about your environment.') 12 _('This block presents some statistics about your environment.')
13 end 13 end
14 14
15 - def content 15 + def content(args={})
16 users = owner.people.visible.count 16 users = owner.people.visible.count
17 enterprises = owner.enterprises.visible.count 17 enterprises = owner.enterprises.visible.count
18 communities = owner.communities.visible.count 18 communities = owner.communities.visible.count
app/models/featured_products_block.rb
@@ -25,7 +25,7 @@ class FeaturedProductsBlock &lt; Block @@ -25,7 +25,7 @@ class FeaturedProductsBlock &lt; Block
25 self.owner.highlighted_products_with_image 25 self.owner.highlighted_products_with_image
26 end 26 end
27 27
28 - def content 28 + def content(args={})
29 block = self 29 block = self
30 lambda do 30 lambda do
31 render :file => 'blocks/featured_products', :locals => { :block => block } 31 render :file => 'blocks/featured_products', :locals => { :block => block }
app/models/feed_reader_block.rb
@@ -78,7 +78,7 @@ class FeedReaderBlock &lt; Block @@ -78,7 +78,7 @@ class FeedReaderBlock &lt; Block
78 self.save! 78 self.save!
79 end 79 end
80 80
81 - def content 81 + def content(args={})
82 block_title(title) + formatted_feed_content 82 block_title(title) + formatted_feed_content
83 end 83 end
84 84
app/models/highlights_block.rb
@@ -28,7 +28,7 @@ class HighlightsBlock &lt; Block @@ -28,7 +28,7 @@ class HighlightsBlock &lt; Block
28 shuffle ? block_images.shuffle : block_images 28 shuffle ? block_images.shuffle : block_images
29 end 29 end
30 30
31 - def content 31 + def content(args={})
32 block = self 32 block = self
33 lambda do 33 lambda do
34 render :file => 'blocks/highlights', :locals => { :block => block } 34 render :file => 'blocks/highlights', :locals => { :block => block }
app/models/link_list_block.rb
@@ -47,7 +47,7 @@ class LinkListBlock &lt; Block @@ -47,7 +47,7 @@ class LinkListBlock &lt; Block
47 _('This block can be used to create a menu of links. You can add, remove and update the links as you wish.') 47 _('This block can be used to create a menu of links. You can add, remove and update the links as you wish.')
48 end 48 end
49 49
50 - def content 50 + def content(args={})
51 block_title(title) + 51 block_title(title) +
52 content_tag('ul', 52 content_tag('ul',
53 links.select{|i| !i[:name].blank? and !i[:address].blank?}.map{|i| content_tag('li', link_html(i))} 53 links.select{|i| !i[:name].blank? and !i[:address].blank?}.map{|i| content_tag('li', link_html(i))}
app/models/location_block.rb
@@ -11,7 +11,7 @@ class LocationBlock &lt; Block @@ -11,7 +11,7 @@ class LocationBlock &lt; Block
11 _('Shows where the profile is on the material world.') 11 _('Shows where the profile is on the material world.')
12 end 12 end
13 13
14 - def content 14 + def content(args={})
15 profile = self.owner 15 profile = self.owner
16 title = self.title 16 title = self.title
17 if profile.lat 17 if profile.lat
app/models/login_block.rb
@@ -8,7 +8,7 @@ class LoginBlock &lt; Block @@ -8,7 +8,7 @@ class LoginBlock &lt; Block
8 _('This block presents a login/logout block.') 8 _('This block presents a login/logout block.')
9 end 9 end
10 10
11 - def content 11 + def content(args={})
12 lambda do 12 lambda do
13 render :file => 'blocks/login_block' 13 render :file => 'blocks/login_block'
14 end 14 end
app/models/main_block.rb
@@ -8,7 +8,7 @@ class MainBlock &lt; Block @@ -8,7 +8,7 @@ class MainBlock &lt; Block
8 _('This block presents the main content of your pages.') 8 _('This block presents the main content of your pages.')
9 end 9 end
10 10
11 - def content 11 + def content(args={})
12 nil 12 nil
13 end 13 end
14 14
app/models/my_network_block.rb
@@ -14,7 +14,7 @@ class MyNetworkBlock &lt; Block @@ -14,7 +14,7 @@ class MyNetworkBlock &lt; Block
14 _('This block displays some info about your networking.') 14 _('This block displays some info about your networking.')
15 end 15 end
16 16
17 - def content 17 + def content(args={})
18 block = self 18 block = self
19 lambda do 19 lambda do
20 render :file => 'blocks/my_network', :locals => { 20 render :file => 'blocks/my_network', :locals => {
app/models/products_block.rb
@@ -16,7 +16,7 @@ class ProductsBlock &lt; Block @@ -16,7 +16,7 @@ class ProductsBlock &lt; Block
16 _('This block presents a list of your products.') 16 _('This block presents a list of your products.')
17 end 17 end
18 18
19 - def content 19 + def content(args={})
20 block_title(title) + 20 block_title(title) +
21 content_tag( 21 content_tag(
22 'ul', 22 'ul',
app/models/profile_image_block.rb
@@ -10,7 +10,7 @@ class ProfileImageBlock &lt; Block @@ -10,7 +10,7 @@ class ProfileImageBlock &lt; Block
10 _('This block presents the profile image') 10 _('This block presents the profile image')
11 end 11 end
12 12
13 - def content 13 + def content(args={})
14 block = self 14 block = self
15 s = show_name 15 s = show_name
16 lambda do 16 lambda do
app/models/profile_info_block.rb
@@ -8,7 +8,7 @@ class ProfileInfoBlock &lt; Block @@ -8,7 +8,7 @@ class ProfileInfoBlock &lt; Block
8 _('Basic information about <i>%{user}</i>: how long <i>%{user}</i> is part of <i>%{env}</i> and useful links.') % { :user => self.owner.name(), :env => self.owner.environment.name() } 8 _('Basic information about <i>%{user}</i>: how long <i>%{user}</i> is part of <i>%{env}</i> and useful links.') % { :user => self.owner.name(), :env => self.owner.environment.name() }
9 end 9 end
10 10
11 - def content 11 + def content(args={})
12 block = self 12 block = self
13 lambda do 13 lambda do
14 render :file => 'blocks/profile_info', :locals => { :block => block } 14 render :file => 'blocks/profile_info', :locals => { :block => block }
app/models/profile_list_block.rb
@@ -37,7 +37,7 @@ class ProfileListBlock &lt; Block @@ -37,7 +37,7 @@ class ProfileListBlock &lt; Block
37 _('Clicking on the people or groups will take you to their home page.') 37 _('Clicking on the people or groups will take you to their home page.')
38 end 38 end
39 39
40 - def content 40 + def content(args={})
41 profiles = self.profile_list 41 profiles = self.profile_list
42 title = self.view_title 42 title = self.view_title
43 nl = "\n" 43 nl = "\n"
app/models/profile_search_block.rb
@@ -4,7 +4,7 @@ class ProfileSearchBlock &lt; Block @@ -4,7 +4,7 @@ class ProfileSearchBlock &lt; Block
4 _('Display a form to search the profile') 4 _('Display a form to search the profile')
5 end 5 end
6 6
7 - def content 7 + def content(args={})
8 title = self.title 8 title = self.title
9 lambda do 9 lambda do
10 render :file => 'blocks/profile_search', :locals => { :title => title } 10 render :file => 'blocks/profile_search', :locals => { :title => title }
app/models/raw_html_block.rb
@@ -6,7 +6,7 @@ class RawHTMLBlock &lt; Block @@ -6,7 +6,7 @@ class RawHTMLBlock &lt; Block
6 6
7 settings_items :html, :type => :text 7 settings_items :html, :type => :text
8 8
9 - def content 9 + def content(args={})
10 (title.blank? ? '' : block_title(title)) + html.to_s 10 (title.blank? ? '' : block_title(title)) + html.to_s
11 end 11 end
12 12
app/models/recent_documents_block.rb
@@ -15,7 +15,7 @@ class RecentDocumentsBlock &lt; Block @@ -15,7 +15,7 @@ class RecentDocumentsBlock &lt; Block
15 settings_items :limit, :type => :integer, :default => 5 15 settings_items :limit, :type => :integer, :default => 5
16 16
17 include ActionController::UrlWriter 17 include ActionController::UrlWriter
18 - def content 18 + def content(args={})
19 docs = self.limit.nil? ? owner.recent_documents : owner.recent_documents(self.limit) 19 docs = self.limit.nil? ? owner.recent_documents : owner.recent_documents(self.limit)
20 20
21 block_title(title) + 21 block_title(title) +
app/models/sellers_search_block.rb
@@ -16,7 +16,7 @@ class SellersSearchBlock &lt; Block @@ -16,7 +16,7 @@ class SellersSearchBlock &lt; Block
16 _('This block presents a search engine for products.') 16 _('This block presents a search engine for products.')
17 end 17 end
18 18
19 - def content 19 + def content(args={})
20 title = self.title 20 title = self.title
21 lambda do 21 lambda do
22 render :file => 'search/_sellers_form', :locals => { :title => title } 22 render :file => 'search/_sellers_form', :locals => { :title => title }
app/models/slideshow_block.rb
@@ -31,7 +31,7 @@ class SlideshowBlock &lt; Block @@ -31,7 +31,7 @@ class SlideshowBlock &lt; Block
31 gallery.images.reject {|item| item.folder?} 31 gallery.images.reject {|item| item.folder?}
32 end 32 end
33 33
34 - def content 34 + def content(args={})
35 block = self 35 block = self
36 if gallery 36 if gallery
37 images = block_images 37 images = block_images
app/models/tags_block.rb
@@ -19,7 +19,7 @@ class TagsBlock &lt; Block @@ -19,7 +19,7 @@ class TagsBlock &lt; Block
19 Try to add some tags to some articles and you'l see your tag cloud growing.") 19 Try to add some tags to some articles and you'l see your tag cloud growing.")
20 end 20 end
21 21
22 - def content 22 + def content(args={})
23 tags = owner.article_tags 23 tags = owner.article_tags
24 return '' if tags.empty? 24 return '' if tags.empty?
25 25
app/views/shared/block.rhtml
1 <% if block.cacheable? && use_cache %> 1 <% if block.cacheable? && use_cache %>
2 <% cache_timeout(block.cache_key, block.timeout.from_now) do %> 2 <% cache_timeout(block.cache_key, block.timeout.from_now) do %>
3 - <%= display_block_content(block, main_content) %> 3 + <%= display_block_content(block, user, main_content) %>
4 <% end %> 4 <% end %>
5 <% else %> 5 <% else %>
6 - <%= display_block_content(block, main_content) %> 6 + <%= display_block_content(block, user, main_content) %>
7 <% end %> 7 <% end %>
test/unit/blog_archives_block_test.rb
@@ -157,4 +157,21 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase @@ -157,4 +157,21 @@ class BlogArchivesBlockTest &lt; ActiveSupport::TestCase
157 end 157 end
158 end 158 end
159 159
  160 + should 'not count articles if the user can\'t see them' do
  161 + person = create_user('testuser').person
  162 + blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog_path')
  163 + block = fast_create(BlogArchivesBlock)
  164 +
  165 + feed = mock()
  166 + feed.stubs(:url).returns('feed_url')
  167 + blog.stubs(:feed).returns(feed)
  168 + block.stubs(:blog).returns(blog)
  169 + block.stubs(:owner).returns(profile)
  170 +
  171 + public_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => true, :published_at => Time.mktime(2012, 'jan'))
  172 + private_post = fast_create(TextileArticle, :profile_id => profile.id, :parent_id => blog.id, :published => false, :published_at => Time.mktime(2012, 'jan'))
  173 +
  174 + assert_match /January \(1\)/, block.content({:person => person})
  175 + assert_match /January \(2\)/, block.content({:person => profile})
  176 + end
160 end 177 end