diff --git a/app/models/block.rb b/app/models/block.rb
index 5917e84..1d51c1c 100644
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -23,30 +23,41 @@ class Block < ActiveRecord::Base
# * :article: the article being viewed currently
# * :language: in which language the block will be displayed
def visible?(context = nil)
- if display == 'never'
- return false
- end
+ return false if display == 'never'
+
if context
- if language != 'all' && language != context[:locale]
- return false
- end
- if display == 'home_page_only'
- if context[:article]
- return context[:article] == owner.home_page
- else
- return context[:request_path] == '/'
- end
- elsif display == 'except_home_page'
- if context[:article]
- return context[:article] != owner.home_page
- else
- return context[:request_path] != '/' + (owner.kind_of?(Profile) ? owner.identifier : '')
- end
+ return false if language != 'all' && language != context[:locale]
+
+ begin
+ return self.send("display_#{display}", context)
+ rescue NoMethodError => exception
+ raise "Display '#{display}' is not a valid value."
end
end
+
+ true
+ end
+
+ def display_always(context)
true
end
+ def display_home_page_only(context)
+ if context[:article]
+ return context[:article] == owner.home_page
+ else
+ return context[:request_path] == '/'
+ end
+ end
+
+ def display_except_home_page(context)
+ if context[:article]
+ return context[:article] != owner.home_page
+ else
+ return context[:request_path] != '/' + (owner.kind_of?(Profile) ? owner.identifier : '')
+ end
+ end
+
# The condition for displaying a block. It can assume the following values:
#
# * 'always': the block is always displayed
diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb
index 2fb5951..e7d6957 100644
--- a/test/unit/block_test.rb
+++ b/test/unit/block_test.rb
@@ -205,4 +205,21 @@ class BlockTest < ActiveSupport::TestCase
assert_not_equal block.created_at, duplicated.created_at
assert_not_equal block.updated_at, duplicated.updated_at
end
+
+ should 'support custom display options for blocks visible' do
+ class MyBlock < Block
+ def display
+ 'even_context'
+ end
+
+ def display_even_context(context)
+ context % 2 == 0
+ end
+ end
+
+ block = MyBlock.new
+
+ assert block.visible?(2)
+ assert !block.visible?(3)
+ end
end
--
libgit2 0.21.2