Commit c722f4b7c436ae4bcdd717a40d2dd8bfe0460dc6
Exists in
master
and in
23 other branches
Merge branch 'stable' of https://gitlab.com/noosfero/noosfero into stable
Showing
5 changed files
with
44 additions
and
2 deletions
Show diff stats
app/models/block.rb
| @@ -130,7 +130,7 @@ class Block < ActiveRecord::Base | @@ -130,7 +130,7 @@ class Block < ActiveRecord::Base | ||
| 130 | end | 130 | end |
| 131 | 131 | ||
| 132 | alias :active_record_cache_key :cache_key | 132 | alias :active_record_cache_key :cache_key |
| 133 | - def cache_key(language='en') | 133 | + def cache_key(language='en', user=nil) |
| 134 | active_record_cache_key+'-'+language | 134 | active_record_cache_key+'-'+language |
| 135 | end | 135 | end |
| 136 | 136 |
app/models/members_block.rb
| @@ -36,4 +36,15 @@ class MembersBlock < ProfileListBlock | @@ -36,4 +36,15 @@ class MembersBlock < ProfileListBlock | ||
| 36 | } | 36 | } |
| 37 | end | 37 | end |
| 38 | 38 | ||
| 39 | + def cache_key(language='en', user=nil) | ||
| 40 | + logged = '' | ||
| 41 | + if user | ||
| 42 | + logged += '-logged-in' | ||
| 43 | + if user.is_member_of? self.owner | ||
| 44 | + logged += '-member' | ||
| 45 | + end | ||
| 46 | + end | ||
| 47 | + super + logged | ||
| 48 | + end | ||
| 49 | + | ||
| 39 | end | 50 | end |
app/views/shared/block.rhtml
| 1 | <% if block.cacheable? && use_cache %> | 1 | <% if block.cacheable? && use_cache %> |
| 2 | - <% cache_timeout(block.cache_key(language), block.timeout) do %> | 2 | + <% cache_timeout(block.cache_key(language, user), block.timeout) do %> |
| 3 | <%= display_block_content(block, user, main_content) %> | 3 | <%= display_block_content(block, user, main_content) %> |
| 4 | <% end %> | 4 | <% end %> |
| 5 | <% else %> | 5 | <% else %> |
test/unit/block_test.rb
| @@ -165,4 +165,11 @@ class BlockTest < ActiveSupport::TestCase | @@ -165,4 +165,11 @@ class BlockTest < ActiveSupport::TestCase | ||
| 165 | conditions = Block.expire_on | 165 | conditions = Block.expire_on |
| 166 | assert conditions[:environment].kind_of?(Array) | 166 | assert conditions[:environment].kind_of?(Array) |
| 167 | end | 167 | end |
| 168 | + | ||
| 169 | + should 'accept user as parameter on cache_key without change its value' do | ||
| 170 | + person = fast_create(Person) | ||
| 171 | + block = Block.new | ||
| 172 | + assert_equal block.cache_key('en'), block.cache_key('en', person) | ||
| 173 | + end | ||
| 174 | + | ||
| 168 | end | 175 | end |
test/unit/members_block_test.rb
| @@ -35,4 +35,28 @@ class MembersBlockTest < ActiveSupport::TestCase | @@ -35,4 +35,28 @@ class MembersBlockTest < ActiveSupport::TestCase | ||
| 35 | assert_same list, block.profiles | 35 | assert_same list, block.profiles |
| 36 | end | 36 | end |
| 37 | 37 | ||
| 38 | + should 'use logged-in to compose cache key' do | ||
| 39 | + person = fast_create(Person) | ||
| 40 | + community = fast_create(Community) | ||
| 41 | + block = MembersBlock.create | ||
| 42 | + block.expects(:owner).returns(community) | ||
| 43 | + | ||
| 44 | + assert_match(/-logged-in/,block.cache_key('en', person)) | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + should 'use logged-in and member to compose cache key for members' do | ||
| 48 | + person = fast_create(Person) | ||
| 49 | + community = fast_create(Community) | ||
| 50 | + community.add_member person | ||
| 51 | + block = MembersBlock.create | ||
| 52 | + block.expects(:owner).returns(community) | ||
| 53 | + | ||
| 54 | + assert_match(/-logged-in-member/,block.cache_key('en', person)) | ||
| 55 | + end | ||
| 56 | + | ||
| 57 | + should 'not change block cache key if user is nil' do | ||
| 58 | + block = MembersBlock.new | ||
| 59 | + assert_equal block.cache_key('en'), block.cache_key('en', nil) | ||
| 60 | + end | ||
| 61 | + | ||
| 38 | end | 62 | end |