plugin.rb
15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
require 'noosfero'
class Noosfero::Plugin
attr_accessor :context
def initialize(context=nil)
self.context = context
end
class << self
attr_writer :should_load
def should_load
@should_load.nil? && true || @boot
end
def initialize!
return if !should_load
enabled.each do |plugin_dir|
plugin_name = File.basename(plugin_dir)
load_plugin(plugin_name)
load_plugin_extensions(plugin_dir)
end
end
def setup(config)
return if !should_load
enabled.each do |dir|
setup_plugin(dir, config)
end
end
def setup_plugin(dir, config)
plugin_name = File.basename(dir)
plugin_dependencies_ok = true
plugin_dependencies_file = File.join(dir, 'dependencies.rb')
if File.exists?(plugin_dependencies_file)
begin
require plugin_dependencies_file
rescue LoadError => ex
plugin_dependencies_ok = false
$stderr.puts "W: Noosfero plugin #{plugin_name} failed to load (#{ex})"
end
end
if plugin_dependencies_ok
%w[
controllers
controllers/public
controllers/profile
controllers/myprofile
controllers/admin
].each do |folder|
config.autoload_paths << File.join(dir, folder)
end
[ config.autoload_paths, $:].each do |path|
path << File.join(dir, 'models')
path << File.join(dir, 'lib')
# load vendor/plugins
Dir.glob(File.join(dir, '/vendor/plugins/*')).each do |vendor_plugin|
path << "#{vendor_plugin}/lib"
init = "#{vendor_plugin}/init.rb"
require init.gsub(/.rb$/, '') if File.file? init
end
end
# add view path
ActionController::Base.view_paths.unshift(File.join(dir, 'views'))
end
end
def load_plugin(plugin_name)
(plugin_name.to_s.camelize + 'Plugin').constantize
end
def load_plugin_extensions(dir)
Dir[File.join(dir, 'lib', 'ext', '*.rb')].each {|file| require_dependency file }
end
def enabled
@enabled ||=
begin
plugins = Dir.glob(Rails.root.join('config', 'plugins', '*'))
if Rails.env.test? && !plugins.include?(Rails.root.join('config', 'plugins', 'foo'))
plugins << Rails.root.join('plugins', 'foo')
end
plugins.select do |entry|
File.directory?(entry)
end
end
end
def all
@all ||= []
end
def inherited(subclass)
all << subclass.to_s unless all.include?(subclass.to_s)
end
def public_name
self.name.underscore.gsub('_plugin','')
end
def public_path(file = '')
compute_public_path((public_name + '/' + file), 'plugins')
end
def root_path
Rails.root.join('plugins', public_name)
end
def view_path
File.join(root_path,'views')
end
# Here the developer should specify the meta-informations that the plugin can
# inform.
def plugin_name
self.name.underscore.humanize
end
def plugin_description
_("No description informed.")
end
def admin_url
{:controller => "#{name.underscore}_admin", :action => 'index'}
end
def has_admin_url?
File.exists?(File.join(root_path, 'controllers', "#{name.underscore}_admin_controller.rb"))
end
end
def expanded_template(file_path, locals = {})
views_path = Rails.root.join('plugins', "#{self.class.public_name}", 'views')
ERB.new(File.read("#{views_path}/#{file_path}")).result(binding)
end
def extra_blocks(params = {})
return [] if self.class.extra_blocks.nil?
blocks = self.class.extra_blocks.map do |block, options|
type = options[:type]
type = type.is_a?(Array) ? type : [type].compact
type = type.map do |x|
x.is_a?(String) ? x.capitalize.constantize : x
end
raise "This is not a valid type" if !type.empty? && ![Person, Community, Enterprise, Environment].detect{|m| type.include?(m)}
position = options[:position]
position = position.is_a?(Array) ? position : [position].compact
position = position.map{|p| p.to_i}
raise "This is not a valid position" if !position.empty? && ![1,2,3].detect{|m| position.include?(m)}
if !type.empty? && (params[:type] != :all)
block = type.include?(params[:type]) ? block : nil
end
if !position.empty? && !params[:position].nil?
block = position.detect{ |p| [params[:position]].flatten.include?(p)} ? block : nil
end
block
end
blocks.compact!
blocks || []
end
def macros
self.class.constants.map do |constant_name|
self.class.const_get(constant_name)
end.select {|const| const.is_a?(Class) && const < Noosfero::Plugin::Macro}
end
# Here the developer may specify the events to which the plugins can
# register and must return true or false. The default value must be false.
# Must also explicitly define its returning variables.
# -> If true, noosfero will include plugin_dir/public/style.css into
# application
def stylesheet?
false
end
# -> Adds buttons to the control panel
# returns = { :title => title, :icon => icon, :url => url }
# title = name that will be displayed.
# icon = css class name (for customized icons include them in a css file).
# url = url or route to which the button will redirect.
def control_panel_buttons
nil
end
# -> Customize profile block design and behavior
# (overwrites profile_image_link function)
# returns = lambda block that creates html code.
def profile_image_link(profile, size, tag, extra_info)
nil
end
# -> Adds tabs to the profile
# returns = { :title => title, :id => id, :content => content, :start => start }
# title = name that will be displayed.
# id = div id.
# content = lambda block that creates html code.
# start = boolean that specifies if the tab must come before noosfero tabs (optional).
def profile_tabs
nil
end
# -> Adds plugin-specific content types to CMS
# returns = [ContentClass1, ContentClass2, ...]
def content_types
nil
end
# -> Adds content to calalog item
# returns = lambda block that creates html code
def catalog_item_extras(item)
nil
end
# -> Adds content to profile editor info and settings
# returns = lambda block that creates html code or raw rhtml/html.erb
def profile_editor_extras
nil
end
# -> Adds content to calalog list item
# returns = lambda block that creates html code
def catalog_list_item_extras(item)
nil
end
# -> Adds content to products info
# returns = lambda block that creates html code
def product_info_extras(product)
nil
end
# -> Adds content to products on asset list
# returns = lambda block that creates html code
def asset_product_extras(product)
nil
end
# -> Adds a property to the product on asset products
# returns = {:name => name, :content => content}
# name = Name of the property
# content = lambda block that creates an html
def asset_product_properties(product)
nil
end
# -> Adds content to the beginning of the page
# returns = lambda block that creates html code or raw rhtml/html.erb
def body_beginning
nil
end
# -> Adds content to the ending of the page head
# returns = lambda block that creates html code or raw rhtml/html.erb
def head_ending
nil
end
# -> Adds plugins' javascript files to application
# returns = ['example1.js', 'javascripts/example2.js', 'example3.js']
def js_files
[]
end
# -> Adds stuff in user data hash
# returns = { :some_data => some_value, :another_data => another_value }
def user_data_extras
{}
end
# -> Parse and possibly make changes of content (article, block, etc) during HTML rendering
# returns = content as string after parser and changes
def parse_content(html, source)
[html, source]
end
# -> Adds links to the admin panel
# returns = {:title => title, :url => url}
# title = name that will be displayed in the link
# url = url or route to which the link will redirect to.
def admin_panel_links
nil
end
# -> Adds buttons to manage members page
# returns = { :title => title, :icon => icon, :url => url }
# title = name that will be displayed.
# icon = css class name (for customized icons include them in a css file).
# url = url or route to which the button will redirect.
def manage_members_extra_buttons
nil
end
# This method will be called just before a comment is saved to the database.
#
# It can modify the comment in several ways. In special, a plugin can call
# reject! on the comment and that will cause the comment to not be saved.
#
# example:
#
# def filter_comment(comment)
# if user_not_logged_in
# comment.reject!
# end
# end
#
def filter_comment(comment)
end
# Define custom logic to filter loaded comments.
#
# Example:
#
# def unavailable_comments(scope)
# scope.without_spams
# end
#
def unavailable_comments(scope)
scope
end
# -> Allows plugins to check weather object is a spam
def check_for_spam(object)
end
# -> Allows plugins to know when an object is marked as a spam
def marked_as_spam(object)
end
# -> Allows plugins to know when an object is marked as a ham
def marked_as_ham(object)
end
# Adds extra actions for comments
# returns = list of hashes or lambda block that creates a list of hashes
# example:
#
# def comment_actions(comment)
# [{:link => link_to_function(...)}]
# end
#
def comment_actions(comment)
nil
end
# This method is called when the user click on comment actions menu.
# returns = list or lambda block that return ids of enabled menu items for comments
# example:
#
# def check_comment_actions(comment)
# ['#action1', '#action2']
# end
#
def check_comment_actions(comment)
[]
end
# -> Adds fields to the signup form
# returns = lambda block that creates html code
def signup_extra_contents
nil
end
# -> Adds adicional content to profile info
# returns = lambda block that creates html code
def profile_info_extra_contents
nil
end
# -> Removes the invite friend button from the friends controller
# returns = boolean
def remove_invite_friends_button
nil
end
# -> Extends organization list of members
# returns = An instance of ActiveRecord::NamedScope::Scope retrieved through
# Person.members_of method.
def organization_members(organization)
nil
end
# -> Extends person permission access
# returns = boolean
def has_permission?(person, permission, target)
nil
end
# -> Adds hidden_fields to the new community view
# returns = {key => value}
def new_community_hidden_fields
nil
end
# -> Adds hidden_fields to the enterprise registration view
# returns = {key => value}
def enterprise_registration_hidden_fields
nil
end
# -> Add an alternative authentication method.
# Your plugin have to make the access control and return the logged user.
# returns = User
def alternative_authentication
nil
end
# -> Adds adicional link to make the user authentication
# returns = lambda block that creates html code
def alternative_authentication_link
nil
end
# -> Allow or not user registration
# returns = boolean
def allow_user_registration
true
end
# -> Allow or not password recovery by users
# returns = boolean
def allow_password_recovery
true
end
# -> Adds fields to the login form
# returns = lambda block that creates html code
def login_extra_contents
nil
end
# -> Adds adicional content to comment form
# returns = lambda block that creates html code
def comment_form_extra_contents(args)
nil
end
# -> Finds objects by their contents
# returns = {:results => [a, b, c, ...], ...}
# P.S.: The plugin might add other informations on the return hash for its
# own use in specific views
def find_by_contents(asset, scope, query, paginate_options={}, options={})
end
# -> Adds aditional fields for change_password
# returns = [{:field => 'field1', :name => 'field 1 name', :model => 'person'}, {...}]
def change_password_fields
nil
end
# -> Adds additional blocks to profiles and environments.
# Your plugin must implements a class method called 'extra_blocks'
# that returns a hash with the following syntax.
# {
# 'block_name' =>
# {
# :type => 'for which holder the block will be available',
# :position => 'where the block could be displayed'
# }
# }
#
# Where:
#
# - block_name: Name of the new block added to the blocks list
# - type: Might have some of the values
# - 'environment' or Environment: If the block is available only for Environment models
# - 'community' or Community: If the block is available only for Community models
# - 'enterprise' or Enterprise: If the block is available only for Enterprise models
# - 'person' or Person: If the block is available only for Person models
# - nil: If no type parameter is passed the block will be available for all types
# - position: Is the layout position of the block. It should be:
# - '1' or 1: Area 1 of layout
# - '2' or 2: Area 2 of layout
# - '3' or 3: Area 3 of layout
# - nil: If no position parameter is passed the block will be available for all positions
#
# OBS: Area 1 is where stay the main content of layout. Areas 2 and 3 are the sides of layout.
#
# examples:
#
# def self.extra_blocks(params)
# {
# #Display 'CustomBlock1' only for 'Person' on position '1'
# CustomBlock1 => {:type => 'person', :position => '1' },
#
# #Display 'CustomBlock2' only for 'Community' on position '2'
# CustomBlock2 => {:type => Community, :position => '2' },
#
# #Display 'CustomBlock3' only for 'Enterprise' on position '3'
# CustomBlock3 => {:type => 'enterprise', :position => 3 },
#
# #Display 'CustomBlock2' for 'Environment' and 'Person' on positions '1' and '3'
# CustomBlock4 => {:type => ['environment', Person], :position => ['1','3'] },
#
# #Display 'CustomBlock5' for all types and all positions
# CustomBlock5 => {},
# }
# end
#
# OBS: The default value is a empty hash.
def self.extra_blocks
{}
end
def method_missing(method, *args, &block)
# This is a generic hotspot for all controllers on Noosfero.
# If any plugin wants to define filters to run on any controller, the name of
# the hotspot must be in the following form: <underscored_controller_name>_filters.
# Example: for ProfileController the hotspot is profile_controller_filters
#
# -> Adds a filter to a controller
# returns = { :type => type,
# :method_name => method_name,
# :options => {:opt1 => opt1, :opt2 => opt2},
# :block => Proc or lambda block}
# type = 'before_filter' or 'after_filter'
# method_name = The name of the filter
# option = Filter options, like :only or :except
# block = Block that the filter will call
if method.to_s =~ /^(.+)_controller_filters$/
[]
# -> Removes the action button from the content
# returns = boolean
elsif method.to_s =~ /^content_remove_(#{content_actions.join('|')})$/
nil
# -> Expire the action button from the content
# returns = string with reason of expiration
elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/
nil
else
super
end
end
private
def content_actions
#FIXME 'new' and 'upload' only works for content_remove. It should work for
#content_expire too.
%w[edit delete spread locale suggest home new upload]
end
end
require 'noosfero/plugin/hot_spot'
require 'noosfero/plugin/manager'
require 'noosfero/plugin/active_record'
require 'noosfero/plugin/mailer_base'
require 'noosfero/plugin/settings'