Revision 1298:4f746d8966dd .svn/pristine/07
| .svn/pristine/07/071c1af1d5f365dd709aaf606fafd0332e258d64.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2013 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 14 |
# You should have received a copy of the GNU General Public License |
|
| 15 |
# along with this program; if not, write to the Free Software |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
module Redmine #:nodoc: |
|
| 19 |
|
|
| 20 |
class PluginNotFound < StandardError; end |
|
| 21 |
class PluginRequirementError < StandardError; end |
|
| 22 |
|
|
| 23 |
# Base class for Redmine plugins. |
|
| 24 |
# Plugins are registered using the <tt>register</tt> class method that acts as the public constructor. |
|
| 25 |
# |
|
| 26 |
# Redmine::Plugin.register :example do |
|
| 27 |
# name 'Example plugin' |
|
| 28 |
# author 'John Smith' |
|
| 29 |
# description 'This is an example plugin for Redmine' |
|
| 30 |
# version '0.0.1' |
|
| 31 |
# settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
|
|
| 32 |
# end |
|
| 33 |
# |
|
| 34 |
# === Plugin attributes |
|
| 35 |
# |
|
| 36 |
# +settings+ is an optional attribute that let the plugin be configurable. |
|
| 37 |
# It must be a hash with the following keys: |
|
| 38 |
# * <tt>:default</tt>: default value for the plugin settings |
|
| 39 |
# * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory |
|
| 40 |
# Example: |
|
| 41 |
# settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
|
|
| 42 |
# In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>. |
|
| 43 |
# |
|
| 44 |
# When rendered, the plugin settings value is available as the local variable +settings+ |
|
| 45 |
class Plugin |
|
| 46 |
cattr_accessor :directory |
|
| 47 |
self.directory = File.join(Rails.root, 'plugins') |
|
| 48 |
|
|
| 49 |
cattr_accessor :public_directory |
|
| 50 |
self.public_directory = File.join(Rails.root, 'public', 'plugin_assets') |
|
| 51 |
|
|
| 52 |
@registered_plugins = {}
|
|
| 53 |
class << self |
|
| 54 |
attr_reader :registered_plugins |
|
| 55 |
private :new |
|
| 56 |
|
|
| 57 |
def def_field(*names) |
|
| 58 |
class_eval do |
|
| 59 |
names.each do |name| |
|
| 60 |
define_method(name) do |*args| |
|
| 61 |
args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
|
|
| 62 |
end |
|
| 63 |
end |
|
| 64 |
end |
|
| 65 |
end |
|
| 66 |
end |
|
| 67 |
def_field :name, :description, :url, :author, :author_url, :version, :settings |
|
| 68 |
attr_reader :id |
|
| 69 |
|
|
| 70 |
# Plugin constructor |
|
| 71 |
def self.register(id, &block) |
|
| 72 |
p = new(id) |
|
| 73 |
p.instance_eval(&block) |
|
| 74 |
# Set a default name if it was not provided during registration |
|
| 75 |
p.name(id.to_s.humanize) if p.name.nil? |
|
| 76 |
|
|
| 77 |
# Adds plugin locales if any |
|
| 78 |
# YAML translation files should be found under <plugin>/config/locales/ |
|
| 79 |
::I18n.load_path += Dir.glob(File.join(p.directory, 'config', 'locales', '*.yml')) |
|
| 80 |
|
|
| 81 |
# Prepends the app/views directory of the plugin to the view path |
|
| 82 |
view_path = File.join(p.directory, 'app', 'views') |
|
| 83 |
if File.directory?(view_path) |
|
| 84 |
ActionController::Base.prepend_view_path(view_path) |
|
| 85 |
ActionMailer::Base.prepend_view_path(view_path) |
|
| 86 |
end |
|
| 87 |
|
|
| 88 |
# Adds the app/{controllers,helpers,models} directories of the plugin to the autoload path
|
|
| 89 |
Dir.glob File.expand_path(File.join(p.directory, 'app', '{controllers,helpers,models}')) do |dir|
|
|
| 90 |
ActiveSupport::Dependencies.autoload_paths += [dir] |
|
| 91 |
end |
|
| 92 |
|
|
| 93 |
registered_plugins[id] = p |
|
| 94 |
end |
|
| 95 |
|
|
| 96 |
# Returns an array of all registered plugins |
|
| 97 |
def self.all |
|
| 98 |
registered_plugins.values.sort |
|
| 99 |
end |
|
| 100 |
|
|
| 101 |
# Finds a plugin by its id |
|
| 102 |
# Returns a PluginNotFound exception if the plugin doesn't exist |
|
| 103 |
def self.find(id) |
|
| 104 |
registered_plugins[id.to_sym] || raise(PluginNotFound) |
|
| 105 |
end |
|
| 106 |
|
|
| 107 |
# Clears the registered plugins hash |
|
| 108 |
# It doesn't unload installed plugins |
|
| 109 |
def self.clear |
|
| 110 |
@registered_plugins = {}
|
|
| 111 |
end |
|
| 112 |
|
|
| 113 |
# Checks if a plugin is installed |
|
| 114 |
# |
|
| 115 |
# @param [String] id name of the plugin |
|
| 116 |
def self.installed?(id) |
|
| 117 |
registered_plugins[id.to_sym].present? |
|
| 118 |
end |
|
| 119 |
|
|
| 120 |
def self.load |
|
| 121 |
Dir.glob(File.join(self.directory, '*')).sort.each do |directory| |
|
| 122 |
if File.directory?(directory) |
|
| 123 |
lib = File.join(directory, "lib") |
|
| 124 |
if File.directory?(lib) |
|
| 125 |
$:.unshift lib |
|
| 126 |
ActiveSupport::Dependencies.autoload_paths += [lib] |
|
| 127 |
end |
|
| 128 |
initializer = File.join(directory, "init.rb") |
|
| 129 |
if File.file?(initializer) |
|
| 130 |
require initializer |
|
| 131 |
end |
|
| 132 |
end |
|
| 133 |
end |
|
| 134 |
end |
|
| 135 |
|
|
| 136 |
def initialize(id) |
|
| 137 |
@id = id.to_sym |
|
| 138 |
end |
|
| 139 |
|
|
| 140 |
def directory |
|
| 141 |
File.join(self.class.directory, id.to_s) |
|
| 142 |
end |
|
| 143 |
|
|
| 144 |
def public_directory |
|
| 145 |
File.join(self.class.public_directory, id.to_s) |
|
| 146 |
end |
|
| 147 |
|
|
| 148 |
def to_param |
|
| 149 |
id |
|
| 150 |
end |
|
| 151 |
|
|
| 152 |
def assets_directory |
|
| 153 |
File.join(directory, 'assets') |
|
| 154 |
end |
|
| 155 |
|
|
| 156 |
def <=>(plugin) |
|
| 157 |
self.id.to_s <=> plugin.id.to_s |
|
| 158 |
end |
|
| 159 |
|
|
| 160 |
# Sets a requirement on Redmine version |
|
| 161 |
# Raises a PluginRequirementError exception if the requirement is not met |
|
| 162 |
# |
|
| 163 |
# Examples |
|
| 164 |
# # Requires Redmine 0.7.3 or higher |
|
| 165 |
# requires_redmine :version_or_higher => '0.7.3' |
|
| 166 |
# requires_redmine '0.7.3' |
|
| 167 |
# |
|
| 168 |
# # Requires Redmine 0.7.x or higher |
|
| 169 |
# requires_redmine '0.7' |
|
| 170 |
# |
|
| 171 |
# # Requires a specific Redmine version |
|
| 172 |
# requires_redmine :version => '0.7.3' # 0.7.3 only |
|
| 173 |
# requires_redmine :version => '0.7' # 0.7.x |
|
| 174 |
# requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 |
|
| 175 |
# |
|
| 176 |
# # Requires a Redmine version within a range |
|
| 177 |
# requires_redmine :version => '0.7.3'..'0.9.1' # >= 0.7.3 and <= 0.9.1 |
|
| 178 |
# requires_redmine :version => '0.7'..'0.9' # >= 0.7.x and <= 0.9.x |
|
| 179 |
def requires_redmine(arg) |
|
| 180 |
arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
|
|
| 181 |
arg.assert_valid_keys(:version, :version_or_higher) |
|
| 182 |
|
|
| 183 |
current = Redmine::VERSION.to_a |
|
| 184 |
arg.each do |k, req| |
|
| 185 |
case k |
|
| 186 |
when :version_or_higher |
|
| 187 |
raise ArgumentError.new(":version_or_higher accepts a version string only") unless req.is_a?(String)
|
|
| 188 |
unless compare_versions(req, current) <= 0 |
|
| 189 |
raise PluginRequirementError.new("#{id} plugin requires Redmine #{req} or higher but current is #{current.join('.')}")
|
|
| 190 |
end |
|
| 191 |
when :version |
|
| 192 |
req = [req] if req.is_a?(String) |
|
| 193 |
if req.is_a?(Array) |
|
| 194 |
unless req.detect {|ver| compare_versions(ver, current) == 0}
|
|
| 195 |
raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{req.join(', ')} but current is #{current.join('.')}")
|
|
| 196 |
end |
|
| 197 |
elsif req.is_a?(Range) |
|
| 198 |
unless compare_versions(req.first, current) <= 0 && compare_versions(req.last, current) >= 0 |
|
| 199 |
raise PluginRequirementError.new("#{id} plugin requires a Redmine version between #{req.first} and #{req.last} but current is #{current.join('.')}")
|
|
| 200 |
end |
|
| 201 |
else |
|
| 202 |
raise ArgumentError.new(":version option accepts a version string, an array or a range of versions")
|
|
| 203 |
end |
|
| 204 |
end |
|
| 205 |
end |
|
| 206 |
true |
|
| 207 |
end |
|
| 208 |
|
|
| 209 |
def compare_versions(requirement, current) |
|
| 210 |
requirement = requirement.split('.').collect(&:to_i)
|
|
| 211 |
requirement <=> current.slice(0, requirement.size) |
|
| 212 |
end |
|
| 213 |
private :compare_versions |
|
| 214 |
|
|
| 215 |
# Sets a requirement on a Redmine plugin version |
|
| 216 |
# Raises a PluginRequirementError exception if the requirement is not met |
|
| 217 |
# |
|
| 218 |
# Examples |
|
| 219 |
# # Requires a plugin named :foo version 0.7.3 or higher |
|
| 220 |
# requires_redmine_plugin :foo, :version_or_higher => '0.7.3' |
|
| 221 |
# requires_redmine_plugin :foo, '0.7.3' |
|
| 222 |
# |
|
| 223 |
# # Requires a specific version of a Redmine plugin |
|
| 224 |
# requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only |
|
| 225 |
# requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0 |
|
| 226 |
def requires_redmine_plugin(plugin_name, arg) |
|
| 227 |
arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
|
|
| 228 |
arg.assert_valid_keys(:version, :version_or_higher) |
|
| 229 |
|
|
| 230 |
plugin = Plugin.find(plugin_name) |
|
| 231 |
current = plugin.version.split('.').collect(&:to_i)
|
|
| 232 |
|
|
| 233 |
arg.each do |k, v| |
|
| 234 |
v = [] << v unless v.is_a?(Array) |
|
| 235 |
versions = v.collect {|s| s.split('.').collect(&:to_i)}
|
|
| 236 |
case k |
|
| 237 |
when :version_or_higher |
|
| 238 |
raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
|
|
| 239 |
unless (current <=> versions.first) >= 0 |
|
| 240 |
raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
|
|
| 241 |
end |
|
| 242 |
when :version |
|
| 243 |
unless versions.include?(current.slice(0,3)) |
|
| 244 |
raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
|
|
| 245 |
end |
|
| 246 |
end |
|
| 247 |
end |
|
| 248 |
true |
|
| 249 |
end |
|
| 250 |
|
|
| 251 |
# Adds an item to the given +menu+. |
|
| 252 |
# The +id+ parameter (equals to the project id) is automatically added to the url. |
|
| 253 |
# menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
|
|
| 254 |
# |
|
| 255 |
# +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu |
|
| 256 |
# |
|
| 257 |
def menu(menu, item, url, options={})
|
|
| 258 |
Redmine::MenuManager.map(menu).push(item, url, options) |
|
| 259 |
end |
|
| 260 |
alias :add_menu_item :menu |
|
| 261 |
|
|
| 262 |
# Removes +item+ from the given +menu+. |
|
| 263 |
def delete_menu_item(menu, item) |
|
| 264 |
Redmine::MenuManager.map(menu).delete(item) |
|
| 265 |
end |
|
| 266 |
|
|
| 267 |
# Defines a permission called +name+ for the given +actions+. |
|
| 268 |
# |
|
| 269 |
# The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array): |
|
| 270 |
# permission :destroy_contacts, { :contacts => :destroy }
|
|
| 271 |
# permission :view_contacts, { :contacts => [:index, :show] }
|
|
| 272 |
# |
|
| 273 |
# The +options+ argument is a hash that accept the following keys: |
|
| 274 |
# * :public => the permission is public if set to true (implicitly given to any user) |
|
| 275 |
# * :require => can be set to one of the following values to restrict users the permission can be given to: :loggedin, :member |
|
| 276 |
# * :read => set it to true so that the permission is still granted on closed projects |
|
| 277 |
# |
|
| 278 |
# Examples |
|
| 279 |
# # A permission that is implicitly given to any user |
|
| 280 |
# # This permission won't appear on the Roles & Permissions setup screen |
|
| 281 |
# permission :say_hello, { :example => :say_hello }, :public => true, :read => true
|
|
| 282 |
# |
|
| 283 |
# # A permission that can be given to any user |
|
| 284 |
# permission :say_hello, { :example => :say_hello }
|
|
| 285 |
# |
|
| 286 |
# # A permission that can be given to registered users only |
|
| 287 |
# permission :say_hello, { :example => :say_hello }, :require => :loggedin
|
|
| 288 |
# |
|
| 289 |
# # A permission that can be given to project members only |
|
| 290 |
# permission :say_hello, { :example => :say_hello }, :require => :member
|
|
| 291 |
def permission(name, actions, options = {})
|
|
| 292 |
if @project_module |
|
| 293 |
Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
|
|
| 294 |
else |
|
| 295 |
Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
|
|
| 296 |
end |
|
| 297 |
end |
|
| 298 |
|
|
| 299 |
# Defines a project module, that can be enabled/disabled for each project. |
|
| 300 |
# Permissions defined inside +block+ will be bind to the module. |
|
| 301 |
# |
|
| 302 |
# project_module :things do |
|
| 303 |
# permission :view_contacts, { :contacts => [:list, :show] }, :public => true
|
|
| 304 |
# permission :destroy_contacts, { :contacts => :destroy }
|
|
| 305 |
# end |
|
| 306 |
def project_module(name, &block) |
|
| 307 |
@project_module = name |
|
| 308 |
self.instance_eval(&block) |
|
| 309 |
@project_module = nil |
|
| 310 |
end |
|
| 311 |
|
|
| 312 |
# Registers an activity provider. |
|
| 313 |
# |
|
| 314 |
# Options: |
|
| 315 |
# * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default) |
|
| 316 |
# * <tt>:default</tt> - setting this option to false will make the events not displayed by default |
|
| 317 |
# |
|
| 318 |
# A model can provide several activity event types. |
|
| 319 |
# |
|
| 320 |
# Examples: |
|
| 321 |
# register :news |
|
| 322 |
# register :scrums, :class_name => 'Meeting' |
|
| 323 |
# register :issues, :class_name => ['Issue', 'Journal'] |
|
| 324 |
# |
|
| 325 |
# Retrieving events: |
|
| 326 |
# Associated model(s) must implement the find_events class method. |
|
| 327 |
# ActiveRecord models can use acts_as_activity_provider as a way to implement this class method. |
|
| 328 |
# |
|
| 329 |
# The following call should return all the scrum events visible by current user that occured in the 5 last days: |
|
| 330 |
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
|
|
| 331 |
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
|
|
| 332 |
# |
|
| 333 |
# Note that :view_scrums permission is required to view these events in the activity view. |
|
| 334 |
def activity_provider(*args) |
|
| 335 |
Redmine::Activity.register(*args) |
|
| 336 |
end |
|
| 337 |
|
|
| 338 |
# Registers a wiki formatter. |
|
| 339 |
# |
|
| 340 |
# Parameters: |
|
| 341 |
# * +name+ - human-readable name |
|
| 342 |
# * +formatter+ - formatter class, which should have an instance method +to_html+ |
|
| 343 |
# * +helper+ - helper module, which will be included by wiki pages |
|
| 344 |
def wiki_format_provider(name, formatter, helper) |
|
| 345 |
Redmine::WikiFormatting.register(name, formatter, helper) |
|
| 346 |
end |
|
| 347 |
|
|
| 348 |
# Returns +true+ if the plugin can be configured. |
|
| 349 |
def configurable? |
|
| 350 |
settings && settings.is_a?(Hash) && !settings[:partial].blank? |
|
| 351 |
end |
|
| 352 |
|
|
| 353 |
def mirror_assets |
|
| 354 |
source = assets_directory |
|
| 355 |
destination = public_directory |
|
| 356 |
return unless File.directory?(source) |
|
| 357 |
|
|
| 358 |
source_files = Dir[source + "/**/*"] |
|
| 359 |
source_dirs = source_files.select { |d| File.directory?(d) }
|
|
| 360 |
source_files -= source_dirs |
|
| 361 |
|
|
| 362 |
unless source_files.empty? |
|
| 363 |
base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, '')) |
|
| 364 |
begin |
|
| 365 |
FileUtils.mkdir_p(base_target_dir) |
|
| 366 |
rescue Exception => e |
|
| 367 |
raise "Could not create directory #{base_target_dir}: " + e.message
|
|
| 368 |
end |
|
| 369 |
end |
|
| 370 |
|
|
| 371 |
source_dirs.each do |dir| |
|
| 372 |
# strip down these paths so we have simple, relative paths we can |
|
| 373 |
# add to the destination |
|
| 374 |
target_dir = File.join(destination, dir.gsub(source, '')) |
|
| 375 |
begin |
|
| 376 |
FileUtils.mkdir_p(target_dir) |
|
| 377 |
rescue Exception => e |
|
| 378 |
raise "Could not create directory #{target_dir}: " + e.message
|
|
| 379 |
end |
|
| 380 |
end |
|
| 381 |
|
|
| 382 |
source_files.each do |file| |
|
| 383 |
begin |
|
| 384 |
target = File.join(destination, file.gsub(source, '')) |
|
| 385 |
unless File.exist?(target) && FileUtils.identical?(file, target) |
|
| 386 |
FileUtils.cp(file, target) |
|
| 387 |
end |
|
| 388 |
rescue Exception => e |
|
| 389 |
raise "Could not copy #{file} to #{target}: " + e.message
|
|
| 390 |
end |
|
| 391 |
end |
|
| 392 |
end |
|
| 393 |
|
|
| 394 |
# Mirrors assets from one or all plugins to public/plugin_assets |
|
| 395 |
def self.mirror_assets(name=nil) |
|
| 396 |
if name.present? |
|
| 397 |
find(name).mirror_assets |
|
| 398 |
else |
|
| 399 |
all.each do |plugin| |
|
| 400 |
plugin.mirror_assets |
|
| 401 |
end |
|
| 402 |
end |
|
| 403 |
end |
|
| 404 |
|
|
| 405 |
# The directory containing this plugin's migrations (<tt>plugin/db/migrate</tt>) |
|
| 406 |
def migration_directory |
|
| 407 |
File.join(Rails.root, 'plugins', id.to_s, 'db', 'migrate') |
|
| 408 |
end |
|
| 409 |
|
|
| 410 |
# Returns the version number of the latest migration for this plugin. Returns |
|
| 411 |
# nil if this plugin has no migrations. |
|
| 412 |
def latest_migration |
|
| 413 |
migrations.last |
|
| 414 |
end |
|
| 415 |
|
|
| 416 |
# Returns the version numbers of all migrations for this plugin. |
|
| 417 |
def migrations |
|
| 418 |
migrations = Dir[migration_directory+"/*.rb"] |
|
| 419 |
migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
|
|
| 420 |
end |
|
| 421 |
|
|
| 422 |
# Migrate this plugin to the given version |
|
| 423 |
def migrate(version = nil) |
|
| 424 |
puts "Migrating #{id} (#{name})..."
|
|
| 425 |
Redmine::Plugin::Migrator.migrate_plugin(self, version) |
|
| 426 |
end |
|
| 427 |
|
|
| 428 |
# Migrates all plugins or a single plugin to a given version |
|
| 429 |
# Exemples: |
|
| 430 |
# Plugin.migrate |
|
| 431 |
# Plugin.migrate('sample_plugin')
|
|
| 432 |
# Plugin.migrate('sample_plugin', 1)
|
|
| 433 |
# |
|
| 434 |
def self.migrate(name=nil, version=nil) |
|
| 435 |
if name.present? |
|
| 436 |
find(name).migrate(version) |
|
| 437 |
else |
|
| 438 |
all.each do |plugin| |
|
| 439 |
plugin.migrate |
|
| 440 |
end |
|
| 441 |
end |
|
| 442 |
end |
|
| 443 |
|
|
| 444 |
class Migrator < ActiveRecord::Migrator |
|
| 445 |
# We need to be able to set the 'current' plugin being migrated. |
|
| 446 |
cattr_accessor :current_plugin |
|
| 447 |
|
|
| 448 |
class << self |
|
| 449 |
# Runs the migrations from a plugin, up (or down) to the version given |
|
| 450 |
def migrate_plugin(plugin, version) |
|
| 451 |
self.current_plugin = plugin |
|
| 452 |
return if current_version(plugin) == version |
|
| 453 |
migrate(plugin.migration_directory, version) |
|
| 454 |
end |
|
| 455 |
|
|
| 456 |
def current_version(plugin=current_plugin) |
|
| 457 |
# Delete migrations that don't match .. to_i will work because the number comes first |
|
| 458 |
::ActiveRecord::Base.connection.select_values( |
|
| 459 |
"SELECT version FROM #{schema_migrations_table_name}"
|
|
| 460 |
).delete_if{ |v| v.match(/-#{plugin.id}/) == nil }.map(&:to_i).max || 0
|
|
| 461 |
end |
|
| 462 |
end |
|
| 463 |
|
|
| 464 |
def migrated |
|
| 465 |
sm_table = self.class.schema_migrations_table_name |
|
| 466 |
::ActiveRecord::Base.connection.select_values( |
|
| 467 |
"SELECT version FROM #{sm_table}"
|
|
| 468 |
).delete_if{ |v| v.match(/-#{current_plugin.id}/) == nil }.map(&:to_i).sort
|
|
| 469 |
end |
|
| 470 |
|
|
| 471 |
def record_version_state_after_migrating(version) |
|
| 472 |
super(version.to_s + "-" + current_plugin.id.to_s) |
|
| 473 |
end |
|
| 474 |
end |
|
| 475 |
end |
|
| 476 |
end |
|
| .svn/pristine/07/073a34bd97c7ca9066ab7d9fa3f008e0e0da8c60.svn-base | ||
|---|---|---|
| 1 |
var draw_gantt = null; |
|
| 2 |
var draw_top; |
|
| 3 |
var draw_right; |
|
| 4 |
var draw_left; |
|
| 5 |
|
|
| 6 |
var rels_stroke_width = 2; |
|
| 7 |
|
|
| 8 |
function setDrawArea() {
|
|
| 9 |
draw_top = $("#gantt_draw_area").position().top;
|
|
| 10 |
draw_right = $("#gantt_draw_area").width();
|
|
| 11 |
draw_left = $("#gantt_area").scrollLeft();
|
|
| 12 |
} |
|
| 13 |
|
|
| 14 |
function getRelationsArray() {
|
|
| 15 |
var arr = new Array(); |
|
| 16 |
$.each($('div.task_todo[data-rels]'), function(index_div, element) {
|
|
| 17 |
var element_id = $(element).attr("id");
|
|
| 18 |
if (element_id != null) {
|
|
| 19 |
var issue_id = element_id.replace("task-todo-issue-", "");
|
|
| 20 |
var data_rels = $(element).data("rels");
|
|
| 21 |
for (rel_type_key in data_rels) {
|
|
| 22 |
$.each(data_rels[rel_type_key], function(index_issue, element_issue) {
|
|
| 23 |
arr.push({issue_from: issue_id, issue_to: element_issue,
|
|
| 24 |
rel_type: rel_type_key}); |
|
| 25 |
}); |
|
| 26 |
} |
|
| 27 |
} |
|
| 28 |
}); |
|
| 29 |
return arr; |
|
| 30 |
} |
|
| 31 |
|
|
| 32 |
function drawRelations() {
|
|
| 33 |
var arr = getRelationsArray(); |
|
| 34 |
$.each(arr, function(index_issue, element_issue) {
|
|
| 35 |
var issue_from = $("#task-todo-issue-" + element_issue["issue_from"]);
|
|
| 36 |
var issue_to = $("#task-todo-issue-" + element_issue["issue_to"]);
|
|
| 37 |
if (issue_from.size() == 0 || issue_to.size() == 0) {
|
|
| 38 |
return; |
|
| 39 |
} |
|
| 40 |
var issue_height = issue_from.height(); |
|
| 41 |
var issue_from_top = issue_from.position().top + (issue_height / 2) - draw_top; |
|
| 42 |
var issue_from_right = issue_from.position().left + issue_from.width(); |
|
| 43 |
var issue_to_top = issue_to.position().top + (issue_height / 2) - draw_top; |
|
| 44 |
var issue_to_left = issue_to.position().left; |
|
| 45 |
var color = issue_relation_type[element_issue["rel_type"]]["color"]; |
|
| 46 |
var landscape_margin = issue_relation_type[element_issue["rel_type"]]["landscape_margin"]; |
|
| 47 |
var issue_from_right_rel = issue_from_right + landscape_margin; |
|
| 48 |
var issue_to_left_rel = issue_to_left - landscape_margin; |
|
| 49 |
draw_gantt.path(["M", issue_from_right + draw_left, issue_from_top, |
|
| 50 |
"L", issue_from_right_rel + draw_left, issue_from_top]) |
|
| 51 |
.attr({stroke: color,
|
|
| 52 |
"stroke-width": rels_stroke_width |
|
| 53 |
}); |
|
| 54 |
if (issue_from_right_rel < issue_to_left_rel) {
|
|
| 55 |
draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top, |
|
| 56 |
"L", issue_from_right_rel + draw_left, issue_to_top]) |
|
| 57 |
.attr({stroke: color,
|
|
| 58 |
"stroke-width": rels_stroke_width |
|
| 59 |
}); |
|
| 60 |
draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_to_top, |
|
| 61 |
"L", issue_to_left + draw_left, issue_to_top]) |
|
| 62 |
.attr({stroke: color,
|
|
| 63 |
"stroke-width": rels_stroke_width |
|
| 64 |
}); |
|
| 65 |
} else {
|
|
| 66 |
var issue_middle_top = issue_to_top + |
|
| 67 |
(issue_height * |
|
| 68 |
((issue_from_top > issue_to_top) ? 1 : -1)); |
|
| 69 |
draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top, |
|
| 70 |
"L", issue_from_right_rel + draw_left, issue_middle_top]) |
|
| 71 |
.attr({stroke: color,
|
|
| 72 |
"stroke-width": rels_stroke_width |
|
| 73 |
}); |
|
| 74 |
draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_middle_top, |
|
| 75 |
"L", issue_to_left_rel + draw_left, issue_middle_top]) |
|
| 76 |
.attr({stroke: color,
|
|
| 77 |
"stroke-width": rels_stroke_width |
|
| 78 |
}); |
|
| 79 |
draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_middle_top, |
|
| 80 |
"L", issue_to_left_rel + draw_left, issue_to_top]) |
|
| 81 |
.attr({stroke: color,
|
|
| 82 |
"stroke-width": rels_stroke_width |
|
| 83 |
}); |
|
| 84 |
draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_to_top, |
|
| 85 |
"L", issue_to_left + draw_left, issue_to_top]) |
|
| 86 |
.attr({stroke: color,
|
|
| 87 |
"stroke-width": rels_stroke_width |
|
| 88 |
}); |
|
| 89 |
} |
|
| 90 |
draw_gantt.path(["M", issue_to_left + draw_left, issue_to_top, |
|
| 91 |
"l", -4 * rels_stroke_width, -2 * rels_stroke_width, |
|
| 92 |
"l", 0, 4 * rels_stroke_width, "z"]) |
|
| 93 |
.attr({stroke: "none",
|
|
| 94 |
fill: color, |
|
| 95 |
"stroke-linecap": "butt", |
|
| 96 |
"stroke-linejoin": "miter" |
|
| 97 |
}); |
|
| 98 |
}); |
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
function getProgressLinesArray() {
|
|
| 102 |
var arr = new Array(); |
|
| 103 |
var today_left = $('#today_line').position().left;
|
|
| 104 |
arr.push({left: today_left, top: 0});
|
|
| 105 |
$.each($('div.issue-subject, div.version-name'), function(index, element) {
|
|
| 106 |
var t = $(element).position().top - draw_top ; |
|
| 107 |
var h = ($(element).height() / 9); |
|
| 108 |
var element_top_upper = t - h; |
|
| 109 |
var element_top_center = t + (h * 3); |
|
| 110 |
var element_top_lower = t + (h * 8); |
|
| 111 |
var issue_closed = $(element).children('span').hasClass('issue-closed');
|
|
| 112 |
var version_closed = $(element).children('span').hasClass('version-closed');
|
|
| 113 |
if (issue_closed || version_closed) {
|
|
| 114 |
arr.push({left: today_left, top: element_top_center});
|
|
| 115 |
} else {
|
|
| 116 |
var issue_done = $("#task-done-" + $(element).attr("id"));
|
|
| 117 |
var is_behind_start = $(element).children('span').hasClass('behind-start-date');
|
|
| 118 |
var is_over_end = $(element).children('span').hasClass('over-end-date');
|
|
| 119 |
if (is_over_end) {
|
|
| 120 |
arr.push({left: draw_right, top: element_top_upper, is_right_edge: true});
|
|
| 121 |
arr.push({left: draw_right, top: element_top_lower, is_right_edge: true, none_stroke: true});
|
|
| 122 |
} else if (issue_done.size() > 0) {
|
|
| 123 |
var done_left = issue_done.first().position().left + |
|
| 124 |
issue_done.first().width(); |
|
| 125 |
arr.push({left: done_left, top: element_top_center});
|
|
| 126 |
} else if (is_behind_start) {
|
|
| 127 |
arr.push({left: 0 , top: element_top_upper, is_left_edge: true});
|
|
| 128 |
arr.push({left: 0 , top: element_top_lower, is_left_edge: true, none_stroke: true});
|
|
| 129 |
} else {
|
|
| 130 |
var todo_left = today_left; |
|
| 131 |
var issue_todo = $("#task-todo-" + $(element).attr("id"));
|
|
| 132 |
if (issue_todo.size() > 0){
|
|
| 133 |
todo_left = issue_todo.first().position().left; |
|
| 134 |
} |
|
| 135 |
arr.push({left: Math.min(today_left, todo_left), top: element_top_center});
|
|
| 136 |
} |
|
| 137 |
} |
|
| 138 |
}); |
|
| 139 |
return arr; |
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
function drawGanttProgressLines() {
|
|
| 143 |
var arr = getProgressLinesArray(); |
|
| 144 |
var color = $("#today_line")
|
|
| 145 |
.css("border-left-color");
|
|
| 146 |
var i; |
|
| 147 |
for(i = 1 ; i < arr.length ; i++) {
|
|
| 148 |
if (!("none_stroke" in arr[i]) &&
|
|
| 149 |
(!("is_right_edge" in arr[i - 1] && "is_right_edge" in arr[i]) &&
|
|
| 150 |
!("is_left_edge" in arr[i - 1] && "is_left_edge" in arr[i]))
|
|
| 151 |
) {
|
|
| 152 |
var x1 = (arr[i - 1].left == 0) ? 0 : arr[i - 1].left + draw_left; |
|
| 153 |
var x2 = (arr[i].left == 0) ? 0 : arr[i].left + draw_left; |
|
| 154 |
draw_gantt.path(["M", x1, arr[i - 1].top, |
|
| 155 |
"L", x2, arr[i].top]) |
|
| 156 |
.attr({stroke: color, "stroke-width": 2});
|
|
| 157 |
} |
|
| 158 |
} |
|
| 159 |
} |
|
| 160 |
|
|
| 161 |
function drawGanttHandler() {
|
|
| 162 |
var folder = document.getElementById('gantt_draw_area');
|
|
| 163 |
if(draw_gantt != null) |
|
| 164 |
draw_gantt.clear(); |
|
| 165 |
else |
|
| 166 |
draw_gantt = Raphael(folder); |
|
| 167 |
setDrawArea(); |
|
| 168 |
if ($("#draw_progress_line").attr('checked'))
|
|
| 169 |
drawGanttProgressLines(); |
|
| 170 |
if ($("#draw_rels").attr('checked'))
|
|
| 171 |
drawRelations(); |
|
| 172 |
} |
|
| .svn/pristine/07/07402976102eaabe37c42cc6aab7aeb00d221891.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2011 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 14 |
# You should have received a copy of the GNU General Public License |
|
| 15 |
# along with this program; if not, write to the Free Software |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
require File.expand_path('../../test_helper', __FILE__)
|
|
| 19 |
require 'repositories_controller' |
|
| 20 |
|
|
| 21 |
# Re-raise errors caught by the controller. |
|
| 22 |
class RepositoriesController; def rescue_action(e) raise e end; end |
|
| 23 |
|
|
| 24 |
class RepositoriesDarcsControllerTest < ActionController::TestCase |
|
| 25 |
fixtures :projects, :users, :roles, :members, :member_roles, |
|
| 26 |
:repositories, :enabled_modules |
|
| 27 |
|
|
| 28 |
REPOSITORY_PATH = Rails.root.join('tmp/test/darcs_repository').to_s
|
|
| 29 |
PRJ_ID = 3 |
|
| 30 |
NUM_REV = 6 |
|
| 31 |
|
|
| 32 |
def setup |
|
| 33 |
@controller = RepositoriesController.new |
|
| 34 |
@request = ActionController::TestRequest.new |
|
| 35 |
@response = ActionController::TestResponse.new |
|
| 36 |
User.current = nil |
|
| 37 |
@project = Project.find(PRJ_ID) |
|
| 38 |
@repository = Repository::Darcs.create( |
|
| 39 |
:project => @project, |
|
| 40 |
:url => REPOSITORY_PATH, |
|
| 41 |
:log_encoding => 'UTF-8' |
|
| 42 |
) |
|
| 43 |
assert @repository |
|
| 44 |
end |
|
| 45 |
|
|
| 46 |
if File.directory?(REPOSITORY_PATH) |
|
| 47 |
def test_browse_root |
|
| 48 |
assert_equal 0, @repository.changesets.count |
|
| 49 |
@repository.fetch_changesets |
|
| 50 |
@project.reload |
|
| 51 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 52 |
get :show, :id => PRJ_ID |
|
| 53 |
assert_response :success |
|
| 54 |
assert_template 'show' |
|
| 55 |
assert_not_nil assigns(:entries) |
|
| 56 |
assert_equal 3, assigns(:entries).size |
|
| 57 |
assert assigns(:entries).detect {|e| e.name == 'images' && e.kind == 'dir'}
|
|
| 58 |
assert assigns(:entries).detect {|e| e.name == 'sources' && e.kind == 'dir'}
|
|
| 59 |
assert assigns(:entries).detect {|e| e.name == 'README' && e.kind == 'file'}
|
|
| 60 |
end |
|
| 61 |
|
|
| 62 |
def test_browse_directory |
|
| 63 |
assert_equal 0, @repository.changesets.count |
|
| 64 |
@repository.fetch_changesets |
|
| 65 |
@project.reload |
|
| 66 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 67 |
get :show, :id => PRJ_ID, :path => ['images'] |
|
| 68 |
assert_response :success |
|
| 69 |
assert_template 'show' |
|
| 70 |
assert_not_nil assigns(:entries) |
|
| 71 |
assert_equal ['delete.png', 'edit.png'], assigns(:entries).collect(&:name) |
|
| 72 |
entry = assigns(:entries).detect {|e| e.name == 'edit.png'}
|
|
| 73 |
assert_not_nil entry |
|
| 74 |
assert_equal 'file', entry.kind |
|
| 75 |
assert_equal 'images/edit.png', entry.path |
|
| 76 |
end |
|
| 77 |
|
|
| 78 |
def test_browse_at_given_revision |
|
| 79 |
assert_equal 0, @repository.changesets.count |
|
| 80 |
@repository.fetch_changesets |
|
| 81 |
@project.reload |
|
| 82 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 83 |
get :show, :id => PRJ_ID, :path => ['images'], :rev => 1 |
|
| 84 |
assert_response :success |
|
| 85 |
assert_template 'show' |
|
| 86 |
assert_not_nil assigns(:entries) |
|
| 87 |
assert_equal ['delete.png'], assigns(:entries).collect(&:name) |
|
| 88 |
end |
|
| 89 |
|
|
| 90 |
def test_changes |
|
| 91 |
assert_equal 0, @repository.changesets.count |
|
| 92 |
@repository.fetch_changesets |
|
| 93 |
@project.reload |
|
| 94 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 95 |
get :changes, :id => PRJ_ID, :path => ['images', 'edit.png'] |
|
| 96 |
assert_response :success |
|
| 97 |
assert_template 'changes' |
|
| 98 |
assert_tag :tag => 'h2', :content => 'edit.png' |
|
| 99 |
end |
|
| 100 |
|
|
| 101 |
def test_diff |
|
| 102 |
assert_equal 0, @repository.changesets.count |
|
| 103 |
@repository.fetch_changesets |
|
| 104 |
@project.reload |
|
| 105 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 106 |
# Full diff of changeset 5 |
|
| 107 |
['inline', 'sbs'].each do |dt| |
|
| 108 |
get :diff, :id => PRJ_ID, :rev => 5, :type => dt |
|
| 109 |
assert_response :success |
|
| 110 |
assert_template 'diff' |
|
| 111 |
# Line 22 removed |
|
| 112 |
assert_tag :tag => 'th', |
|
| 113 |
:content => '22', |
|
| 114 |
:sibling => { :tag => 'td',
|
|
| 115 |
:attributes => { :class => /diff_out/ },
|
|
| 116 |
:content => /def remove/ } |
|
| 117 |
end |
|
| 118 |
end |
|
| 119 |
|
|
| 120 |
def test_destroy_valid_repository |
|
| 121 |
@request.session[:user_id] = 1 # admin |
|
| 122 |
assert_equal 0, @repository.changesets.count |
|
| 123 |
@repository.fetch_changesets |
|
| 124 |
@project.reload |
|
| 125 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 126 |
|
|
| 127 |
get :destroy, :id => PRJ_ID |
|
| 128 |
assert_response 302 |
|
| 129 |
@project.reload |
|
| 130 |
assert_nil @project.repository |
|
| 131 |
end |
|
| 132 |
|
|
| 133 |
def test_destroy_invalid_repository |
|
| 134 |
@request.session[:user_id] = 1 # admin |
|
| 135 |
assert_equal 0, @repository.changesets.count |
|
| 136 |
@repository.fetch_changesets |
|
| 137 |
@project.reload |
|
| 138 |
assert_equal NUM_REV, @repository.changesets.count |
|
| 139 |
|
|
| 140 |
get :destroy, :id => PRJ_ID |
|
| 141 |
assert_response 302 |
|
| 142 |
@project.reload |
|
| 143 |
assert_nil @project.repository |
|
| 144 |
|
|
| 145 |
@repository = Repository::Darcs.create( |
|
| 146 |
:project => @project, |
|
| 147 |
:url => "/invalid", |
|
| 148 |
:log_encoding => 'UTF-8' |
|
| 149 |
) |
|
| 150 |
assert @repository |
|
| 151 |
@repository.fetch_changesets |
|
| 152 |
@project.reload |
|
| 153 |
assert_equal 0, @repository.changesets.count |
|
| 154 |
|
|
| 155 |
get :destroy, :id => PRJ_ID |
|
| 156 |
assert_response 302 |
|
| 157 |
@project.reload |
|
| 158 |
assert_nil @project.repository |
|
| 159 |
end |
|
| 160 |
else |
|
| 161 |
puts "Darcs test repository NOT FOUND. Skipping functional tests !!!" |
|
| 162 |
def test_fake; assert true end |
|
| 163 |
end |
|
| 164 |
end |
|
| .svn/pristine/07/074588d42b3ac157ab70417170443c5531685d7e.svn-base | ||
|---|---|---|
| 1 |
module ActiveRecord |
|
| 2 |
module Acts #:nodoc: |
|
| 3 |
module List #:nodoc: |
|
| 4 |
def self.included(base) |
|
| 5 |
base.extend(ClassMethods) |
|
| 6 |
end |
|
| 7 |
|
|
| 8 |
# This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list. |
|
| 9 |
# The class that has this specified needs to have a +position+ column defined as an integer on |
|
| 10 |
# the mapped database table. |
|
| 11 |
# |
|
| 12 |
# Todo list example: |
|
| 13 |
# |
|
| 14 |
# class TodoList < ActiveRecord::Base |
|
| 15 |
# has_many :todo_items, :order => "position" |
|
| 16 |
# end |
|
| 17 |
# |
|
| 18 |
# class TodoItem < ActiveRecord::Base |
|
| 19 |
# belongs_to :todo_list |
|
| 20 |
# acts_as_list :scope => :todo_list |
|
| 21 |
# end |
|
| 22 |
# |
|
| 23 |
# todo_list.first.move_to_bottom |
|
| 24 |
# todo_list.last.move_higher |
|
| 25 |
module ClassMethods |
|
| 26 |
# Configuration options are: |
|
| 27 |
# |
|
| 28 |
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+) |
|
| 29 |
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt> |
|
| 30 |
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible |
|
| 31 |
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. |
|
| 32 |
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
|
|
| 33 |
def acts_as_list(options = {})
|
|
| 34 |
configuration = { :column => "position", :scope => "1 = 1" }
|
|
| 35 |
configuration.update(options) if options.is_a?(Hash) |
|
| 36 |
|
|
| 37 |
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
|
|
| 38 |
|
|
| 39 |
if configuration[:scope].is_a?(Symbol) |
|
| 40 |
scope_condition_method = %( |
|
| 41 |
def scope_condition |
|
| 42 |
if #{configuration[:scope].to_s}.nil?
|
|
| 43 |
"#{configuration[:scope].to_s} IS NULL"
|
|
| 44 |
else |
|
| 45 |
"#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
|
|
| 46 |
end |
|
| 47 |
end |
|
| 48 |
) |
|
| 49 |
else |
|
| 50 |
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
|
|
| 51 |
end |
|
| 52 |
|
|
| 53 |
class_eval <<-EOV |
|
| 54 |
include ActiveRecord::Acts::List::InstanceMethods |
|
| 55 |
|
|
| 56 |
def acts_as_list_class |
|
| 57 |
::#{self.name}
|
|
| 58 |
end |
|
| 59 |
|
|
| 60 |
def position_column |
|
| 61 |
'#{configuration[:column]}'
|
|
| 62 |
end |
|
| 63 |
|
|
| 64 |
#{scope_condition_method}
|
|
| 65 |
|
|
| 66 |
before_destroy :remove_from_list |
|
| 67 |
before_create :add_to_list_bottom |
|
| 68 |
EOV |
|
| 69 |
end |
|
| 70 |
end |
|
| 71 |
|
|
| 72 |
# All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works |
|
| 73 |
# by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter |
|
| 74 |
# lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is |
|
| 75 |
# the first in the list of all chapters. |
|
| 76 |
module InstanceMethods |
|
| 77 |
# Insert the item at the given position (defaults to the top position of 1). |
|
| 78 |
def insert_at(position = 1) |
|
| 79 |
insert_at_position(position) |
|
| 80 |
end |
|
| 81 |
|
|
| 82 |
# Swap positions with the next lower item, if one exists. |
|
| 83 |
def move_lower |
|
| 84 |
return unless lower_item |
|
| 85 |
|
|
| 86 |
acts_as_list_class.transaction do |
|
| 87 |
lower_item.decrement_position |
|
| 88 |
increment_position |
|
| 89 |
end |
|
| 90 |
end |
|
| 91 |
|
|
| 92 |
# Swap positions with the next higher item, if one exists. |
|
| 93 |
def move_higher |
|
| 94 |
return unless higher_item |
|
| 95 |
|
|
| 96 |
acts_as_list_class.transaction do |
|
| 97 |
higher_item.increment_position |
|
| 98 |
decrement_position |
|
| 99 |
end |
|
| 100 |
end |
|
| 101 |
|
|
| 102 |
# Move to the bottom of the list. If the item is already in the list, the items below it have their |
|
| 103 |
# position adjusted accordingly. |
|
| 104 |
def move_to_bottom |
|
| 105 |
return unless in_list? |
|
| 106 |
acts_as_list_class.transaction do |
|
| 107 |
decrement_positions_on_lower_items |
|
| 108 |
assume_bottom_position |
|
| 109 |
end |
|
| 110 |
end |
|
| 111 |
|
|
| 112 |
# Move to the top of the list. If the item is already in the list, the items above it have their |
|
| 113 |
# position adjusted accordingly. |
|
| 114 |
def move_to_top |
|
| 115 |
return unless in_list? |
|
| 116 |
acts_as_list_class.transaction do |
|
| 117 |
increment_positions_on_higher_items |
|
| 118 |
assume_top_position |
|
| 119 |
end |
|
| 120 |
end |
|
| 121 |
|
|
| 122 |
# Move to the given position |
|
| 123 |
def move_to=(pos) |
|
| 124 |
case pos.to_s |
|
| 125 |
when 'highest' |
|
| 126 |
move_to_top |
|
| 127 |
when 'higher' |
|
| 128 |
move_higher |
|
| 129 |
when 'lower' |
|
| 130 |
move_lower |
|
| 131 |
when 'lowest' |
|
| 132 |
move_to_bottom |
|
| 133 |
end |
|
| 134 |
end |
|
| 135 |
|
|
| 136 |
# Removes the item from the list. |
|
| 137 |
def remove_from_list |
|
| 138 |
if in_list? |
|
| 139 |
decrement_positions_on_lower_items |
|
| 140 |
update_attribute position_column, nil |
|
| 141 |
end |
|
| 142 |
end |
|
| 143 |
|
|
| 144 |
# Increase the position of this item without adjusting the rest of the list. |
|
| 145 |
def increment_position |
|
| 146 |
return unless in_list? |
|
| 147 |
update_attribute position_column, self.send(position_column).to_i + 1 |
|
| 148 |
end |
|
| 149 |
|
|
| 150 |
# Decrease the position of this item without adjusting the rest of the list. |
|
| 151 |
def decrement_position |
|
| 152 |
return unless in_list? |
|
| 153 |
update_attribute position_column, self.send(position_column).to_i - 1 |
|
| 154 |
end |
|
| 155 |
|
|
| 156 |
# Return +true+ if this object is the first in the list. |
|
| 157 |
def first? |
|
| 158 |
return false unless in_list? |
|
| 159 |
self.send(position_column) == 1 |
|
| 160 |
end |
|
| 161 |
|
|
| 162 |
# Return +true+ if this object is the last in the list. |
|
| 163 |
def last? |
|
| 164 |
return false unless in_list? |
|
| 165 |
self.send(position_column) == bottom_position_in_list |
|
| 166 |
end |
|
| 167 |
|
|
| 168 |
# Return the next higher item in the list. |
|
| 169 |
def higher_item |
|
| 170 |
return nil unless in_list? |
|
| 171 |
acts_as_list_class.find(:first, :conditions => |
|
| 172 |
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
|
|
| 173 |
) |
|
| 174 |
end |
|
| 175 |
|
|
| 176 |
# Return the next lower item in the list. |
|
| 177 |
def lower_item |
|
| 178 |
return nil unless in_list? |
|
| 179 |
acts_as_list_class.find(:first, :conditions => |
|
| 180 |
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
|
|
| 181 |
) |
|
| 182 |
end |
|
| 183 |
|
|
| 184 |
# Test if this record is in a list |
|
| 185 |
def in_list? |
|
| 186 |
!send(position_column).nil? |
|
| 187 |
end |
|
| 188 |
|
|
| 189 |
private |
|
| 190 |
def add_to_list_top |
|
| 191 |
increment_positions_on_all_items |
|
| 192 |
end |
|
| 193 |
|
|
| 194 |
def add_to_list_bottom |
|
| 195 |
self[position_column] = bottom_position_in_list.to_i + 1 |
|
| 196 |
end |
|
| 197 |
|
|
| 198 |
# Overwrite this method to define the scope of the list changes |
|
| 199 |
def scope_condition() "1" end |
|
| 200 |
|
|
| 201 |
# Returns the bottom position number in the list. |
|
| 202 |
# bottom_position_in_list # => 2 |
|
| 203 |
def bottom_position_in_list(except = nil) |
|
| 204 |
item = bottom_item(except) |
|
| 205 |
item ? item.send(position_column) : 0 |
|
| 206 |
end |
|
| 207 |
|
|
| 208 |
# Returns the bottom item |
|
| 209 |
def bottom_item(except = nil) |
|
| 210 |
conditions = scope_condition |
|
| 211 |
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
|
|
| 212 |
acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
|
|
| 213 |
end |
|
| 214 |
|
|
| 215 |
# Forces item to assume the bottom position in the list. |
|
| 216 |
def assume_bottom_position |
|
| 217 |
update_attribute(position_column, bottom_position_in_list(self).to_i + 1) |
|
| 218 |
end |
|
| 219 |
|
|
| 220 |
# Forces item to assume the top position in the list. |
|
| 221 |
def assume_top_position |
|
| 222 |
update_attribute(position_column, 1) |
|
| 223 |
end |
|
| 224 |
|
|
| 225 |
# This has the effect of moving all the higher items up one. |
|
| 226 |
def decrement_positions_on_higher_items(position) |
|
| 227 |
acts_as_list_class.update_all( |
|
| 228 |
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}"
|
|
| 229 |
) |
|
| 230 |
end |
|
| 231 |
|
|
| 232 |
# This has the effect of moving all the lower items up one. |
|
| 233 |
def decrement_positions_on_lower_items |
|
| 234 |
return unless in_list? |
|
| 235 |
acts_as_list_class.update_all( |
|
| 236 |
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
|
|
| 237 |
) |
|
| 238 |
end |
|
| 239 |
|
|
| 240 |
# This has the effect of moving all the higher items down one. |
|
| 241 |
def increment_positions_on_higher_items |
|
| 242 |
return unless in_list? |
|
| 243 |
acts_as_list_class.update_all( |
|
| 244 |
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
|
|
| 245 |
) |
|
| 246 |
end |
|
| 247 |
|
|
| 248 |
# This has the effect of moving all the lower items down one. |
|
| 249 |
def increment_positions_on_lower_items(position) |
|
| 250 |
acts_as_list_class.update_all( |
|
| 251 |
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}"
|
|
| 252 |
) |
|
| 253 |
end |
|
| 254 |
|
|
| 255 |
# Increments position (<tt>position_column</tt>) of all items in the list. |
|
| 256 |
def increment_positions_on_all_items |
|
| 257 |
acts_as_list_class.update_all( |
|
| 258 |
"#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
|
|
| 259 |
) |
|
| 260 |
end |
|
| 261 |
|
|
| 262 |
def insert_at_position(position) |
|
| 263 |
remove_from_list |
|
| 264 |
increment_positions_on_lower_items(position) |
|
| 265 |
self.update_attribute(position_column, position) |
|
| 266 |
end |
|
| 267 |
end |
|
| 268 |
end |
|
| 269 |
end |
|
| 270 |
end |
|
| .svn/pristine/07/07ff3aff4af2ca5266644ab7e838820cda8c8752.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2013 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 14 |
# You should have received a copy of the GNU General Public License |
|
| 15 |
# along with this program; if not, write to the Free Software |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
require File.expand_path('../../../test_helper', __FILE__)
|
|
| 19 |
|
|
| 20 |
class RoutingAutoCompletesTest < ActionController::IntegrationTest |
|
| 21 |
def test_auto_completes |
|
| 22 |
assert_routing( |
|
| 23 |
{ :method => 'get', :path => "/issues/auto_complete" },
|
|
| 24 |
{ :controller => 'auto_completes', :action => 'issues' }
|
|
| 25 |
) |
|
| 26 |
end |
|
| 27 |
end |
|
Also available in: Unified diff