annotate .svn/pristine/11/1126b71d1361e2719f05011613589ea2d0df072c.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Copyright (c) 2005 Rick Olson
Chris@909 2 #
Chris@909 3 # Permission is hereby granted, free of charge, to any person obtaining
Chris@909 4 # a copy of this software and associated documentation files (the
Chris@909 5 # "Software"), to deal in the Software without restriction, including
Chris@909 6 # without limitation the rights to use, copy, modify, merge, publish,
Chris@909 7 # distribute, sublicense, and/or sell copies of the Software, and to
Chris@909 8 # permit persons to whom the Software is furnished to do so, subject to
Chris@909 9 # the following conditions:
Chris@909 10 #
Chris@909 11 # The above copyright notice and this permission notice shall be
Chris@909 12 # included in all copies or substantial portions of the Software.
Chris@909 13 #
Chris@909 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@909 15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@909 16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@909 17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Chris@909 18 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Chris@909 19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@909 20 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@909 21
Chris@909 22 module ActiveRecord #:nodoc:
Chris@909 23 module Acts #:nodoc:
Chris@909 24 # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
Chris@909 25 # versioned table ready and that your model has a version field. This works with optimistic locking if the lock_version
Chris@909 26 # column is present as well.
Chris@909 27 #
Chris@909 28 # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart
Chris@909 29 # your container for the changes to be reflected. In development mode this usually means restarting WEBrick.
Chris@909 30 #
Chris@909 31 # class Page < ActiveRecord::Base
Chris@909 32 # # assumes pages_versions table
Chris@909 33 # acts_as_versioned
Chris@909 34 # end
Chris@909 35 #
Chris@909 36 # Example:
Chris@909 37 #
Chris@909 38 # page = Page.create(:title => 'hello world!')
Chris@909 39 # page.version # => 1
Chris@909 40 #
Chris@909 41 # page.title = 'hello world'
Chris@909 42 # page.save
Chris@909 43 # page.version # => 2
Chris@909 44 # page.versions.size # => 2
Chris@909 45 #
Chris@909 46 # page.revert_to(1) # using version number
Chris@909 47 # page.title # => 'hello world!'
Chris@909 48 #
Chris@909 49 # page.revert_to(page.versions.last) # using versioned instance
Chris@909 50 # page.title # => 'hello world'
Chris@909 51 #
Chris@909 52 # page.versions.earliest # efficient query to find the first version
Chris@909 53 # page.versions.latest # efficient query to find the most recently created version
Chris@909 54 #
Chris@909 55 #
Chris@909 56 # Simple Queries to page between versions
Chris@909 57 #
Chris@909 58 # page.versions.before(version)
Chris@909 59 # page.versions.after(version)
Chris@909 60 #
Chris@909 61 # Access the previous/next versions from the versioned model itself
Chris@909 62 #
Chris@909 63 # version = page.versions.latest
Chris@909 64 # version.previous # go back one version
Chris@909 65 # version.next # go forward one version
Chris@909 66 #
Chris@909 67 # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
Chris@909 68 module Versioned
Chris@909 69 CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?, :clear_altered_attributes]
Chris@909 70 def self.included(base) # :nodoc:
Chris@909 71 base.extend ClassMethods
Chris@909 72 end
Chris@909 73
Chris@909 74 module ClassMethods
Chris@909 75 # == Configuration options
Chris@909 76 #
Chris@909 77 # * <tt>class_name</tt> - versioned model class name (default: PageVersion in the above example)
Chris@909 78 # * <tt>table_name</tt> - versioned model table name (default: page_versions in the above example)
Chris@909 79 # * <tt>foreign_key</tt> - foreign key used to relate the versioned model to the original model (default: page_id in the above example)
Chris@909 80 # * <tt>inheritance_column</tt> - name of the column to save the model's inheritance_column value for STI. (default: versioned_type)
Chris@909 81 # * <tt>version_column</tt> - name of the column in the model that keeps the version number (default: version)
Chris@909 82 # * <tt>sequence_name</tt> - name of the custom sequence to be used by the versioned model.
Chris@909 83 # * <tt>limit</tt> - number of revisions to keep, defaults to unlimited
Chris@909 84 # * <tt>if</tt> - symbol of method to check before saving a new version. If this method returns false, a new version is not saved.
Chris@909 85 # For finer control, pass either a Proc or modify Model#version_condition_met?
Chris@909 86 #
Chris@909 87 # acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
Chris@909 88 #
Chris@909 89 # or...
Chris@909 90 #
Chris@909 91 # class Auction
Chris@909 92 # def version_condition_met? # totally bypasses the <tt>:if</tt> option
Chris@909 93 # !expired?
Chris@909 94 # end
Chris@909 95 # end
Chris@909 96 #
Chris@909 97 # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
Chris@909 98 # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
Chris@909 99 # Use this instead if you want to write your own attribute setters (and ignore if_changed):
Chris@909 100 #
Chris@909 101 # def name=(new_name)
Chris@909 102 # write_changed_attribute :name, new_name
Chris@909 103 # end
Chris@909 104 #
Chris@909 105 # * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block
Chris@909 106 # to create an anonymous mixin:
Chris@909 107 #
Chris@909 108 # class Auction
Chris@909 109 # acts_as_versioned do
Chris@909 110 # def started?
Chris@909 111 # !started_at.nil?
Chris@909 112 # end
Chris@909 113 # end
Chris@909 114 # end
Chris@909 115 #
Chris@909 116 # or...
Chris@909 117 #
Chris@909 118 # module AuctionExtension
Chris@909 119 # def started?
Chris@909 120 # !started_at.nil?
Chris@909 121 # end
Chris@909 122 # end
Chris@909 123 # class Auction
Chris@909 124 # acts_as_versioned :extend => AuctionExtension
Chris@909 125 # end
Chris@909 126 #
Chris@909 127 # Example code:
Chris@909 128 #
Chris@909 129 # @auction = Auction.find(1)
Chris@909 130 # @auction.started?
Chris@909 131 # @auction.versions.first.started?
Chris@909 132 #
Chris@909 133 # == Database Schema
Chris@909 134 #
Chris@909 135 # The model that you're versioning needs to have a 'version' attribute. The model is versioned
Chris@909 136 # into a table called #{model}_versions where the model name is singlular. The _versions table should
Chris@909 137 # contain all the fields you want versioned, the same version column, and a #{model}_id foreign key field.
Chris@909 138 #
Chris@909 139 # A lock_version field is also accepted if your model uses Optimistic Locking. If your table uses Single Table inheritance,
Chris@909 140 # then that field is reflected in the versioned model as 'versioned_type' by default.
Chris@909 141 #
Chris@909 142 # Acts_as_versioned comes prepared with the ActiveRecord::Acts::Versioned::ActMethods::ClassMethods#create_versioned_table
Chris@909 143 # method, perfect for a migration. It will also create the version column if the main model does not already have it.
Chris@909 144 #
Chris@909 145 # class AddVersions < ActiveRecord::Migration
Chris@909 146 # def self.up
Chris@909 147 # # create_versioned_table takes the same options hash
Chris@909 148 # # that create_table does
Chris@909 149 # Post.create_versioned_table
Chris@909 150 # end
Chris@909 151 #
Chris@909 152 # def self.down
Chris@909 153 # Post.drop_versioned_table
Chris@909 154 # end
Chris@909 155 # end
Chris@909 156 #
Chris@909 157 # == Changing What Fields Are Versioned
Chris@909 158 #
Chris@909 159 # By default, acts_as_versioned will version all but these fields:
Chris@909 160 #
Chris@909 161 # [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
Chris@909 162 #
Chris@909 163 # You can add or change those by modifying #non_versioned_columns. Note that this takes strings and not symbols.
Chris@909 164 #
Chris@909 165 # class Post < ActiveRecord::Base
Chris@909 166 # acts_as_versioned
Chris@909 167 # self.non_versioned_columns << 'comments_count'
Chris@909 168 # end
Chris@909 169 #
Chris@909 170 def acts_as_versioned(options = {}, &extension)
Chris@909 171 # don't allow multiple calls
Chris@909 172 return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
Chris@909 173
Chris@909 174 send :include, ActiveRecord::Acts::Versioned::ActMethods
Chris@909 175
Chris@909 176 cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
Chris@909 177 :version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
Chris@909 178 :version_association_options
Chris@909 179
Chris@909 180 # legacy
Chris@909 181 alias_method :non_versioned_fields, :non_versioned_columns
Chris@909 182 alias_method :non_versioned_fields=, :non_versioned_columns=
Chris@909 183
Chris@909 184 class << self
Chris@909 185 alias_method :non_versioned_fields, :non_versioned_columns
Chris@909 186 alias_method :non_versioned_fields=, :non_versioned_columns=
Chris@909 187 end
Chris@909 188
Chris@909 189 send :attr_accessor, :altered_attributes
Chris@909 190
Chris@909 191 self.versioned_class_name = options[:class_name] || "Version"
Chris@909 192 self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
Chris@909 193 self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
Chris@909 194 self.versioned_inheritance_column = options[:inheritance_column] || "versioned_#{inheritance_column}"
Chris@909 195 self.version_column = options[:version_column] || 'version'
Chris@909 196 self.version_sequence_name = options[:sequence_name]
Chris@909 197 self.max_version_limit = options[:limit].to_i
Chris@909 198 self.version_condition = options[:if] || true
Chris@909 199 self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
Chris@909 200 self.version_association_options = {
Chris@909 201 :class_name => "#{self.to_s}::#{versioned_class_name}",
Chris@909 202 :foreign_key => versioned_foreign_key,
Chris@909 203 :dependent => :delete_all
Chris@909 204 }.merge(options[:association_options] || {})
Chris@909 205
Chris@909 206 if block_given?
Chris@909 207 extension_module_name = "#{versioned_class_name}Extension"
Chris@909 208 silence_warnings do
Chris@909 209 self.const_set(extension_module_name, Module.new(&extension))
Chris@909 210 end
Chris@909 211
Chris@909 212 options[:extend] = self.const_get(extension_module_name)
Chris@909 213 end
Chris@909 214
Chris@909 215 class_eval do
Chris@909 216 has_many :versions, version_association_options do
Chris@909 217 # finds earliest version of this record
Chris@909 218 def earliest
Chris@909 219 @earliest ||= find(:first, :order => 'version')
Chris@909 220 end
Chris@909 221
Chris@909 222 # find latest version of this record
Chris@909 223 def latest
Chris@909 224 @latest ||= find(:first, :order => 'version desc')
Chris@909 225 end
Chris@909 226 end
Chris@909 227 before_save :set_new_version
Chris@909 228 after_create :save_version_on_create
Chris@909 229 after_update :save_version
Chris@909 230 after_save :clear_old_versions
Chris@909 231 after_save :clear_altered_attributes
Chris@909 232
Chris@909 233 unless options[:if_changed].nil?
Chris@909 234 self.track_altered_attributes = true
Chris@909 235 options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
Chris@909 236 options[:if_changed].each do |attr_name|
Chris@909 237 define_method("#{attr_name}=") do |value|
Chris@909 238 write_changed_attribute attr_name, value
Chris@909 239 end
Chris@909 240 end
Chris@909 241 end
Chris@909 242
Chris@909 243 include options[:extend] if options[:extend].is_a?(Module)
Chris@909 244 end
Chris@909 245
Chris@909 246 # create the dynamic versioned model
Chris@909 247 const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
Chris@909 248 def self.reloadable? ; false ; end
Chris@909 249 # find first version before the given version
Chris@909 250 def self.before(version)
Chris@909 251 find :first, :order => 'version desc',
Chris@909 252 :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
Chris@909 253 end
Chris@909 254
Chris@909 255 # find first version after the given version.
Chris@909 256 def self.after(version)
Chris@909 257 find :first, :order => 'version',
Chris@909 258 :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
Chris@909 259 end
Chris@909 260
Chris@909 261 def previous
Chris@909 262 self.class.before(self)
Chris@909 263 end
Chris@909 264
Chris@909 265 def next
Chris@909 266 self.class.after(self)
Chris@909 267 end
Chris@909 268
Chris@909 269 def versions_count
Chris@909 270 page.version
Chris@909 271 end
Chris@909 272 end
Chris@909 273
Chris@909 274 versioned_class.cattr_accessor :original_class
Chris@909 275 versioned_class.original_class = self
Chris@909 276 versioned_class.set_table_name versioned_table_name
Chris@909 277 versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
Chris@909 278 :class_name => "::#{self.to_s}",
Chris@909 279 :foreign_key => versioned_foreign_key
Chris@909 280 versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module)
Chris@909 281 versioned_class.set_sequence_name version_sequence_name if version_sequence_name
Chris@909 282 end
Chris@909 283 end
Chris@909 284
Chris@909 285 module ActMethods
Chris@909 286 def self.included(base) # :nodoc:
Chris@909 287 base.extend ClassMethods
Chris@909 288 end
Chris@909 289
Chris@909 290 # Finds a specific version of this record
Chris@909 291 def find_version(version = nil)
Chris@909 292 self.class.find_version(id, version)
Chris@909 293 end
Chris@909 294
Chris@909 295 # Saves a version of the model if applicable
Chris@909 296 def save_version
Chris@909 297 save_version_on_create if save_version?
Chris@909 298 end
Chris@909 299
Chris@909 300 # Saves a version of the model in the versioned table. This is called in the after_save callback by default
Chris@909 301 def save_version_on_create
Chris@909 302 rev = self.class.versioned_class.new
Chris@909 303 self.clone_versioned_model(self, rev)
Chris@909 304 rev.version = send(self.class.version_column)
Chris@909 305 rev.send("#{self.class.versioned_foreign_key}=", self.id)
Chris@909 306 rev.save
Chris@909 307 end
Chris@909 308
Chris@909 309 # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
Chris@909 310 # Override this method to set your own criteria for clearing old versions.
Chris@909 311 def clear_old_versions
Chris@909 312 return if self.class.max_version_limit == 0
Chris@909 313 excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
Chris@909 314 if excess_baggage > 0
Chris@909 315 sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
Chris@909 316 self.class.versioned_class.connection.execute sql
Chris@909 317 end
Chris@909 318 end
Chris@909 319
Chris@909 320 def versions_count
Chris@909 321 version
Chris@909 322 end
Chris@909 323
Chris@909 324 # Reverts a model to a given version. Takes either a version number or an instance of the versioned model
Chris@909 325 def revert_to(version)
Chris@909 326 if version.is_a?(self.class.versioned_class)
Chris@909 327 return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
Chris@909 328 else
Chris@909 329 return false unless version = versions.find_by_version(version)
Chris@909 330 end
Chris@909 331 self.clone_versioned_model(version, self)
Chris@909 332 self.send("#{self.class.version_column}=", version.version)
Chris@909 333 true
Chris@909 334 end
Chris@909 335
Chris@909 336 # Reverts a model to a given version and saves the model.
Chris@909 337 # Takes either a version number or an instance of the versioned model
Chris@909 338 def revert_to!(version)
Chris@909 339 revert_to(version) ? save_without_revision : false
Chris@909 340 end
Chris@909 341
Chris@909 342 # Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
Chris@909 343 def save_without_revision
Chris@909 344 save_without_revision!
Chris@909 345 true
Chris@909 346 rescue
Chris@909 347 false
Chris@909 348 end
Chris@909 349
Chris@909 350 def save_without_revision!
Chris@909 351 without_locking do
Chris@909 352 without_revision do
Chris@909 353 save!
Chris@909 354 end
Chris@909 355 end
Chris@909 356 end
Chris@909 357
Chris@909 358 # Returns an array of attribute keys that are versioned. See non_versioned_columns
Chris@909 359 def versioned_attributes
Chris@909 360 self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
Chris@909 361 end
Chris@909 362
Chris@909 363 # If called with no parameters, gets whether the current model has changed and needs to be versioned.
Chris@909 364 # If called with a single parameter, gets whether the parameter has changed.
Chris@909 365 def changed?(attr_name = nil)
Chris@909 366 attr_name.nil? ?
Chris@909 367 (!self.class.track_altered_attributes || (altered_attributes && altered_attributes.length > 0)) :
Chris@909 368 (altered_attributes && altered_attributes.include?(attr_name.to_s))
Chris@909 369 end
Chris@909 370
Chris@909 371 # keep old dirty? method
Chris@909 372 alias_method :dirty?, :changed?
Chris@909 373
Chris@909 374 # Clones a model. Used when saving a new version or reverting a model's version.
Chris@909 375 def clone_versioned_model(orig_model, new_model)
Chris@909 376 self.versioned_attributes.each do |key|
Chris@909 377 new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key)
Chris@909 378 end
Chris@909 379
Chris@909 380 if orig_model.is_a?(self.class.versioned_class)
Chris@909 381 new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
Chris@909 382 elsif new_model.is_a?(self.class.versioned_class)
Chris@909 383 new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
Chris@909 384 end
Chris@909 385 end
Chris@909 386
Chris@909 387 # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
Chris@909 388 def save_version?
Chris@909 389 version_condition_met? && changed?
Chris@909 390 end
Chris@909 391
Chris@909 392 # Checks condition set in the :if option to check whether a revision should be created or not. Override this for
Chris@909 393 # custom version condition checking.
Chris@909 394 def version_condition_met?
Chris@909 395 case
Chris@909 396 when version_condition.is_a?(Symbol)
Chris@909 397 send(version_condition)
Chris@909 398 when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
Chris@909 399 version_condition.call(self)
Chris@909 400 else
Chris@909 401 version_condition
Chris@909 402 end
Chris@909 403 end
Chris@909 404
Chris@909 405 # Executes the block with the versioning callbacks disabled.
Chris@909 406 #
Chris@909 407 # @foo.without_revision do
Chris@909 408 # @foo.save
Chris@909 409 # end
Chris@909 410 #
Chris@909 411 def without_revision(&block)
Chris@909 412 self.class.without_revision(&block)
Chris@909 413 end
Chris@909 414
Chris@909 415 # Turns off optimistic locking for the duration of the block
Chris@909 416 #
Chris@909 417 # @foo.without_locking do
Chris@909 418 # @foo.save
Chris@909 419 # end
Chris@909 420 #
Chris@909 421 def without_locking(&block)
Chris@909 422 self.class.without_locking(&block)
Chris@909 423 end
Chris@909 424
Chris@909 425 def empty_callback() end #:nodoc:
Chris@909 426
Chris@909 427 protected
Chris@909 428 # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
Chris@909 429 def set_new_version
Chris@909 430 self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?)
Chris@909 431 end
Chris@909 432
Chris@909 433 # Gets the next available version for the current record, or 1 for a new record
Chris@909 434 def next_version
Chris@909 435 return 1 if new_record?
Chris@909 436 (versions.calculate(:max, :version) || 0) + 1
Chris@909 437 end
Chris@909 438
Chris@909 439 # clears current changed attributes. Called after save.
Chris@909 440 def clear_altered_attributes
Chris@909 441 self.altered_attributes = []
Chris@909 442 end
Chris@909 443
Chris@909 444 def write_changed_attribute(attr_name, attr_value)
Chris@909 445 # Convert to db type for comparison. Avoids failing Float<=>String comparisons.
Chris@909 446 attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value)
Chris@909 447 (self.altered_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db
Chris@909 448 write_attribute(attr_name, attr_value_for_db)
Chris@909 449 end
Chris@909 450
Chris@909 451 module ClassMethods
Chris@909 452 # Finds a specific version of a specific row of this model
Chris@909 453 def find_version(id, version = nil)
Chris@909 454 return find(id) unless version
Chris@909 455
Chris@909 456 conditions = ["#{versioned_foreign_key} = ? AND version = ?", id, version]
Chris@909 457 options = { :conditions => conditions, :limit => 1 }
Chris@909 458
Chris@909 459 if result = find_versions(id, options).first
Chris@909 460 result
Chris@909 461 else
Chris@909 462 raise RecordNotFound, "Couldn't find #{name} with ID=#{id} and VERSION=#{version}"
Chris@909 463 end
Chris@909 464 end
Chris@909 465
Chris@909 466 # Finds versions of a specific model. Takes an options hash like <tt>find</tt>
Chris@909 467 def find_versions(id, options = {})
Chris@909 468 versioned_class.find :all, {
Chris@909 469 :conditions => ["#{versioned_foreign_key} = ?", id],
Chris@909 470 :order => 'version' }.merge(options)
Chris@909 471 end
Chris@909 472
Chris@909 473 # Returns an array of columns that are versioned. See non_versioned_columns
Chris@909 474 def versioned_columns
Chris@909 475 self.columns.select { |c| !non_versioned_columns.include?(c.name) }
Chris@909 476 end
Chris@909 477
Chris@909 478 # Returns an instance of the dynamic versioned model
Chris@909 479 def versioned_class
Chris@909 480 const_get versioned_class_name
Chris@909 481 end
Chris@909 482
Chris@909 483 # Rake migration task to create the versioned table using options passed to acts_as_versioned
Chris@909 484 def create_versioned_table(create_table_options = {})
Chris@909 485 # create version column in main table if it does not exist
Chris@909 486 if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
Chris@909 487 self.connection.add_column table_name, :version, :integer
Chris@909 488 end
Chris@909 489
Chris@909 490 self.connection.create_table(versioned_table_name, create_table_options) do |t|
Chris@909 491 t.column versioned_foreign_key, :integer
Chris@909 492 t.column :version, :integer
Chris@909 493 end
Chris@909 494
Chris@909 495 updated_col = nil
Chris@909 496 self.versioned_columns.each do |col|
Chris@909 497 updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
Chris@909 498 self.connection.add_column versioned_table_name, col.name, col.type,
Chris@909 499 :limit => col.limit,
Chris@909 500 :default => col.default,
Chris@909 501 :scale => col.scale,
Chris@909 502 :precision => col.precision
Chris@909 503 end
Chris@909 504
Chris@909 505 if type_col = self.columns_hash[inheritance_column]
Chris@909 506 self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
Chris@909 507 :limit => type_col.limit,
Chris@909 508 :default => type_col.default,
Chris@909 509 :scale => type_col.scale,
Chris@909 510 :precision => type_col.precision
Chris@909 511 end
Chris@909 512
Chris@909 513 if updated_col.nil?
Chris@909 514 self.connection.add_column versioned_table_name, :updated_at, :timestamp
Chris@909 515 end
Chris@909 516 end
Chris@909 517
Chris@909 518 # Rake migration task to drop the versioned table
Chris@909 519 def drop_versioned_table
Chris@909 520 self.connection.drop_table versioned_table_name
Chris@909 521 end
Chris@909 522
Chris@909 523 # Executes the block with the versioning callbacks disabled.
Chris@909 524 #
Chris@909 525 # Foo.without_revision do
Chris@909 526 # @foo.save
Chris@909 527 # end
Chris@909 528 #
Chris@909 529 def without_revision(&block)
Chris@909 530 class_eval do
Chris@909 531 CALLBACKS.each do |attr_name|
Chris@909 532 alias_method "orig_#{attr_name}".to_sym, attr_name
Chris@909 533 alias_method attr_name, :empty_callback
Chris@909 534 end
Chris@909 535 end
Chris@909 536 block.call
Chris@909 537 ensure
Chris@909 538 class_eval do
Chris@909 539 CALLBACKS.each do |attr_name|
Chris@909 540 alias_method attr_name, "orig_#{attr_name}".to_sym
Chris@909 541 end
Chris@909 542 end
Chris@909 543 end
Chris@909 544
Chris@909 545 # Turns off optimistic locking for the duration of the block
Chris@909 546 #
Chris@909 547 # Foo.without_locking do
Chris@909 548 # @foo.save
Chris@909 549 # end
Chris@909 550 #
Chris@909 551 def without_locking(&block)
Chris@909 552 current = ActiveRecord::Base.lock_optimistically
Chris@909 553 ActiveRecord::Base.lock_optimistically = false if current
Chris@909 554 result = block.call
Chris@909 555 ActiveRecord::Base.lock_optimistically = true if current
Chris@909 556 result
Chris@909 557 end
Chris@909 558 end
Chris@909 559 end
Chris@909 560 end
Chris@909 561 end
Chris@909 562 end
Chris@909 563
Chris@909 564 ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned