Commit 144d578353865120973b3d47f8ad357032b98cdb
1 parent
3176444c
Exists in
master
and in
29 other branches
Extra methods for the plugin manager
* first: returns the first non-blank result * first_plugin: returns the first plugin that answers true * dispatch_plugins: returns all plugins that answers true
Showing
2 changed files
with
106 additions
and
0 deletions
Show diff stats
lib/noosfero/plugin/manager.rb
@@ -23,12 +23,36 @@ class Noosfero::Plugin::Manager | @@ -23,12 +23,36 @@ class Noosfero::Plugin::Manager | ||
23 | dispatch_without_flatten(event, *args).flatten | 23 | dispatch_without_flatten(event, *args).flatten |
24 | end | 24 | end |
25 | 25 | ||
26 | + def dispatch_plugins(event, *args) | ||
27 | + map { |plugin| plugin.class if plugin.send(event, *args) }.compact.flatten | ||
28 | + end | ||
29 | + | ||
26 | def dispatch_without_flatten(event, *args) | 30 | def dispatch_without_flatten(event, *args) |
27 | map { |plugin| plugin.send(event, *args) }.compact | 31 | map { |plugin| plugin.send(event, *args) }.compact |
28 | end | 32 | end |
29 | 33 | ||
30 | alias :dispatch_scopes :dispatch_without_flatten | 34 | alias :dispatch_scopes :dispatch_without_flatten |
31 | 35 | ||
36 | + def first(event, *args) | ||
37 | + result = nil | ||
38 | + each do |plugin| | ||
39 | + result = plugin.send(event, *args) | ||
40 | + break if result.present? | ||
41 | + end | ||
42 | + result | ||
43 | + end | ||
44 | + | ||
45 | + def first_plugin(event, *args) | ||
46 | + result = nil | ||
47 | + each do |plugin| | ||
48 | + if plugin.send(event, *args) | ||
49 | + result = plugin.class | ||
50 | + break | ||
51 | + end | ||
52 | + end | ||
53 | + result | ||
54 | + end | ||
55 | + | ||
32 | def enabled_plugins | 56 | def enabled_plugins |
33 | @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| | 57 | @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| |
34 | p = plugin.constantize.new | 58 | p = plugin.constantize.new |
test/unit/plugin_manager_test.rb
@@ -58,5 +58,87 @@ class PluginManagerTest < ActiveSupport::TestCase | @@ -58,5 +58,87 @@ class PluginManagerTest < ActiveSupport::TestCase | ||
58 | assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event) | 58 | assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event) |
59 | end | 59 | end |
60 | 60 | ||
61 | + should 'return the first non-blank result' do | ||
62 | + class Plugin1 < Noosfero::Plugin | ||
63 | + def random_event | ||
64 | + end | ||
65 | + end | ||
66 | + | ||
67 | + class Plugin2 < Noosfero::Plugin | ||
68 | + def random_event | ||
69 | + 'Plugin2' | ||
70 | + end | ||
71 | + end | ||
72 | + | ||
73 | + class Plugin3 < Noosfero::Plugin | ||
74 | + def random_event | ||
75 | + 'Plugin3' | ||
76 | + end | ||
77 | + end | ||
78 | + | ||
79 | + environment.enable_plugin(Plugin1.name) | ||
80 | + environment.enable_plugin(Plugin2.name) | ||
81 | + environment.enable_plugin(Plugin3.name) | ||
82 | + | ||
83 | + Plugin3.any_instance.expects(:random_event).never | ||
84 | + | ||
85 | + assert 'Plugin2', manager.first(:random_event) | ||
86 | + end | ||
87 | + | ||
88 | + should 'returns plugins that returns true to the event' do | ||
89 | + class Plugin1 < Noosfero::Plugin | ||
90 | + def random_event | ||
91 | + end | ||
92 | + end | ||
93 | + | ||
94 | + class Plugin2 < Noosfero::Plugin | ||
95 | + def random_event | ||
96 | + true | ||
97 | + end | ||
98 | + end | ||
99 | + | ||
100 | + class Plugin3 < Noosfero::Plugin | ||
101 | + def random_event | ||
102 | + true | ||
103 | + end | ||
104 | + end | ||
105 | + | ||
106 | + environment.enable_plugin(Plugin1.name) | ||
107 | + environment.enable_plugin(Plugin2.name) | ||
108 | + environment.enable_plugin(Plugin3.name) | ||
109 | + | ||
110 | + results = manager.dispatch_plugins(:random_event) | ||
111 | + | ||
112 | + assert_includes results, Plugin2 | ||
113 | + assert_includes results, Plugin3 | ||
114 | + end | ||
115 | + | ||
116 | + should 'return the first plugin that returns true' do | ||
117 | + class Plugin1 < Noosfero::Plugin | ||
118 | + def random_event | ||
119 | + end | ||
120 | + end | ||
121 | + | ||
122 | + class Plugin2 < Noosfero::Plugin | ||
123 | + def random_event | ||
124 | + true | ||
125 | + end | ||
126 | + end | ||
127 | + | ||
128 | + class Plugin3 < Noosfero::Plugin | ||
129 | + def random_event | ||
130 | + true | ||
131 | + end | ||
132 | + end | ||
133 | + | ||
134 | + environment.enable_plugin(Plugin1.name) | ||
135 | + environment.enable_plugin(Plugin2.name) | ||
136 | + environment.enable_plugin(Plugin3.name) | ||
137 | + | ||
138 | + Plugin3.any_instance.expects(:random_event).never | ||
139 | + | ||
140 | + assert_equal Plugin2, manager.first_plugin(:random_event) | ||
141 | + end | ||
142 | + | ||
61 | end | 143 | end |
62 | 144 |