ref: master
lib/noosfero/plugin.rb
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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
require_relative 'plugin/macro' require_relative 'plugin/hot_spot' require_relative 'plugin/manager' require_relative 'plugin/parent_methods' require_relative 'plugin/settings' class Noosfero::Plugin attr_accessor :context def initialize(context=nil) self.context = context end def environment context.send :environment if self.context end def profile context.send :profile if self.context end class << self include Noosfero::Plugin::ParentMethods attr_writer :should_load def should_load @should_load.nil? && true || @boot end def initialize! return if !should_load klasses = available_plugins.map do |plugin_dir| plugin_name = File.basename(plugin_dir) load_plugin plugin_name end available_plugins.each do |plugin_dir| load_plugin_extensions plugin_dir end # filters must be loaded after all extensions klasses.each do |plugin| load_plugin_filters plugin load_plugin_hotspots plugin end end def setup(config) return if !should_load available_plugins.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 [config.autoload_paths, config.eager_load_paths].each do |path| path.concat Dir["#{dir}/app/*/"] path.concat Dir["#{dir}/controllers/{,public,profile,myprofile,admin,concerns}"] path.concat Dir["#{dir}/models{,/concerns}"] path.concat Dir["#{dir}/lib"] path.concat Dir["#{dir}/serializers"] path.concat Dir["#{dir}/helpers"] end # add view path config.paths['app/views'].unshift File.join(dir, 'views') end end def load_plugin_identifier identifier klass = identifier.to_s.camelize.constantize klass = klass.const_get :Base if klass.class == Module klass end def load_plugin public_name load_plugin_identifier "#{public_name.to_s.camelize}Plugin" end # This is a generic method that initialize any possible filter defined by a # plugin to a specific controller def load_plugin_filters(plugin) ActionDispatch::Reloader.to_prepare do filters = plugin.new.send 'application_controller_filters' rescue [] Noosfero::Plugin.add_controller_filters ApplicationController, plugin, filters plugin_methods = plugin.instance_methods.select {|m| m.to_s.end_with?('_filters')} plugin_methods.each do |plugin_method| controller_class = plugin_method.to_s.gsub('_filters', '').camelize.constantize filters = plugin.new.send(plugin_method) Noosfero::Plugin.add_controller_filters controller_class, plugin, filters end end end # This is a generic method to extend the hotspots list with plugins # hotspots. This allows plugins to extend other plugins. # To use this, the plugin must define its hotspots inside a module Hotspots. # Its also needed to include Noosfero::Plugin::HotSpot module # in order to dispatch plugins methods. # # Checkout FooPlugin for usage example. def load_plugin_hotspots(plugin) ActionDispatch::Reloader.to_prepare do begin module_name = "#{plugin.name}::Hotspots" Noosfero::Plugin.send(:include, module_name.constantize) rescue NameError end end end def add_controller_filters(controller_class, plugin, filters) unless filters.is_a?(Array) filters = [filters] end filters.each do |plugin_filter| plugin_filter[:options] ||= {} plugin_filter[:options][:if] = -> { environment.plugin_enabled? plugin.module_name } filter_method = "#{plugin.identifier}_#{plugin_filter[:method_name]}".to_sym controller_class.send plugin_filter[:type], filter_method, plugin_filter[:options] controller_class.send :define_method, filter_method, &plugin_filter[:block] end end def load_plugin_extensions(dir) ActionDispatch::Reloader.to_prepare do ActiveSupport.run_load_hooks "#{File.basename dir}_plugin_extensions".to_sym Dir[File.join(dir, 'lib', 'ext', '**', '*.rb')].each{ |file| require_dependency file } end end def available_plugins unless @available_plugins path = File.join(Rails.root, '{baseplugins,config/plugins}', '*') @available_plugins = Dir.glob(path).select{ |i| File.directory?(i) } if (Rails.env.test? || Rails.env.cucumber?) && !@available_plugins.include?(File.join(Rails.root, 'config', 'plugins', 'foo')) @available_plugins << File.join(Rails.root, 'plugins', 'foo') end end @available_plugins end def available_plugin_names available_plugins.map { |f| File.basename(f).camelize } end def all @all ||= available_plugins.map{ |dir| (File.basename(dir) + "_plugin").camelize } end end def has_block?(block) self.class.extra_blocks.keys.include?(block) 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? && (type.any?{|t| !(t < Profile) && t != Environment }) 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. # html_options = aditional html options. 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 # -> Customize the way comments are counted for Profiles and Environment # considering more than just articles comments # Used on statistic block # Ex: a plugin may want that Communities receive comments themselves # as evaluations # returns = the number of comments to be sum on the statistics def more_comments_count owner 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 tabs to the products # returns = { :title => title, :id => id, :content => content } # title = name that will be displayed. # id = div id. # content = lambda block that creates html code. def product_tabs product nil end # -> Adds content to calalog item # returns = lambda block that creates html code def catalog_item_extras(item) nil end # -> Filters the types of organizations that are shown on manage organizations # returns a scope filtered by the specified type def filter_manage_organization_scope type nil end # -> Add new options for manage organization filters # returns an array of new options # i.e [[_('Type'), 'type'], [_('Type2'), 'type2']] def organization_types_filter_options 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 catalog 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 # returns = lambda block that creates html code or raw rhtml/html.erb def body_ending 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 aditional action buttons to article # returns = { :title => title, :icon => icon, :url => url, :html_options => {} } # 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. # html_options = Html options for customization # # Multiple values could be passed as parameter. # returns = [{:title => title, :icon => icon}, {:title => title, :icon => icon}] def article_extra_toolbar_buttons(article) [] end # -> Adds adicional content to article # returns = lambda block that creates html code def article_extra_contents(article) nil end # -> Adds adicional fields to a view # returns = proc block that creates html code def upload_files_extra_fields(article) nil end # -> Adds fields to the signup form # returns = proc 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 memberships list # returns = An instance of ActiveRecord::NamedScope::Scope retrived through # Person.memberships_of method. def person_memberships(person) 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 = proc 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 # -> Adds adicional content to article header # returns = lambda block that creates html code def article_header_extra_contents(article) nil end # -> Adds adittional content to comment visualization # returns = lambda block that creates html code def comment_extra_contents(args) nil end # This method is called when the user clicks to send a comment. # A plugin can add new content to comment form and this method can process the params sent to avoid creating field on core tables. # returns = params after processed by plugins # example: # # def process_extra_comment_params(params) # params.delete(:extra_field) # end # def process_extra_comment_params(params) params 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={}) scope = scope.like_search(query, options) unless query.blank? scope = scope.send(options[:filter]) unless options[:filter].blank? || options[:filter] == 'more_relevant' {:results => scope.paginate(paginate_options)} end def autocomplete asset, scope, query, paginate_options={:page => 1}, options={:field => 'name'} nil end def catalog_autocomplete_item_extras product nil end def search_order asset nil end def catalog_search_extras_begin nil end def catalog_search_extras_end nil end # -> Suggests terms based on asset and query # returns = [a, b, c, ...] def find_suggestions(query, context, asset, options={:limit => 5}) context.search_terms. where(:asset => asset). where("search_terms.term like ?", "%#{query}%"). where('search_terms.score > 0'). order('search_terms.score DESC'). limit(options[:limit]).map(&:term) end # -> Adds aditional fields for change_password # returns = [{:field => 'field1', :name => 'field 1 name', :model => 'person'}, {...}] def change_password_fields nil end # -> Perform extra transactions related to profile in profile editor # returns = true in success or raise an exception if it could not update the data def profile_editor_transaction_extras nil end # -> Return a list of hashs with the needed information to create optional fields # returns = a list of hashs as {:name => "string", :label => "string", :object_name => :key, :method => :key} def extra_optional_fields [] end # -> Adds css class to <html> tag # returns = ['class1', 'class2'] def html_tag_classes nil end # -> Displays the facets view side box # returns = true or false def search_facets? false end # -> Adds reserved identifiers to profile # returns = ['identifier1', 'identifier2'] def reserved_identifiers [] end # -> Adds new items to the user menu # returns = [item1, item2] def user_menu_items(user) [] 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 api_custom_login request nil 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 # -> Generic hotspots for models callbacks # Example: article_after_create_callback elsif method.to_s =~ /^(.+)_#{Noosfero::Plugin::HotSpot::CALLBACK_HOTSPOTS.join('|')}_callback$/ nil elsif context.respond_to?(method, true) context.send(method) 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 undo clone] end end |