0001-Add-a-Redmine-plugin-locator-to-be-able-to-query-the.patch

Tim Felgentreff, 2010-09-08 11:54

Download (4.77 KB)

View differences:

config/environment.rb
50 50
  # It will automatically turn deliveries on
51 51
  config.action_mailer.perform_deliveries = false
52 52

  
53
  # Use redmine's custom plugin locater
54
  require File.join(RAILS_ROOT, "lib/redmine_plugin_locator")
55
  config.plugin_locators << RedminePluginLocator
56

  
53 57
  config.gem 'rubytree', :lib => 'tree'
54 58
  
55 59
  # Load any local configuration that is kept out of source control
lib/redmine/plugin.rb
17 17

  
18 18
module Redmine #:nodoc:
19 19

  
20
  class PluginNotFound < StandardError; end
20
  class PluginNotFound < StandardError
21
    attr_reader :plugin_id
22
    def initialize(plug_id=nil)
23
      super
24
      @plugin_id = plug_id
25
    end
26
  end
21 27
  class PluginRequirementError < StandardError; end
22 28
  
23 29
  # Base class for Redmine plugins.
......
44 50
  # When rendered, the plugin settings value is available as the local variable +settings+
45 51
  class Plugin
46 52
    @registered_plugins = {}
53
    @deferred_plugins   = {}
54

  
47 55
    class << self
48
      attr_reader :registered_plugins
56
      attr_reader :registered_plugins, :deferred_plugins
49 57
      private :new
50 58

  
51 59
      def def_field(*names)
......
63 71
    
64 72
    # Plugin constructor
65 73
    def self.register(id, &block)
66
      p = new(id)
67
      p.instance_eval(&block)
68
      # Set a default name if it was not provided during registration
69
      p.name(id.to_s.humanize) if p.name.nil?
70
      # Adds plugin locales if any
71
      # YAML translation files should be found under <plugin>/config/locales/
72
      ::I18n.load_path += Dir.glob(File.join(RAILS_ROOT, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml'))
73
      registered_plugins[id] = p
74
      begin
75
        id = id.to_sym
76
        p = new(id)
77
        p.instance_eval(&block)
78
        # Set a default name if it was not provided during registration
79
        p.name(id.to_s.humanize) if p.name.nil?
80
        # Adds plugin locales if any
81
        # YAML translation files should be found under <plugin>/config/locales/
82
        ::I18n.load_path += Dir.glob(File.join(RAILS_ROOT, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml'))
83
        registered_plugins[id] = p
84

  
85
        # If there are plugins waiting for us to be loaded, we try loading those, again
86
        if deferred_plugins[id]
87
          deferred_plugins[id].each do |ary|
88
            plugin_id, block = ary
89
            register(plugin_id, &block)
90
          end
91
          deferred_plugins.delete(id)
92
        end
93

  
94
        return p
95
      rescue PluginNotFound => e
96
        if RedminePluginLocator.instance.has_plugin? e.plugin_id
97
          # The required plugin is going to be loaded later, defer loading this plugin
98
          (deferred_plugins[e.plugin_id] ||= []) << [id, block]
99
          return p
100
        else
101
          raise e
102
        end
103
      end
74 104
    end
75 105
    
76 106
    # Returns an array off all registered plugins
......
81 111
    # Finds a plugin by its id
82 112
    # Returns a PluginNotFound exception if the plugin doesn't exist
83 113
    def self.find(id)
84
      registered_plugins[id.to_sym] || raise(PluginNotFound)
114
      registered_plugins[id.to_sym] || raise(PluginNotFound.new(id.to_sym))
85 115
    end
86 116
    
87 117
    # Clears the registered plugins hash
lib/redmine_plugin_locator.rb
1
class RedminePluginLocator < Rails::Plugin::FileSystemLocator
2
  def initialize(initializer)
3
    super
4
    @@instance = self
5
  end
6

  
7
  def self.instance
8
    @@instance
9
  end
10

  
11
  # This locator is not meant for loading plugins
12
  # The plugin loading is done by the default rails locator, this one is
13
  # only for querying available plugins easily
14
  def plugins(for_loading = true)
15
    return [] if for_loading
16
    super()
17
  end
18

  
19
  def has_plugin?(name)
20
    plugins(false).collect(&:name).include? name.to_s
21
  end
22
end
0
-