comparison lib/redmine/plugin.rb @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents 433d4f72a19b
children e248c7af89ec
comparison
equal deleted inserted replaced
1296:038ba2d95de8 1464:261b3d9a4903
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
62 end 62 end
63 end 63 end
64 end 64 end
65 end 65 end
66 end 66 end
67 def_field :name, :description, :url, :author, :author_url, :version, :settings 67 def_field :name, :description, :url, :author, :author_url, :version, :settings, :directory
68 attr_reader :id 68 attr_reader :id
69 69
70 # Plugin constructor 70 # Plugin constructor
71 def self.register(id, &block) 71 def self.register(id, &block)
72 p = new(id) 72 p = new(id)
73 p.instance_eval(&block) 73 p.instance_eval(&block)
74
74 # Set a default name if it was not provided during registration 75 # Set a default name if it was not provided during registration
75 p.name(id.to_s.humanize) if p.name.nil? 76 p.name(id.to_s.humanize) if p.name.nil?
77 # Set a default directory if it was not provided during registration
78 p.directory(File.join(self.directory, id.to_s)) if p.directory.nil?
76 79
77 # Adds plugin locales if any 80 # Adds plugin locales if any
78 # YAML translation files should be found under <plugin>/config/locales/ 81 # YAML translation files should be found under <plugin>/config/locales/
79 ::I18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml')) 82 ::I18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml'))
80 83
135 138
136 def initialize(id) 139 def initialize(id)
137 @id = id.to_sym 140 @id = id.to_sym
138 end 141 end
139 142
140 def directory
141 File.join(self.class.directory, id.to_s)
142 end
143
144 def public_directory 143 def public_directory
145 File.join(self.class.public_directory, id.to_s) 144 File.join(self.class.public_directory, id.to_s)
145 end
146
147 def to_param
148 id
146 end 149 end
147 150
148 def assets_directory 151 def assets_directory
149 File.join(directory, 'assets') 152 File.join(directory, 'assets')
150 end 153 end
438 end 441 end
439 442
440 class Migrator < ActiveRecord::Migrator 443 class Migrator < ActiveRecord::Migrator
441 # We need to be able to set the 'current' plugin being migrated. 444 # We need to be able to set the 'current' plugin being migrated.
442 cattr_accessor :current_plugin 445 cattr_accessor :current_plugin
443 446
444 class << self 447 class << self
445 # Runs the migrations from a plugin, up (or down) to the version given 448 # Runs the migrations from a plugin, up (or down) to the version given
446 def migrate_plugin(plugin, version) 449 def migrate_plugin(plugin, version)
447 self.current_plugin = plugin 450 self.current_plugin = plugin
448 return if current_version(plugin) == version 451 return if current_version(plugin) == version
449 migrate(plugin.migration_directory, version) 452 migrate(plugin.migration_directory, version)
450 end 453 end
451 454
452 def current_version(plugin=current_plugin) 455 def current_version(plugin=current_plugin)
453 # Delete migrations that don't match .. to_i will work because the number comes first 456 # Delete migrations that don't match .. to_i will work because the number comes first
454 ::ActiveRecord::Base.connection.select_values( 457 ::ActiveRecord::Base.connection.select_values(
455 "SELECT version FROM #{schema_migrations_table_name}" 458 "SELECT version FROM #{schema_migrations_table_name}"
456 ).delete_if{ |v| v.match(/-#{plugin.id}/) == nil }.map(&:to_i).max || 0 459 ).delete_if{ |v| v.match(/-#{plugin.id}/) == nil }.map(&:to_i).max || 0
457 end 460 end
458 end 461 end
459 462
460 def migrated 463 def migrated
461 sm_table = self.class.schema_migrations_table_name 464 sm_table = self.class.schema_migrations_table_name
462 ::ActiveRecord::Base.connection.select_values( 465 ::ActiveRecord::Base.connection.select_values(
463 "SELECT version FROM #{sm_table}" 466 "SELECT version FROM #{sm_table}"
464 ).delete_if{ |v| v.match(/-#{current_plugin.id}/) == nil }.map(&:to_i).sort 467 ).delete_if{ |v| v.match(/-#{current_plugin.id}/) == nil }.map(&:to_i).sort
465 end 468 end
466 469
467 def record_version_state_after_migrating(version) 470 def record_version_state_after_migrating(version)
468 super(version.to_s + "-" + current_plugin.id.to_s) 471 super(version.to_s + "-" + current_plugin.id.to_s)
469 end 472 end
470 end 473 end
471 end 474 end