2011-05-30 00:11:52 +04:00
#-- copyright
# ChiliProject is a project management system.
2011-05-30 22:52:25 +04:00
#
2011-05-30 00:11:52 +04:00
# Copyright (C) 2010-2011 the ChiliProject Team
2011-05-30 22:52:25 +04:00
#
2011-05-30 00:11:52 +04:00
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
2011-05-30 22:52:25 +04:00
#
2011-05-30 00:11:52 +04:00
# See doc/COPYRIGHT.rdoc for more details.
#++
2007-09-23 21:19:27 +04:00
module Redmine #:nodoc:
2008-11-16 18:38:37 +03:00
class PluginNotFound < StandardError ; end
2008-11-16 23:00:20 +03:00
class PluginRequirementError < StandardError ; end
2011-05-30 22:52:25 +04:00
2007-09-23 21:19:27 +04:00
# Base class for Redmine plugins.
# Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# Redmine::Plugin.register :example do
# name 'Example plugin'
# author 'John Smith'
# description 'This is an example plugin for Redmine'
# version '0.0.1'
# settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
# end
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# === Plugin attributes
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# +settings+ is an optional attribute that let the plugin be configurable.
# It must be a hash with the following keys:
# * <tt>:default</tt>: default value for the plugin settings
# * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
# Example:
# settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
# In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# When rendered, the plugin settings value is available as the local variable +settings+
class Plugin
@registered_plugins = { }
class << self
attr_reader :registered_plugins
private :new
def def_field ( * names )
2011-05-30 22:52:25 +04:00
class_eval do
2007-09-23 21:19:27 +04:00
names . each do | name |
2011-05-30 22:52:25 +04:00
define_method ( name ) do | * args |
2007-09-23 21:19:27 +04:00
args . empty? ? instance_variable_get ( " @ #{ name } " ) : instance_variable_set ( " @ #{ name } " , * args )
end
end
end
end
end
2008-11-16 20:12:02 +03:00
def_field :name , :description , :url , :author , :author_url , :version , :settings
2008-11-16 18:22:48 +03:00
attr_reader :id
2011-05-30 22:52:25 +04:00
2007-09-23 21:19:27 +04:00
# Plugin constructor
2008-11-16 18:22:48 +03:00
def self . register ( id , & block )
p = new ( id )
2007-09-23 21:19:27 +04:00
p . instance_eval ( & block )
2008-11-16 19:08:25 +03:00
# Set a default name if it was not provided during registration
p . name ( id . to_s . humanize ) if p . name . nil?
2009-02-21 14:04:50 +03:00
# Adds plugin locales if any
# YAML translation files should be found under <plugin>/config/locales/
:: I18n . load_path += Dir . glob ( File . join ( RAILS_ROOT , 'vendor' , 'plugins' , id . to_s , 'config' , 'locales' , '*.yml' ) )
2008-11-16 18:38:37 +03:00
registered_plugins [ id ] = p
end
2011-05-30 22:52:25 +04:00
2008-11-16 18:38:37 +03:00
# Returns an array off all registered plugins
def self . all
registered_plugins . values . sort
end
2011-05-30 22:52:25 +04:00
2008-11-16 18:38:37 +03:00
# Finds a plugin by its id
2008-11-16 19:08:25 +03:00
# Returns a PluginNotFound exception if the plugin doesn't exist
2008-11-16 18:38:37 +03:00
def self . find ( id )
registered_plugins [ id . to_sym ] || raise ( PluginNotFound )
2008-11-16 18:22:48 +03:00
end
2011-05-30 22:52:25 +04:00
2008-11-16 19:08:25 +03:00
# Clears the registered plugins hash
# It doesn't unload installed plugins
def self . clear
@registered_plugins = { }
end
2010-10-26 03:32:01 +04:00
# Checks if a plugin is installed
#
# @param [String] id name of the plugin
def self . installed? ( id )
registered_plugins [ id . to_sym ] . present?
end
2011-05-30 22:52:25 +04:00
2008-11-16 18:22:48 +03:00
def initialize ( id )
@id = id . to_sym
end
2011-05-30 22:52:25 +04:00
2008-11-16 18:22:48 +03:00
def <=> ( plugin )
self . id . to_s < = > plugin . id . to_s
2007-09-23 21:19:27 +04:00
end
2011-05-30 22:52:25 +04:00
2011-03-13 18:53:09 +03:00
# Sets a requirement on the ChiliProject version.
# Raises a PluginRequirementError exception if the requirement is not met.
#
# It uses the same syntax as rubygems requirements.
# Examples
# # Requires exactly ChiliProject 1.1.1
# requires_chiliproject "1.1.1"
# requires_chiliproject "= 1.1.1"
# # Requires ChiliProject 1.1.x
# requires_chiliproject "~> 1.1.0"
# # Requires ChiliProject between 1.1.0 and 1.1.5 or higher
# requires_chiliproject ">= 1.1.0", "<= 1.1.5"
def requires_chiliproject ( * args )
required_version = Gem :: Requirement . new ( * args )
2011-08-21 17:16:34 +04:00
chili_version = Gem :: Version . new ( ChiliProject :: VERSION . to_semver )
2011-03-13 18:53:09 +03:00
unless required_version . satisfied_by? chili_version
raise PluginRequirementError . new ( " #{ id } plugin requires ChiliProject version #{ required_version } but current version is #{ chili_version } . " )
end
true
end
# Sets a requirement on Redmine version.
2008-11-16 23:00:20 +03:00
# Raises a PluginRequirementError exception if the requirement is not met
#
2011-03-13 18:53:09 +03:00
# THIS IS A REDMINE COMPATIBILITY INTERFACE
#
2008-11-16 23:00:20 +03:00
# Examples
# # Requires Redmine 0.7.3 or higher
# requires_redmine :version_or_higher => '0.7.3'
# requires_redmine '0.7.3'
#
# # Requires a specific Redmine version
# requires_redmine :version => '0.7.3' # 0.7.3 only
# requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 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 )
2011-05-30 22:52:25 +04:00
2008-11-16 23:00:20 +03:00
current = Redmine :: VERSION . to_a
arg . each do | k , v |
v = [ ] << v unless v . is_a? ( Array )
versions = v . collect { | s | s . split ( '.' ) . collect ( & :to_i ) }
case k
when :version_or_higher
raise ArgumentError . new ( " wrong number of versions ( #{ versions . size } for 1) " ) unless versions . size == 1
unless ( current < = > versions . first ) > = 0
raise PluginRequirementError . new ( " #{ id } plugin requires Redmine #{ v } or higher but current is #{ current . join ( '.' ) } " )
end
when :version
unless versions . include? ( current . slice ( 0 , 3 ) )
raise PluginRequirementError . new ( " #{ id } plugin requires one the following Redmine versions: #{ v . join ( ', ' ) } but current is #{ current . join ( '.' ) } " )
end
end
end
true
end
2007-09-23 21:19:27 +04:00
2009-12-16 05:07:46 +03:00
# Sets a requirement on a Redmine plugin version
# Raises a PluginRequirementError exception if the requirement is not met
#
# Examples
# # 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'
#
# # 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
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 )
plugin = Plugin . find ( plugin_name )
current = plugin . version . split ( '.' ) . collect ( & :to_i )
arg . each do | k , v |
v = [ ] << v unless v . is_a? ( Array )
versions = v . collect { | s | s . split ( '.' ) . collect ( & :to_i ) }
case k
when :version_or_higher
raise ArgumentError . new ( " wrong number of versions ( #{ versions . size } for 1) " ) unless versions . size == 1
unless ( current < = > versions . first ) > = 0
raise PluginRequirementError . new ( " #{ id } plugin requires the #{ plugin_name } plugin #{ v } or higher but current is #{ current . join ( '.' ) } " )
end
when :version
unless versions . include? ( current . slice ( 0 , 3 ) )
raise PluginRequirementError . new ( " #{ id } plugin requires one the following versions of #{ plugin_name } : #{ v . join ( ', ' ) } but current is #{ current . join ( '.' ) } " )
end
end
end
true
end
2007-09-23 21:19:27 +04:00
# Adds an item to the given +menu+.
# The +id+ parameter (equals to the project id) is automatically added to the url.
2008-02-22 21:19:00 +03:00
# menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
2011-05-30 22:52:25 +04:00
#
2008-02-22 21:19:00 +03:00
# +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
2011-05-30 22:52:25 +04:00
#
2008-10-25 13:55:31 +04:00
def menu ( menu , item , url , options = { } )
Redmine :: MenuManager . map ( menu ) . push ( item , url , options )
end
alias :add_menu_item :menu
2011-05-30 22:52:25 +04:00
2008-10-25 13:55:31 +04:00
# Removes +item+ from the given +menu+.
def delete_menu_item ( menu , item )
Redmine :: MenuManager . map ( menu ) . delete ( item )
2007-09-23 21:19:27 +04:00
end
# Defines a permission called +name+ for the given +actions+.
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
# permission :destroy_contacts, { :contacts => :destroy }
# permission :view_contacts, { :contacts => [:index, :show] }
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# The +options+ argument can be used to make the permission public (implicitly given to any user)
# or to restrict users the permission can be given to.
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# Examples
# # 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
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# # A permission that can be given to any user
# permission :say_hello, { :example => :say_hello }
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# # A permission that can be given to registered users only
2010-07-16 07:11:35 +04:00
# permission :say_hello, { :example => :say_hello }, :require => :loggedin
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# # A permission that can be given to project members only
2010-07-16 07:11:35 +04:00
# permission :say_hello, { :example => :say_hello }, :require => :member
2007-09-23 21:19:27 +04:00
def permission ( name , actions , options = { } )
if @project_module
Redmine :: AccessControl . map { | map | map . project_module ( @project_module ) { | map | map . permission ( name , actions , options ) } }
else
Redmine :: AccessControl . map { | map | map . permission ( name , actions , options ) }
end
end
2011-05-30 22:52:25 +04:00
2007-09-23 21:19:27 +04:00
# Defines a project module, that can be enabled/disabled for each project.
# Permissions defined inside +block+ will be bind to the module.
2011-05-30 22:52:25 +04:00
#
2007-09-23 21:19:27 +04:00
# project_module :things do
# permission :view_contacts, { :contacts => [:list, :show] }, :public => true
# permission :destroy_contacts, { :contacts => :destroy }
# end
def project_module ( name , & block )
@project_module = name
self . instance_eval ( & block )
@project_module = nil
end
2011-05-30 22:52:25 +04:00
2008-07-27 22:38:31 +04:00
# Registers an activity provider.
#
# Options:
# * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
# * <tt>:default</tt> - setting this option to false will make the events not displayed by default
2011-05-30 22:52:25 +04:00
#
2008-07-27 22:38:31 +04:00
# A model can provide several activity event types.
2011-05-30 22:52:25 +04:00
#
2008-07-27 22:38:31 +04:00
# Examples:
# register :news
# register :scrums, :class_name => 'Meeting'
# register :issues, :class_name => ['Issue', 'Journal']
2011-05-30 22:52:25 +04:00
#
2008-07-27 22:38:31 +04:00
# Retrieving events:
# Associated model(s) must implement the find_events class method.
# ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
2011-05-30 22:52:25 +04:00
#
# The following call should return all the scrum events visible by current user that occured in the 5 last days:
2008-07-27 22:38:31 +04:00
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
2011-05-30 22:52:25 +04:00
#
2008-07-27 22:38:31 +04:00
# Note that :view_scrums permission is required to view these events in the activity view.
def activity_provider ( * args )
Redmine :: Activity . register ( * args )
end
2011-05-30 22:52:25 +04:00
2008-10-27 14:08:29 +03:00
# Registers a wiki formatter.
#
# Parameters:
# * +name+ - human-readable name
# * +formatter+ - formatter class, which should have an instance method +to_html+
# * +helper+ - helper module, which will be included by wiki pages
def wiki_format_provider ( name , formatter , helper )
Redmine :: WikiFormatting . register ( name , formatter , helper )
end
2007-09-23 21:19:27 +04:00
# Returns +true+ if the plugin can be configured.
def configurable?
settings && settings . is_a? ( Hash ) && ! settings [ :partial ] . blank?
end
end
end