##// END OF EJS Templates
Adds .find and .all Plugin class methods....
Jean-Philippe Lang -
r2037:fefc6e6bec61
parent child
Show More
@@ -1,92 +1,92
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class AdminController < ApplicationController
19 19 before_filter :require_admin
20 20
21 21 helper :sort
22 22 include SortHelper
23 23
24 24 def index
25 25 @no_configuration_data = Redmine::DefaultData::Loader::no_data?
26 26 end
27 27
28 28 def projects
29 29 sort_init 'name', 'asc'
30 30 sort_update
31 31
32 32 @status = params[:status] ? params[:status].to_i : 1
33 33 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
34 34
35 35 unless params[:name].blank?
36 36 name = "%#{params[:name].strip.downcase}%"
37 37 c << ["LOWER(identifier) LIKE ? OR LOWER(name) LIKE ?", name, name]
38 38 end
39 39
40 40 @project_count = Project.count(:conditions => c.conditions)
41 41 @project_pages = Paginator.new self, @project_count,
42 42 per_page_option,
43 43 params['page']
44 44 @projects = Project.find :all, :order => sort_clause,
45 45 :conditions => c.conditions,
46 46 :limit => @project_pages.items_per_page,
47 47 :offset => @project_pages.current.offset
48 48
49 49 render :action => "projects", :layout => false if request.xhr?
50 50 end
51 51
52 52 def plugins
53 @plugins = Redmine::Plugin.registered_plugins.values.sort
53 @plugins = Redmine::Plugin.all
54 54 end
55 55
56 56 # Loads the default configuration
57 57 # (roles, trackers, statuses, workflow, enumerations)
58 58 def default_configuration
59 59 if request.post?
60 60 begin
61 61 Redmine::DefaultData::Loader::load(params[:lang])
62 62 flash[:notice] = l(:notice_default_data_loaded)
63 63 rescue Exception => e
64 64 flash[:error] = l(:error_can_t_load_default_data, e.message)
65 65 end
66 66 end
67 67 redirect_to :action => 'index'
68 68 end
69 69
70 70 def test_email
71 71 raise_delivery_errors = ActionMailer::Base.raise_delivery_errors
72 72 # Force ActionMailer to raise delivery errors so we can catch it
73 73 ActionMailer::Base.raise_delivery_errors = true
74 74 begin
75 75 @test = Mailer.deliver_test(User.current)
76 76 flash[:notice] = l(:notice_email_sent, User.current.mail)
77 77 rescue Exception => e
78 78 flash[:error] = l(:notice_email_error, e.message)
79 79 end
80 80 ActionMailer::Base.raise_delivery_errors = raise_delivery_errors
81 81 redirect_to :controller => 'settings', :action => 'edit', :tab => 'notifications'
82 82 end
83 83
84 84 def info
85 85 @db_adapter_name = ActiveRecord::Base.connection.adapter_name
86 86 @flags = {
87 87 :default_admin_changed => User.find(:first, :conditions => ["login=? and hashed_password=?", 'admin', User.hash_password('admin')]).nil?,
88 88 :file_repository_writable => File.writable?(Attachment.storage_path),
89 89 :rmagick_available => Object.const_defined?(:Magick)
90 90 }
91 91 end
92 92 end
@@ -1,58 +1,59
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class SettingsController < ApplicationController
19 19 before_filter :require_admin
20 20
21 21 def index
22 22 edit
23 23 render :action => 'edit'
24 24 end
25 25
26 26 def edit
27 27 @notifiables = %w(issue_added issue_updated news_added document_added file_added message_posted)
28 28 if request.post? && params[:settings] && params[:settings].is_a?(Hash)
29 29 settings = (params[:settings] || {}).dup.symbolize_keys
30 30 settings.each do |name, value|
31 31 # remove blank values in array settings
32 32 value.delete_if {|v| v.blank? } if value.is_a?(Array)
33 33 Setting[name] = value
34 34 end
35 35 flash[:notice] = l(:notice_successful_update)
36 36 redirect_to :action => 'edit', :tab => params[:tab]
37 37 return
38 38 end
39 39 @options = {}
40 40 @options[:user_format] = User::USER_FORMATS.keys.collect {|f| [User.current.name(f), f.to_s] }
41 41 @deliveries = ActionMailer::Base.perform_deliveries
42 42
43 43 @guessed_host_and_path = request.host_with_port
44 44 @guessed_host_and_path << ('/'+ request.relative_url_root.gsub(%r{^\/}, '')) unless request.relative_url_root.blank?
45 45 end
46 46
47 47 def plugin
48 plugin_id = params[:id].to_sym
49 @plugin = Redmine::Plugin.registered_plugins[plugin_id]
48 @plugin = Redmine::Plugin.find(params[:id])
50 49 if request.post?
51 Setting["plugin_#{plugin_id}"] = params[:settings]
50 Setting["plugin_#{@plugin.id}"] = params[:settings]
52 51 flash[:notice] = l(:notice_successful_update)
53 redirect_to :action => 'plugin', :id => params[:id]
52 redirect_to :action => 'plugin', :id => @plugin.id
54 53 end
55 54 @partial = @plugin.settings[:partial]
56 @settings = Setting["plugin_#{plugin_id}"]
55 @settings = Setting["plugin_#{@plugin.id}"]
56 rescue Redmine::PluginNotFound
57 render_404
57 58 end
58 59 end
@@ -1,164 +1,164
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Setting < ActiveRecord::Base
19 19
20 20 DATE_FORMATS = [
21 21 '%Y-%m-%d',
22 22 '%d/%m/%Y',
23 23 '%d.%m.%Y',
24 24 '%d-%m-%Y',
25 25 '%m/%d/%Y',
26 26 '%d %b %Y',
27 27 '%d %B %Y',
28 28 '%b %d, %Y',
29 29 '%B %d, %Y'
30 30 ]
31 31
32 32 TIME_FORMATS = [
33 33 '%H:%M',
34 34 '%I:%M %p'
35 35 ]
36 36
37 37 ENCODINGS = %w(US-ASCII
38 38 windows-1250
39 39 windows-1251
40 40 windows-1252
41 41 windows-1253
42 42 windows-1254
43 43 windows-1255
44 44 windows-1256
45 45 windows-1257
46 46 windows-1258
47 47 windows-31j
48 48 ISO-2022-JP
49 49 ISO-2022-KR
50 50 ISO-8859-1
51 51 ISO-8859-2
52 52 ISO-8859-3
53 53 ISO-8859-4
54 54 ISO-8859-5
55 55 ISO-8859-6
56 56 ISO-8859-7
57 57 ISO-8859-8
58 58 ISO-8859-9
59 59 ISO-8859-13
60 60 ISO-8859-15
61 61 KOI8-R
62 62 UTF-8
63 63 UTF-16
64 64 UTF-16BE
65 65 UTF-16LE
66 66 EUC-JP
67 67 Shift_JIS
68 68 GB18030
69 69 GBK
70 70 ISCII91
71 71 EUC-KR
72 72 Big5
73 73 Big5-HKSCS
74 74 TIS-620)
75 75
76 76 cattr_accessor :available_settings
77 77 @@available_settings = YAML::load(File.open("#{RAILS_ROOT}/config/settings.yml"))
78 Redmine::Plugin.registered_plugins.each do |id, plugin|
78 Redmine::Plugin.all.each do |plugin|
79 79 next unless plugin.settings
80 @@available_settings["plugin_#{id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
80 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
81 81 end
82 82
83 83 validates_uniqueness_of :name
84 84 validates_inclusion_of :name, :in => @@available_settings.keys
85 85 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
86 86
87 87 # Hash used to cache setting values
88 88 @cached_settings = {}
89 89 @cached_cleared_on = Time.now
90 90
91 91 def value
92 92 v = read_attribute(:value)
93 93 # Unserialize serialized settings
94 94 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
95 95 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
96 96 v
97 97 end
98 98
99 99 def value=(v)
100 100 v = v.to_yaml if v && @@available_settings[name]['serialized']
101 101 write_attribute(:value, v.to_s)
102 102 end
103 103
104 104 # Returns the value of the setting named name
105 105 def self.[](name)
106 106 v = @cached_settings[name]
107 107 v ? v : (@cached_settings[name] = find_or_default(name).value)
108 108 end
109 109
110 110 def self.[]=(name, v)
111 111 setting = find_or_default(name)
112 112 setting.value = (v ? v : "")
113 113 @cached_settings[name] = nil
114 114 setting.save
115 115 setting.value
116 116 end
117 117
118 118 # Defines getter and setter for each setting
119 119 # Then setting values can be read using: Setting.some_setting_name
120 120 # or set using Setting.some_setting_name = "some value"
121 121 @@available_settings.each do |name, params|
122 122 src = <<-END_SRC
123 123 def self.#{name}
124 124 self[:#{name}]
125 125 end
126 126
127 127 def self.#{name}?
128 128 self[:#{name}].to_i > 0
129 129 end
130 130
131 131 def self.#{name}=(value)
132 132 self[:#{name}] = value
133 133 end
134 134 END_SRC
135 135 class_eval src, __FILE__, __LINE__
136 136 end
137 137
138 138 # Helper that returns an array based on per_page_options setting
139 139 def self.per_page_options_array
140 140 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
141 141 end
142 142
143 143 # Checks if settings have changed since the values were read
144 144 # and clears the cache hash if it's the case
145 145 # Called once per request
146 146 def self.check_cache
147 147 settings_updated_on = Setting.maximum(:updated_on)
148 148 if settings_updated_on && @cached_cleared_on <= settings_updated_on
149 149 @cached_settings.clear
150 150 @cached_cleared_on = Time.now
151 151 logger.info "Settings cache cleared." if logger
152 152 end
153 153 end
154 154
155 155 private
156 156 # Returns the Setting instance for the setting named name
157 157 # (record found in database or new record with default value)
158 158 def self.find_or_default(name)
159 159 name = name.to_s
160 160 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
161 161 setting = find_by_name(name)
162 162 setting ||= new(:name => name, :value => @@available_settings[name]['default']) if @@available_settings.has_key? name
163 163 end
164 164 end
@@ -1,176 +1,188
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine #:nodoc:
19 19
20 class PluginNotFound < StandardError; end
21
20 22 # Base class for Redmine plugins.
21 23 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
22 24 #
23 25 # Redmine::Plugin.register :example do
24 26 # name 'Example plugin'
25 27 # author 'John Smith'
26 28 # description 'This is an example plugin for Redmine'
27 29 # version '0.0.1'
28 30 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
29 31 # end
30 32 #
31 33 # === Plugin attributes
32 34 #
33 35 # +settings+ is an optional attribute that let the plugin be configurable.
34 36 # It must be a hash with the following keys:
35 37 # * <tt>:default</tt>: default value for the plugin settings
36 38 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
37 39 # Example:
38 40 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
39 41 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
40 42 #
41 43 # When rendered, the plugin settings value is available as the local variable +settings+
42 44 class Plugin
43 45 @registered_plugins = {}
44 46 class << self
45 47 attr_reader :registered_plugins
46 48 private :new
47 49
48 50 def def_field(*names)
49 51 class_eval do
50 52 names.each do |name|
51 53 define_method(name) do |*args|
52 54 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
53 55 end
54 56 end
55 57 end
56 58 end
57 59 end
58 60 def_field :name, :description, :author, :version, :settings
59 61 attr_reader :id
60 62
61 63 # Plugin constructor
62 64 def self.register(id, &block)
63 65 p = new(id)
64 66 p.instance_eval(&block)
65 Plugin.registered_plugins[id] = p
67 registered_plugins[id] = p
68 end
69
70 # Returns an array off all registered plugins
71 def self.all
72 registered_plugins.values.sort
73 end
74
75 # Finds a plugin by its id
76 def self.find(id)
77 registered_plugins[id.to_sym] || raise(PluginNotFound)
66 78 end
67 79
68 80 def initialize(id)
69 81 @id = id.to_sym
70 82 end
71 83
72 84 def <=>(plugin)
73 85 self.id.to_s <=> plugin.id.to_s
74 86 end
75 87
76 88 # Adds an item to the given +menu+.
77 89 # The +id+ parameter (equals to the project id) is automatically added to the url.
78 90 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
79 91 #
80 92 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
81 93 #
82 94 def menu(menu, item, url, options={})
83 95 Redmine::MenuManager.map(menu).push(item, url, options)
84 96 end
85 97 alias :add_menu_item :menu
86 98
87 99 # Removes +item+ from the given +menu+.
88 100 def delete_menu_item(menu, item)
89 101 Redmine::MenuManager.map(menu).delete(item)
90 102 end
91 103
92 104 # Defines a permission called +name+ for the given +actions+.
93 105 #
94 106 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
95 107 # permission :destroy_contacts, { :contacts => :destroy }
96 108 # permission :view_contacts, { :contacts => [:index, :show] }
97 109 #
98 110 # The +options+ argument can be used to make the permission public (implicitly given to any user)
99 111 # or to restrict users the permission can be given to.
100 112 #
101 113 # Examples
102 114 # # A permission that is implicitly given to any user
103 115 # # This permission won't appear on the Roles & Permissions setup screen
104 116 # permission :say_hello, { :example => :say_hello }, :public => true
105 117 #
106 118 # # A permission that can be given to any user
107 119 # permission :say_hello, { :example => :say_hello }
108 120 #
109 121 # # A permission that can be given to registered users only
110 122 # permission :say_hello, { :example => :say_hello }, :require => loggedin
111 123 #
112 124 # # A permission that can be given to project members only
113 125 # permission :say_hello, { :example => :say_hello }, :require => member
114 126 def permission(name, actions, options = {})
115 127 if @project_module
116 128 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
117 129 else
118 130 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
119 131 end
120 132 end
121 133
122 134 # Defines a project module, that can be enabled/disabled for each project.
123 135 # Permissions defined inside +block+ will be bind to the module.
124 136 #
125 137 # project_module :things do
126 138 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
127 139 # permission :destroy_contacts, { :contacts => :destroy }
128 140 # end
129 141 def project_module(name, &block)
130 142 @project_module = name
131 143 self.instance_eval(&block)
132 144 @project_module = nil
133 145 end
134 146
135 147 # Registers an activity provider.
136 148 #
137 149 # Options:
138 150 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
139 151 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
140 152 #
141 153 # A model can provide several activity event types.
142 154 #
143 155 # Examples:
144 156 # register :news
145 157 # register :scrums, :class_name => 'Meeting'
146 158 # register :issues, :class_name => ['Issue', 'Journal']
147 159 #
148 160 # Retrieving events:
149 161 # Associated model(s) must implement the find_events class method.
150 162 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
151 163 #
152 164 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
153 165 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
154 166 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
155 167 #
156 168 # Note that :view_scrums permission is required to view these events in the activity view.
157 169 def activity_provider(*args)
158 170 Redmine::Activity.register(*args)
159 171 end
160 172
161 173 # Registers a wiki formatter.
162 174 #
163 175 # Parameters:
164 176 # * +name+ - human-readable name
165 177 # * +formatter+ - formatter class, which should have an instance method +to_html+
166 178 # * +helper+ - helper module, which will be included by wiki pages
167 179 def wiki_format_provider(name, formatter, helper)
168 180 Redmine::WikiFormatting.register(name, formatter, helper)
169 181 end
170 182
171 183 # Returns +true+ if the plugin can be configured.
172 184 def configurable?
173 185 settings && settings.is_a?(Hash) && !settings[:partial].blank?
174 186 end
175 187 end
176 188 end
General Comments 0
You need to be logged in to leave comments. Login now