Commit 82426f9f0f0d27c77b5644dec490b772ede86b8d
1 parent
1bcdb1dc
Exists in
master
and in
29 other branches
ActionItem154: refactoring: extracting the logic of block content generation into its own method
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1344 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
28 additions
and
17 deletions
Show diff stats
app/helpers/boxes_helper.rb
... | ... | @@ -41,27 +41,30 @@ module BoxesHelper |
41 | 41 | |
42 | 42 | def display_block(block, main_content = nil) |
43 | 43 | content = block.main? ? main_content : block.content |
44 | - result = | |
45 | - case content | |
46 | - when Hash | |
47 | - content_tag('iframe', '', :src => url_for(content)) | |
48 | - when String | |
49 | - if content =~ /^https?:\/\// | |
50 | - content_tag('iframe', '', :src => content) | |
51 | - else | |
52 | - content | |
53 | - end | |
54 | - when Proc | |
55 | - self.instance_eval(&content) | |
56 | - else | |
57 | - raise "Unsupported content for block (#{content.class})" | |
58 | - end | |
44 | + result = extract_block_content(content) | |
59 | 45 | |
60 | 46 | classes = ['block', block.css_class_name ].uniq.join(' ') |
61 | 47 | |
62 | 48 | box_decorator.block_target(block.box, block) + content_tag('div', result + box_decorator.block_edit_buttons(block), :class => classes, :id => "block-#{block.id}") + box_decorator.block_handle(block) |
63 | 49 | end |
64 | 50 | |
51 | + def extract_block_content(content) | |
52 | + case content | |
53 | + when Hash | |
54 | + content_tag('iframe', '', :src => url_for(content)) | |
55 | + when String | |
56 | + if content =~ /^https?:\/\// | |
57 | + content_tag('iframe', '', :src => content) | |
58 | + else | |
59 | + content | |
60 | + end | |
61 | + when Proc | |
62 | + self.instance_eval(&content) | |
63 | + else | |
64 | + raise "Unsupported content for block (#{content.class})" | |
65 | + end | |
66 | + end | |
67 | + | |
65 | 68 | module DontMoveBlocks |
66 | 69 | # does nothing |
67 | 70 | def self.block_target(box, block = nil) | ... | ... |
app/models/block.rb
... | ... | @@ -16,8 +16,16 @@ class Block < ActiveRecord::Base |
16 | 16 | _('A dummy block.') |
17 | 17 | end |
18 | 18 | |
19 | - # TODO: must have some way to have access to request information (mainly the | |
20 | - # current user) | |
19 | + # Returns the content to be used for this block. | |
20 | + # | |
21 | + # This method can return several types of objects: | |
22 | + # | |
23 | + # * <tt>String</tt>: if the string starts with <tt>http://</tt> or <tt>https://</tt>, then it is assumed to be address of an IFRAME. Otherwise it's is used as regular HTML. | |
24 | + # * <tt>Hash</tt>: the hash is used to build an URL that is used as the address for a IFRAME. | |
25 | + # * <tt>Proc</tt>: the Proc is evaluated in the scope of BoxesHelper. The | |
26 | + # block can then use <tt>render</tt>, <tt>link_to</tt>, etc. | |
27 | + # | |
28 | + # See BoxesHelper#extract_block_content for implementation details. | |
21 | 29 | def content |
22 | 30 | "This is block number %d" % self.id |
23 | 31 | end | ... | ... |