Commit f5c2a127d2e2de8bd51b558ed9a48098365584ad
1 parent
83c8889e
Exists in
master
and in
28 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 | 558 | # displays a link to the profile homepage with its image (as generated by |
| 559 | 559 | # #profile_image) and its name below it. |
| 560 | 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 | 562 | return instance_eval(&content) |
| 563 | 563 | end |
| 564 | 564 | name = profile.short_name | ... | ... |
lib/noosfero/plugin.rb
| ... | ... | @@ -152,7 +152,7 @@ class Noosfero::Plugin |
| 152 | 152 | |
| 153 | 153 | # Here the developer may specify the events to which the plugins can |
| 154 | 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 | 157 | # -> If true, noosfero will include plugin_dir/public/style.css into |
| 158 | 158 | # application |
| ... | ... | @@ -172,7 +172,7 @@ class Noosfero::Plugin |
| 172 | 172 | # -> Customize profile block design and behavior |
| 173 | 173 | # (overwrites profile_image_link function) |
| 174 | 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 | 176 | nil |
| 177 | 177 | end |
| 178 | 178 | ... | ... |
lib/noosfero/plugin/manager.rb
| ... | ... | @@ -31,32 +31,23 @@ class Noosfero::Plugin::Manager |
| 31 | 31 | map { |plugin| plugin.send(event, *args) }.compact |
| 32 | 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 | 34 | alias :dispatch_scopes :dispatch_without_flatten |
| 46 | 35 | |
| 47 | 36 | def dispatch_first(event, *args) |
| 48 | - result = nil | |
| 37 | + default = Noosfero::Plugin.new.send(event, *args) | |
| 38 | + result = default | |
| 49 | 39 | each do |plugin| |
| 50 | 40 | result = plugin.send(event, *args) |
| 51 | - break if result.present? | |
| 41 | + break if result != default | |
| 52 | 42 | end |
| 53 | 43 | result |
| 54 | 44 | end |
| 55 | 45 | |
| 56 | 46 | def fetch_first_plugin(event, *args) |
| 47 | + default = Noosfero::Plugin.new.send(event, *args) | |
| 57 | 48 | result = nil |
| 58 | 49 | each do |plugin| |
| 59 | - if plugin.send(event, *args) | |
| 50 | + if plugin.send(event, *args) != default | |
| 60 | 51 | result = plugin.class |
| 61 | 52 | break |
| 62 | 53 | end | ... | ... |
test/unit/plugin_manager_test.rb
| ... | ... | @@ -61,6 +61,10 @@ class PluginManagerTest < ActiveSupport::TestCase |
| 61 | 61 | end |
| 62 | 62 | |
| 63 | 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 | 69 | class Plugin1 < Noosfero::Plugin |
| 66 | 70 | def random_event | ... | ... |