summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2008-09-05 10:31:06 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2008-09-05 10:31:06 +0000
commitf7acdd1afde3018a9f7c0a7dc3b6bd97a65670a2 (patch)
treeba4070526883885914942b3a0d253eb2a6e63bf8 /lib
parent4b9df2eac774aa045aa5207c8bc613ed6ca67d42 (diff)
downloadredmine-f7acdd1afde3018a9f7c0a7dc3b6bd97a65670a2.tar.gz
redmine-f7acdd1afde3018a9f7c0a7dc3b6bd97a65670a2.zip
Merged hooks branch @ r1785 into trunk.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1786 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/generators/redmine_plugin/USAGE18
-rw-r--r--lib/generators/redmine_plugin/redmine_plugin_generator.rb31
-rw-r--r--lib/generators/redmine_plugin/templates/README3
-rw-r--r--lib/generators/redmine_plugin/templates/en.yml2
-rw-r--r--lib/generators/redmine_plugin/templates/init.rb8
-rw-r--r--lib/generators/redmine_plugin/templates/test_helper.rb5
-rw-r--r--lib/generators/redmine_plugin_controller/USAGE5
-rw-r--r--lib/generators/redmine_plugin_controller/redmine_plugin_controller_generator.rb18
-rw-r--r--lib/generators/redmine_plugin_controller/templates/controller.rb7
-rw-r--r--lib/generators/redmine_plugin_controller/templates/functional_test.rb8
-rw-r--r--lib/generators/redmine_plugin_controller/templates/helper.rb2
-rw-r--r--lib/generators/redmine_plugin_controller/templates/view.html.erb1
-rw-r--r--lib/generators/redmine_plugin_model/USAGE5
-rw-r--r--lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb18
-rw-r--r--lib/generators/redmine_plugin_model/templates/fixtures.yml11
-rw-r--r--lib/generators/redmine_plugin_model/templates/migration.rb13
-rw-r--r--lib/generators/redmine_plugin_model/templates/model.rb2
-rw-r--r--lib/generators/redmine_plugin_model/templates/unit_test.rb10
-rw-r--r--lib/redmine.rb1
-rw-r--r--lib/redmine/hook.rb109
-rw-r--r--lib/tasks/plugins.rake38
21 files changed, 315 insertions, 0 deletions
diff --git a/lib/generators/redmine_plugin/USAGE b/lib/generators/redmine_plugin/USAGE
new file mode 100644
index 000000000..65a45d163
--- /dev/null
+++ b/lib/generators/redmine_plugin/USAGE
@@ -0,0 +1,18 @@
+Description:
+ The plugin generator creates stubs for a new Redmine plugin.
+
+Example:
+ ./script/generate redmine_plugin meetings
+ create vendor/plugins/redmine_meetings/app/controllers
+ create vendor/plugins/redmine_meetings/app/helpers
+ create vendor/plugins/redmine_meetings/app/models
+ create vendor/plugins/redmine_meetings/app/views
+ create vendor/plugins/redmine_meetings/db/migrate
+ create vendor/plugins/redmine_meetings/lib/tasks
+ create vendor/plugins/redmine_meetings/assets/images
+ create vendor/plugins/redmine_meetings/assets/javascripts
+ create vendor/plugins/redmine_meetings/assets/stylesheets
+ create vendor/plugins/redmine_meetings/lang
+ create vendor/plugins/redmine_meetings/README
+ create vendor/plugins/redmine_meetings/init.rb
+ create vendor/plugins/redmine_meetings/lang/en.yml
diff --git a/lib/generators/redmine_plugin/redmine_plugin_generator.rb b/lib/generators/redmine_plugin/redmine_plugin_generator.rb
new file mode 100644
index 000000000..666386abd
--- /dev/null
+++ b/lib/generators/redmine_plugin/redmine_plugin_generator.rb
@@ -0,0 +1,31 @@
+class RedminePluginGenerator < Rails::Generator::NamedBase
+ attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+ @plugin_name = "redmine_#{file_name.underscore}"
+ @plugin_pretty_name = plugin_name.titleize
+ @plugin_path = "vendor/plugins/#{plugin_name}"
+ end
+
+ def manifest
+ record do |m|
+ m.directory "#{plugin_path}/app/controllers"
+ m.directory "#{plugin_path}/app/helpers"
+ m.directory "#{plugin_path}/app/models"
+ m.directory "#{plugin_path}/app/views"
+ m.directory "#{plugin_path}/db/migrate"
+ m.directory "#{plugin_path}/lib/tasks"
+ m.directory "#{plugin_path}/assets/images"
+ m.directory "#{plugin_path}/assets/javascripts"
+ m.directory "#{plugin_path}/assets/stylesheets"
+ m.directory "#{plugin_path}/lang"
+ m.directory "#{plugin_path}/test"
+
+ m.template 'README', "#{plugin_path}/README"
+ m.template 'init.rb', "#{plugin_path}/init.rb"
+ m.template 'en.yml', "#{plugin_path}/lang/en.yml"
+ m.template 'test_helper.rb', "#{plugin_path}/test/test_helper.rb"
+ end
+ end
+end
diff --git a/lib/generators/redmine_plugin/templates/README b/lib/generators/redmine_plugin/templates/README
new file mode 100644
index 000000000..7a31a890e
--- /dev/null
+++ b/lib/generators/redmine_plugin/templates/README
@@ -0,0 +1,3 @@
+= <%= file_name %>
+
+Description goes here
diff --git a/lib/generators/redmine_plugin/templates/en.yml b/lib/generators/redmine_plugin/templates/en.yml
new file mode 100644
index 000000000..e338591ef
--- /dev/null
+++ b/lib/generators/redmine_plugin/templates/en.yml
@@ -0,0 +1,2 @@
+# English strings go here
+my_label: "My label"
diff --git a/lib/generators/redmine_plugin/templates/init.rb b/lib/generators/redmine_plugin/templates/init.rb
new file mode 100644
index 000000000..1ffcd7bb8
--- /dev/null
+++ b/lib/generators/redmine_plugin/templates/init.rb
@@ -0,0 +1,8 @@
+require 'redmine'
+
+Redmine::Plugin.register :<%= plugin_name %> do
+ name '<%= plugin_pretty_name %> plugin'
+ author 'Author name'
+ description 'This is a plugin for Redmine'
+ version '0.0.1'
+end
diff --git a/lib/generators/redmine_plugin/templates/test_helper.rb b/lib/generators/redmine_plugin/templates/test_helper.rb
new file mode 100644
index 000000000..bd1ed0c5d
--- /dev/null
+++ b/lib/generators/redmine_plugin/templates/test_helper.rb
@@ -0,0 +1,5 @@
+# Load the normal Rails helper
+require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper')
+
+# Ensure that we are using the temporary fixture path
+Engines::Testing.set_fixture_path
diff --git a/lib/generators/redmine_plugin_controller/USAGE b/lib/generators/redmine_plugin_controller/USAGE
new file mode 100644
index 000000000..8336e7117
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/USAGE
@@ -0,0 +1,5 @@
+Description:
+ Generates a plugin controller.
+
+Example:
+ ./script/generate redmine_plugin_controller MyPlugin Pools index show vote
diff --git a/lib/generators/redmine_plugin_controller/redmine_plugin_controller_generator.rb b/lib/generators/redmine_plugin_controller/redmine_plugin_controller_generator.rb
new file mode 100644
index 000000000..533d65ce8
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/redmine_plugin_controller_generator.rb
@@ -0,0 +1,18 @@
+require 'rails_generator/base'
+require 'rails_generator/generators/components/controller/controller_generator'
+
+class RedminePluginControllerGenerator < ControllerGenerator
+ attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
+
+ def initialize(runtime_args, runtime_options = {})
+ runtime_args = runtime_args.dup
+ @plugin_name = "redmine_" + runtime_args.shift.underscore
+ @plugin_pretty_name = plugin_name.titleize
+ @plugin_path = "vendor/plugins/#{plugin_name}"
+ super(runtime_args, runtime_options)
+ end
+
+ def destination_root
+ File.join(RAILS_ROOT, plugin_path)
+ end
+end
diff --git a/lib/generators/redmine_plugin_controller/templates/controller.rb b/lib/generators/redmine_plugin_controller/templates/controller.rb
new file mode 100644
index 000000000..615986d9e
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/templates/controller.rb
@@ -0,0 +1,7 @@
+class <%= class_name %>Controller < ApplicationController
+<% actions.each do |action| -%>
+
+ def <%= action %>
+ end
+<% end -%>
+end
diff --git a/lib/generators/redmine_plugin_controller/templates/functional_test.rb b/lib/generators/redmine_plugin_controller/templates/functional_test.rb
new file mode 100644
index 000000000..876bd79e2
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/templates/functional_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class <%= class_name %>ControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end
diff --git a/lib/generators/redmine_plugin_controller/templates/helper.rb b/lib/generators/redmine_plugin_controller/templates/helper.rb
new file mode 100644
index 000000000..3fe2ecdc7
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/templates/helper.rb
@@ -0,0 +1,2 @@
+module <%= class_name %>Helper
+end
diff --git a/lib/generators/redmine_plugin_controller/templates/view.html.erb b/lib/generators/redmine_plugin_controller/templates/view.html.erb
new file mode 100644
index 000000000..c24afaa6e
--- /dev/null
+++ b/lib/generators/redmine_plugin_controller/templates/view.html.erb
@@ -0,0 +1 @@
+<h2><%= class_name %>#<%= action %></h2>
diff --git a/lib/generators/redmine_plugin_model/USAGE b/lib/generators/redmine_plugin_model/USAGE
new file mode 100644
index 000000000..0cb55a186
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/USAGE
@@ -0,0 +1,5 @@
+Description:
+ Generates a plugin model.
+
+Examples:
+ ./script/generate redmine_plugin_model MyPlugin pool title:string question:text
diff --git a/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb b/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb
new file mode 100644
index 000000000..b712d9b07
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb
@@ -0,0 +1,18 @@
+require 'rails_generator/base'
+require 'rails_generator/generators/components/model/model_generator'
+
+class RedminePluginModelGenerator < ModelGenerator
+ attr_accessor :plugin_path, :plugin_name, :plugin_pretty_name
+
+ def initialize(runtime_args, runtime_options = {})
+ runtime_args = runtime_args.dup
+ @plugin_name = "redmine_" + runtime_args.shift.underscore
+ @plugin_pretty_name = plugin_name.titleize
+ @plugin_path = "vendor/plugins/#{plugin_name}"
+ super(runtime_args, runtime_options)
+ end
+
+ def destination_root
+ File.join(RAILS_ROOT, plugin_path)
+ end
+end
diff --git a/lib/generators/redmine_plugin_model/templates/fixtures.yml b/lib/generators/redmine_plugin_model/templates/fixtures.yml
new file mode 100644
index 000000000..6be3c81be
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/templates/fixtures.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+<% for attribute in attributes -%>
+ <%= attribute.name %>: <%= attribute.default %>
+<% end -%>
+two:
+ id: 2
+<% for attribute in attributes -%>
+ <%= attribute.name %>: <%= attribute.default %>
+<% end -%>
diff --git a/lib/generators/redmine_plugin_model/templates/migration.rb b/lib/generators/redmine_plugin_model/templates/migration.rb
new file mode 100644
index 000000000..2a305a6a9
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/templates/migration.rb
@@ -0,0 +1,13 @@
+class <%= migration_name %> < ActiveRecord::Migration
+ def self.up
+ create_table :<%= table_name %> do |t|
+<% for attribute in attributes -%>
+ t.column :<%= attribute.name %>, :<%= attribute.type %>
+<% end -%>
+ end
+ end
+
+ def self.down
+ drop_table :<%= table_name %>
+ end
+end
diff --git a/lib/generators/redmine_plugin_model/templates/model.rb b/lib/generators/redmine_plugin_model/templates/model.rb
new file mode 100644
index 000000000..8d4c89e91
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/templates/model.rb
@@ -0,0 +1,2 @@
+class <%= class_name %> < ActiveRecord::Base
+end
diff --git a/lib/generators/redmine_plugin_model/templates/unit_test.rb b/lib/generators/redmine_plugin_model/templates/unit_test.rb
new file mode 100644
index 000000000..cc8b2e591
--- /dev/null
+++ b/lib/generators/redmine_plugin_model/templates/unit_test.rb
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class <%= class_name %>Test < Test::Unit::TestCase
+ fixtures :<%= table_name %>
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 33d33752b..3ba2f2cc3 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -4,6 +4,7 @@ require 'redmine/activity'
require 'redmine/mime_type'
require 'redmine/core_ext'
require 'redmine/themes'
+require 'redmine/hook'
require 'redmine/plugin'
begin
diff --git a/lib/redmine/hook.rb b/lib/redmine/hook.rb
new file mode 100644
index 000000000..9bf92b3e7
--- /dev/null
+++ b/lib/redmine/hook.rb
@@ -0,0 +1,109 @@
+# Redmine - project management software
+# Copyright (C) 2006-2008 Jean-Philippe Lang
+#
+# 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.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ module Hook
+ @@listener_classes = []
+ @@listeners = nil
+ @@hook_listeners = {}
+
+ class << self
+ # Adds a listener class.
+ # Automatically called when a class inherits from Redmine::Hook::Listener.
+ def add_listener(klass)
+ raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
+ @@listener_classes << klass
+ clear_listeners_instances
+ end
+
+ # Returns all the listerners instances.
+ def listeners
+ @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
+ end
+
+ # Returns the listeners instances for the given hook.
+ def hook_listeners(hook)
+ @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
+ end
+
+ # Clears all the listeners.
+ def clear_listeners
+ @@listener_classes = []
+ clear_listeners_instances
+ end
+
+ # Clears all the listeners instances.
+ def clear_listeners_instances
+ @@listeners = nil
+ @@hook_listeners = {}
+ end
+
+ # Calls a hook.
+ # Returns the listeners response.
+ def call_hook(hook, context={})
+ response = ''
+ hook_listeners(hook).each do |listener|
+ response << listener.send(hook, context).to_s
+ end
+ response
+ end
+ end
+
+ # Base class for hook listeners.
+ class Listener
+ include Singleton
+
+ # Registers the listener
+ def self.inherited(child)
+ Redmine::Hook.add_listener(child)
+ super
+ end
+ end
+
+ # Listener class used for views hooks.
+ # Listeners that inherit this class will include various helpers by default.
+ class ViewListener < Listener
+ include ERB::Util
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::FormHelper
+ include ActionView::Helpers::FormTagHelper
+ include ActionView::Helpers::FormOptionsHelper
+ include ActionView::Helpers::JavaScriptHelper
+ include ActionView::Helpers::PrototypeHelper
+ include ActionView::Helpers::NumberHelper
+ include ActionView::Helpers::UrlHelper
+ include ActionView::Helpers::AssetTagHelper
+ include ActionView::Helpers::TextHelper
+ include ActionController::UrlWriter
+ include ApplicationHelper
+ end
+
+ # Helper module included in ApplicationHelper so that hooks can be called
+ # in views like this:
+ # <%= call_hook(:some_hook) %>
+ # <%= call_hook(:another_hook, :foo => 'bar' %>
+ #
+ # Current project is automatically added to the call context.
+ module Helper
+ def call_hook(hook, context={})
+ Redmine::Hook.call_hook(hook, {:project => @project}.merge(context))
+ end
+ end
+ end
+end
+
+ApplicationHelper.send(:include, Redmine::Hook::Helper)
diff --git a/lib/tasks/plugins.rake b/lib/tasks/plugins.rake
new file mode 100644
index 000000000..136877164
--- /dev/null
+++ b/lib/tasks/plugins.rake
@@ -0,0 +1,38 @@
+require 'source_annotation_extractor'
+
+# Modified version of the SourceAnnotationExtractor in railties
+# Will search for runable code that uses <tt>call_hook</tt>
+class PluginSourceAnnotationExtractor < SourceAnnotationExtractor
+ # Returns a hash that maps filenames under +dir+ (recursively) to arrays
+ # with their annotations. Only files with annotations are included, and only
+ # those with extension +.builder+, +.rb+, +.rxml+, +.rjs+, +.rhtml+, and +.erb+
+ # are taken into account.
+ def find_in(dir)
+ results = {}
+
+ Dir.glob("#{dir}/*") do |item|
+ next if File.basename(item)[0] == ?.
+
+ if File.directory?(item)
+ results.update(find_in(item))
+ elsif item =~ /(hook|test)\.rb/
+ # skip
+ elsif item =~ /\.(builder|(r(?:b|xml|js)))$/
+ results.update(extract_annotations_from(item, /\s*(#{tag})\(?\s*(.*)$/))
+ elsif item =~ /\.(rhtml|erb)$/
+ results.update(extract_annotations_from(item, /<%=\s*\s*(#{tag})\(?\s*(.*?)\s*%>/))
+ end
+ end
+
+ results
+ end
+end
+
+namespace :redmine do
+ namespace :plugins do
+ desc "Enumerate all Redmine plugin hooks and their context parameters"
+ task :hook_list do
+ PluginSourceAnnotationExtractor.enumerate 'call_hook'
+ end
+ end
+end