diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index 203c50881..642755761 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -16,8 +16,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module Redmine - + # Exception on specified plugin was not found. + # + # @since 0.8.0 class PluginNotFound < StandardError; end + + # Exception on required incompatible plugin with redmine. + # + # @since 0.8.0 class PluginRequirementError < StandardError; end # Base class for Redmine plugins. @@ -37,15 +43,22 @@ module Redmine # It must be a hash with the following keys: # * :default: default value for the plugin settings # * :partial: path of the configuration partial view, relative to the plugin app/views directory - # Example: + # @example # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings' # In this example, the settings partial will be found here in the plugin directory: app/views/settings/_settings.rhtml. # # When rendered, the plugin settings value is available as the local variable +settings+ + # + # @since 0.6.0 + # @see http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial class Plugin + # @attr [String] self.directory redmine plugin directory. + # @since 2.0.0 cattr_accessor :directory self.directory = File.join(Rails.root, 'plugins') + # @attr [String] self.public_directory Redmine pulugin's public directory. + # @since 1.4.0 cattr_accessor :public_directory self.public_directory = File.join(Rails.root, 'public', 'plugin_assets') @@ -53,9 +66,11 @@ module Redmine @used_partials = {} class << self + # @since 0.6.0 attr_reader :registered_plugins private :new + # @since 0.6.0 def def_field(*names) class_eval do names.each do |name| @@ -67,9 +82,23 @@ module Redmine end end def_field :name, :description, :url, :author, :author_url, :version, :settings, :directory + # @since 0.8.0 attr_reader :id # Plugin constructor + # @param [String, Symbol] id plugin id. + # @yield Evaluates block with no block parameter. In block, self is new Redmine::Plugin object with specified id. Redmine::Plugin instance methods can call without receiver to new plugin object. + # @example + # Redmine::Plugin.register :example do + # name 'Example plugin' + # author 'John Smith' + # description 'This is an example plugin for Redmine' + # version '0.0.1' + # requires_redmine version_or_higher: '3.0.0' + # end + # + # @since 0.6.0 + # @see Redmine::Plugin#requires_redmine def self.register(id, &block) p = new(id) p.instance_eval(&block) @@ -114,35 +143,49 @@ module Redmine end # Returns an array of all registered plugins + # + # @return [[Redmine::Plugin]] an array of plugins + # @since 0.8.0 def self.all registered_plugins.values.sort end # Finds a plugin by its id # Returns a PluginNotFound exception if the plugin doesn't exist + # + # @param [String, Symbol] id name of the plugin + # @return [Redmine::Plugin] Redmine plugin + # @since 0.8.0 def self.find(id) registered_plugins[id.to_sym] || raise(PluginNotFound) end # Clears the registered plugins hash # It doesn't unload installed plugins + # + # @since 0.8.0 def self.clear @registered_plugins = {} end # Removes a plugin from the registered plugins # It doesn't unload the plugin + # + # @param [String, Symbol] id name of the plugin + # @since 2.6.0 def self.unregister(id) @registered_plugins.delete(id) end # Checks if a plugin is installed # - # @param [String] id name of the plugin + # @param [String, Symbol] id name of the plugin + # @since 1.0.3 def self.installed?(id) registered_plugins[id.to_sym].present? end + # @since 2.0.0 def self.load Dir.glob(File.join(self.directory, '*')).sort.each do |directory| if File.directory?(directory) @@ -159,22 +202,29 @@ module Redmine end end + # @since 0.8.0 def initialize(id) @id = id.to_sym end + # @since 2.0.0 def public_directory File.join(self.class.public_directory, id.to_s) end + # @since 2.3.0 def to_param id end + # plugin assets directory + # + # @since 2.0.0 def assets_directory File.join(directory, 'assets') end + # @since 0.8.0 def <=>(plugin) self.id.to_s <=> plugin.id.to_s end @@ -182,7 +232,7 @@ module Redmine # Sets a requirement on Redmine version # Raises a PluginRequirementError exception if the requirement is not met # - # Examples + # @example # # Requires Redmine 0.7.3 or higher # requires_redmine :version_or_higher => '0.7.3' # requires_redmine '0.7.3' @@ -198,6 +248,8 @@ module Redmine # # Requires a Redmine version within a range # requires_redmine :version => '0.7.3'..'0.9.1' # >= 0.7.3 and <= 0.9.1 # requires_redmine :version => '0.7'..'0.9' # >= 0.7.x and <= 0.9.x + # + # @since 0.8.0 def requires_redmine(arg) arg = { :version_or_higher => arg } unless arg.is_a?(Hash) arg.assert_valid_keys(:version, :version_or_higher) @@ -228,6 +280,7 @@ module Redmine true end + # @since 2.2.0 def compare_versions(requirement, current) requirement = requirement.split('.').collect(&:to_i) requirement <=> current.slice(0, requirement.size) @@ -237,7 +290,7 @@ module Redmine # Sets a requirement on a Redmine plugin version # Raises a PluginRequirementError exception if the requirement is not met # - # Examples + # @example # # Requires a plugin named :foo version 0.7.3 or higher # requires_redmine_plugin :foo, :version_or_higher => '0.7.3' # requires_redmine_plugin :foo, '0.7.3' @@ -245,6 +298,8 @@ module Redmine # # Requires a specific version of a Redmine plugin # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 + # + # @since 0.9.0 def requires_redmine_plugin(plugin_name, arg) arg = { :version_or_higher => arg } unless arg.is_a?(Hash) arg.assert_valid_keys(:version, :version_or_higher) @@ -276,12 +331,16 @@ module Redmine # # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu # + # @since 0.6.0 def menu(menu, item, url, options={}) Redmine::MenuManager.map(menu).push(item, url, options) end + # @since 0.8.0 alias :add_menu_item :menu # Removes +item+ from the given +menu+. + # + # @since 0.8.0 def delete_menu_item(menu, item) Redmine::MenuManager.map(menu).delete(item) end @@ -297,7 +356,7 @@ module Redmine # * :require => can be set to one of the following values to restrict users the permission can be given to: :loggedin, :member # * :read => set it to true so that the permission is still granted on closed projects # - # Examples + # @example # # A permission that is implicitly given to any user # # This permission won't appear on the Roles & Permissions setup screen # permission :say_hello, { :example => :say_hello }, :public => true, :read => true @@ -310,6 +369,8 @@ module Redmine # # # A permission that can be given to project members only # permission :say_hello, { :example => :say_hello }, :require => :member + # + # @since 0.6.0 def permission(name, actions, options = {}) if @project_module Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}} @@ -325,6 +386,8 @@ module Redmine # permission :view_contacts, { :contacts => [:list, :show] }, :public => true # permission :destroy_contacts, { :contacts => :destroy } # end + # + # @since 0.6.0 def project_module(name, &block) @project_module = name self.instance_eval(&block) @@ -339,7 +402,7 @@ module Redmine # # A model can provide several activity event types. # - # Examples: + # @example # register :news # register :scrums, :class_name => 'Meeting' # register :issues, :class_name => ['Issue', 'Journal'] @@ -353,6 +416,8 @@ module Redmine # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only # # Note that :view_scrums permission is required to view these events in the activity view. + # + # @since 0.8.0 def activity_provider(*args) Redmine::Activity.register(*args) end @@ -367,18 +432,22 @@ module Redmine # * +options+ - a Hash of options (optional) # * :label - label for the formatter displayed in application settings # - # Examples: + # @example # wiki_format_provider(:custom_formatter, CustomFormatter, :label => "My custom formatter") # + # @since 0.8.0 def wiki_format_provider(name, *args) Redmine::WikiFormatting.register(name, *args) end # Returns +true+ if the plugin can be configured. + # + # @since 0.6.0 def configurable? settings && settings.is_a?(Hash) && !settings[:partial].blank? end + # @since 2.0.0 def mirror_assets source = assets_directory destination = public_directory @@ -421,6 +490,8 @@ module Redmine end # Mirrors assets from one or all plugins to public/plugin_assets + # + # @since 2.0.0 def self.mirror_assets(name=nil) if name.present? find(name).mirror_assets @@ -432,23 +503,31 @@ module Redmine end # The directory containing this plugin's migrations (plugin/db/migrate) + # + # @since 2.0.0 def migration_directory File.join(directory, 'db', 'migrate') end # Returns the version number of the latest migration for this plugin. Returns # nil if this plugin has no migrations. + # + # @since 2.0.0 def latest_migration migrations.last end # Returns the version numbers of all migrations for this plugin. + # + # @since 2.0.0 def migrations migrations = Dir[migration_directory+"/*.rb"] migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort end # Migrate this plugin to the given version + # + # @since 2.0.0 def migrate(version = nil) puts "Migrating #{id} (#{name})..." Redmine::Plugin::Migrator.migrate_plugin(self, version) @@ -460,6 +539,7 @@ module Redmine # Plugin.migrate('sample_plugin') # Plugin.migrate('sample_plugin', 1) # + # @since 2.0.0 def self.migrate(name=nil, version=nil) if name.present? find(name).migrate(version) @@ -470,18 +550,24 @@ module Redmine end end + # @since 2.0.0 class Migrator < ActiveRecord::Migrator # We need to be able to set the 'current' plugin being migrated. + # + # @since 2.0.0 cattr_accessor :current_plugin class << self # Runs the migrations from a plugin, up (or down) to the version given + # + # @since 2.0.0 def migrate_plugin(plugin, version) self.current_plugin = plugin return if current_version(plugin) == version migrate(plugin.migration_directory, version) end + # @since 2.0.0 def current_version(plugin=current_plugin) # Delete migrations that don't match .. to_i will work because the number comes first sm_table = ::ActiveRecord::SchemaMigration.table_name @@ -491,6 +577,7 @@ module Redmine end end + # @since 2.0.0 def migrated sm_table = ::ActiveRecord::SchemaMigration.table_name ::ActiveRecord::Base.connection.select_values( @@ -498,6 +585,7 @@ module Redmine ).delete_if{ |v| v.match(/-#{current_plugin.id}$/) == nil }.map(&:to_i).sort end + # @since 2.0.0 def record_version_state_after_migrating(version) super(version.to_s + "-" + current_plugin.id.to_s) end