Commit f7c887983df69f0a733eb1fd1ba8097c1f28a444
1 parent
43535f4f
Exists in
master
and in
28 other branches
block: add support for custom display modes for blocks
Showing
2 changed files
with
46 additions
and
18 deletions
Show diff stats
app/models/block.rb
... | ... | @@ -23,30 +23,41 @@ class Block < ActiveRecord::Base |
23 | 23 | # * <tt>:article</tt>: the article being viewed currently |
24 | 24 | # * <tt>:language</tt>: in which language the block will be displayed |
25 | 25 | def visible?(context = nil) |
26 | - if display == 'never' | |
27 | - return false | |
28 | - end | |
26 | + return false if display == 'never' | |
27 | + | |
29 | 28 | if context |
30 | - if language != 'all' && language != context[:locale] | |
31 | - return false | |
32 | - end | |
33 | - if display == 'home_page_only' | |
34 | - if context[:article] | |
35 | - return context[:article] == owner.home_page | |
36 | - else | |
37 | - return context[:request_path] == '/' | |
38 | - end | |
39 | - elsif display == 'except_home_page' | |
40 | - if context[:article] | |
41 | - return context[:article] != owner.home_page | |
42 | - else | |
43 | - return context[:request_path] != '/' + (owner.kind_of?(Profile) ? owner.identifier : '') | |
44 | - end | |
29 | + return false if language != 'all' && language != context[:locale] | |
30 | + | |
31 | + begin | |
32 | + return self.send("display_#{display}", context) | |
33 | + rescue NoMethodError => exception | |
34 | + raise "Display '#{display}' is not a valid value." | |
45 | 35 | end |
46 | 36 | end |
37 | + | |
38 | + true | |
39 | + end | |
40 | + | |
41 | + def display_always(context) | |
47 | 42 | true |
48 | 43 | end |
49 | 44 | |
45 | + def display_home_page_only(context) | |
46 | + if context[:article] | |
47 | + return context[:article] == owner.home_page | |
48 | + else | |
49 | + return context[:request_path] == '/' | |
50 | + end | |
51 | + end | |
52 | + | |
53 | + def display_except_home_page(context) | |
54 | + if context[:article] | |
55 | + return context[:article] != owner.home_page | |
56 | + else | |
57 | + return context[:request_path] != '/' + (owner.kind_of?(Profile) ? owner.identifier : '') | |
58 | + end | |
59 | + end | |
60 | + | |
50 | 61 | # The condition for displaying a block. It can assume the following values: |
51 | 62 | # |
52 | 63 | # * <tt>'always'</tt>: the block is always displayed | ... | ... |
test/unit/block_test.rb
... | ... | @@ -205,4 +205,21 @@ class BlockTest < ActiveSupport::TestCase |
205 | 205 | assert_not_equal block.created_at, duplicated.created_at |
206 | 206 | assert_not_equal block.updated_at, duplicated.updated_at |
207 | 207 | end |
208 | + | |
209 | + should 'support custom display options for blocks visible' do | |
210 | + class MyBlock < Block | |
211 | + def display | |
212 | + 'even_context' | |
213 | + end | |
214 | + | |
215 | + def display_even_context(context) | |
216 | + context % 2 == 0 | |
217 | + end | |
218 | + end | |
219 | + | |
220 | + block = MyBlock.new | |
221 | + | |
222 | + assert block.visible?(2) | |
223 | + assert !block.visible?(3) | |
224 | + end | |
208 | 225 | end | ... | ... |