annotate .svn/pristine/b8/b8657f7d2e5a1ade105ba1c1a91e64ff816547b4.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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