Commit dbd6c201d9ae21a35cf878ee83a6dadfab43e0ce
1 parent
0770524c
Exists in
master
and in
29 other branches
accept lambda in comment_actions hot spot
Showing
3 changed files
with
14 additions
and
2 deletions
Show diff stats
app/helpers/comment_helper.rb
| @@ -31,7 +31,10 @@ module CommentHelper | @@ -31,7 +31,10 @@ module CommentHelper | ||
| 31 | 31 | ||
| 32 | def links_for_comment_actions(comment) | 32 | def links_for_comment_actions(comment) |
| 33 | actions = [link_for_report_abuse(comment), link_for_spam(comment), link_for_edit(comment), link_for_remove(comment)] | 33 | actions = [link_for_report_abuse(comment), link_for_spam(comment), link_for_edit(comment), link_for_remove(comment)] |
| 34 | - actions += @plugins.dispatch(:comment_actions, comment) | 34 | + @plugins.dispatch(:comment_actions, comment).collect do |action| |
| 35 | + actions << (action.kind_of?(Proc) ? self.instance_eval(&action) : action) | ||
| 36 | + end | ||
| 37 | + actions.flatten | ||
| 35 | end | 38 | end |
| 36 | 39 | ||
| 37 | def link_for_report_abuse(comment) | 40 | def link_for_report_abuse(comment) |
lib/noosfero/plugin.rb
| @@ -278,7 +278,7 @@ class Noosfero::Plugin | @@ -278,7 +278,7 @@ class Noosfero::Plugin | ||
| 278 | end | 278 | end |
| 279 | 279 | ||
| 280 | # Adds extra actions for comments | 280 | # Adds extra actions for comments |
| 281 | - # | 281 | + # returns = list of hashes or lambda block that creates a list of hashes |
| 282 | # example: | 282 | # example: |
| 283 | # | 283 | # |
| 284 | # def comment_actions(comment) | 284 | # def comment_actions(comment) |
test/unit/comment_helper_test.rb
| @@ -37,6 +37,15 @@ class CommentHelperTest < ActiveSupport::TestCase | @@ -37,6 +37,15 @@ class CommentHelperTest < ActiveSupport::TestCase | ||
| 37 | links = links_for_comment_actions(comment) | 37 | links = links_for_comment_actions(comment) |
| 38 | assert_includes links, plugin_action | 38 | assert_includes links, plugin_action |
| 39 | end | 39 | end |
| 40 | + | ||
| 41 | + should 'include lambda actions of plugins in menu' do | ||
| 42 | + comment = Comment.new | ||
| 43 | + plugin_action = lambda{[{:link => 'plugin_action'}, {:link => 'plugin_action2'}]} | ||
| 44 | + @plugins.stubs(:dispatch).returns([plugin_action]) | ||
| 45 | + links = links_for_comment_actions(comment) | ||
| 46 | + assert_includes links, {:link => 'plugin_action'} | ||
| 47 | + assert_includes links, {:link => 'plugin_action2'} | ||
| 48 | + end | ||
| 40 | 49 | ||
| 41 | should 'return link for report abuse action when comment has a author' do | 50 | should 'return link for report abuse action when comment has a author' do |
| 42 | comment = Comment.new | 51 | comment = Comment.new |