Commit f5c2a127d2e2de8bd51b558ed9a48098365584ad
1 parent
83c8889e
Exists in
master
and in
22 other branches
hotspot: refactor profile_image_link hotspot
* The first_impl method was already implemented as dispatch_first
(which was probably incorporated after this request was done). But
I utilized the idea proposed here to use the default value for the
hotspot instead of nil.
* I changed the hotspot to pass all the parameters passed to the
method since it might be useful for the plugins in the future.
(ActionItem2666)
Showing
4 changed files
with
12 additions
and
17 deletions
Show diff stats
app/helpers/application_helper.rb
| @@ -558,7 +558,7 @@ module ApplicationHelper | @@ -558,7 +558,7 @@ module ApplicationHelper | ||
| 558 | # displays a link to the profile homepage with its image (as generated by | 558 | # displays a link to the profile homepage with its image (as generated by |
| 559 | # #profile_image) and its name below it. | 559 | # #profile_image) and its name below it. |
| 560 | def profile_image_link( profile, size=:portrait, tag='li', extra_info = nil ) | 560 | def profile_image_link( profile, size=:portrait, tag='li', extra_info = nil ) |
| 561 | - if content = @plugins.first_impl(:profile_image_link, profile) | 561 | + if content = @plugins.dispatch_first(:profile_image_link, profile, size, tag, extra_info) |
| 562 | return instance_eval(&content) | 562 | return instance_eval(&content) |
| 563 | end | 563 | end |
| 564 | name = profile.short_name | 564 | name = profile.short_name |
lib/noosfero/plugin.rb
| @@ -152,7 +152,7 @@ class Noosfero::Plugin | @@ -152,7 +152,7 @@ class Noosfero::Plugin | ||
| 152 | 152 | ||
| 153 | # Here the developer may specify the events to which the plugins can | 153 | # Here the developer may specify the events to which the plugins can |
| 154 | # register and must return true or false. The default value must be false. | 154 | # register and must return true or false. The default value must be false. |
| 155 | - # Must also explicitly defined its returning variables. | 155 | + # Must also explicitly define its returning variables. |
| 156 | 156 | ||
| 157 | # -> If true, noosfero will include plugin_dir/public/style.css into | 157 | # -> If true, noosfero will include plugin_dir/public/style.css into |
| 158 | # application | 158 | # application |
| @@ -172,7 +172,7 @@ class Noosfero::Plugin | @@ -172,7 +172,7 @@ class Noosfero::Plugin | ||
| 172 | # -> Customize profile block design and behavior | 172 | # -> Customize profile block design and behavior |
| 173 | # (overwrites profile_image_link function) | 173 | # (overwrites profile_image_link function) |
| 174 | # returns = lambda block that creates html code. | 174 | # returns = lambda block that creates html code. |
| 175 | - def profile_image_link profile | 175 | + def profile_image_link(profile, size, tag, extra_info) |
| 176 | nil | 176 | nil |
| 177 | end | 177 | end |
| 178 | 178 |
lib/noosfero/plugin/manager.rb
| @@ -31,32 +31,23 @@ class Noosfero::Plugin::Manager | @@ -31,32 +31,23 @@ class Noosfero::Plugin::Manager | ||
| 31 | map { |plugin| plugin.send(event, *args) }.compact | 31 | map { |plugin| plugin.send(event, *args) }.compact |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | - # return first implementation of a specific hotspot | ||
| 35 | - def first_impl(event, *args) | ||
| 36 | - default = Noosfero::Plugin.new.send(event, *args) | ||
| 37 | - impl = default | ||
| 38 | - each do |plugin| | ||
| 39 | - impl = plugin.send(event, *args) | ||
| 40 | - break if impl != default | ||
| 41 | - end | ||
| 42 | - impl | ||
| 43 | - end | ||
| 44 | - | ||
| 45 | alias :dispatch_scopes :dispatch_without_flatten | 34 | alias :dispatch_scopes :dispatch_without_flatten |
| 46 | 35 | ||
| 47 | def dispatch_first(event, *args) | 36 | def dispatch_first(event, *args) |
| 48 | - result = nil | 37 | + default = Noosfero::Plugin.new.send(event, *args) |
| 38 | + result = default | ||
| 49 | each do |plugin| | 39 | each do |plugin| |
| 50 | result = plugin.send(event, *args) | 40 | result = plugin.send(event, *args) |
| 51 | - break if result.present? | 41 | + break if result != default |
| 52 | end | 42 | end |
| 53 | result | 43 | result |
| 54 | end | 44 | end |
| 55 | 45 | ||
| 56 | def fetch_first_plugin(event, *args) | 46 | def fetch_first_plugin(event, *args) |
| 47 | + default = Noosfero::Plugin.new.send(event, *args) | ||
| 57 | result = nil | 48 | result = nil |
| 58 | each do |plugin| | 49 | each do |plugin| |
| 59 | - if plugin.send(event, *args) | 50 | + if plugin.send(event, *args) != default |
| 60 | result = plugin.class | 51 | result = plugin.class |
| 61 | break | 52 | break |
| 62 | end | 53 | end |
test/unit/plugin_manager_test.rb
| @@ -61,6 +61,10 @@ class PluginManagerTest < ActiveSupport::TestCase | @@ -61,6 +61,10 @@ class PluginManagerTest < ActiveSupport::TestCase | ||
| 61 | end | 61 | end |
| 62 | 62 | ||
| 63 | should 'dispatch_first method returns the first plugin response if there is many plugins to responde the event' do | 63 | should 'dispatch_first method returns the first plugin response if there is many plugins to responde the event' do |
| 64 | + class Noosfero::Plugin | ||
| 65 | + def random_event | ||
| 66 | + end | ||
| 67 | + end | ||
| 64 | 68 | ||
| 65 | class Plugin1 < Noosfero::Plugin | 69 | class Plugin1 < Noosfero::Plugin |
| 66 | def random_event | 70 | def random_event |