Module: ActiveRecord::Acts::Versioned::ActMethods::ClassMethods

Defined in:
vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb

Instance Method Summary

Instance Method Details

- (Object) create_versioned_table(create_table_options = {})

Rake migration task to create the versioned table using options passed to acts_as_versioned



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 484

def create_versioned_table(create_table_options = {})
  # create version column in main table if it does not exist
  if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
    self.connection.add_column table_name, :version, :integer
  end

  self.connection.create_table(versioned_table_name, create_table_options) do |t|
    t.column versioned_foreign_key, :integer
    t.column :version, :integer
  end

  updated_col = nil
  self.versioned_columns.each do |col| 
    updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
    self.connection.add_column versioned_table_name, col.name, col.type, 
      :limit => col.limit, 
      :default => col.default,
      :scale => col.scale,
      :precision => col.precision
  end

  if type_col = self.columns_hash[inheritance_column]
    self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type, 
      :limit => type_col.limit, 
      :default => type_col.default,
      :scale => type_col.scale,
      :precision => type_col.precision
  end

  if updated_col.nil?
    self.connection.add_column versioned_table_name, :updated_at, :timestamp
  end
end

- (Object) drop_versioned_table

Rake migration task to drop the versioned table



519
520
521
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 519

def drop_versioned_table
  self.connection.drop_table versioned_table_name
end

- (Object) find_version(id, version = nil)

Finds a specific version of a specific row of this model



453
454
455
456
457
458
459
460
461
462
463
464
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 453

def find_version(id, version = nil)
  return find(id) unless version

  conditions = ["#{versioned_foreign_key} = ? AND version = ?", id, version]
  options = { :conditions => conditions, :limit => 1 }

  if result = find_versions(id, options).first
    result
  else
    raise RecordNotFound, "Couldn't find #{name} with ID=#{id} and VERSION=#{version}"
  end
end

- (Object) find_versions(id, options = {})

Finds versions of a specific model. Takes an options hash like find



467
468
469
470
471
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 467

def find_versions(id, options = {})
  versioned_class.find :all, {
    :conditions => ["#{versioned_foreign_key} = ?", id],
    :order      => 'version' }.merge(options)
end

- (Object) versioned_class

Returns an instance of the dynamic versioned model



479
480
481
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 479

def versioned_class
  const_get versioned_class_name
end

- (Object) versioned_columns

Returns an array of columns that are versioned. See non_versioned_columns



474
475
476
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 474

def versioned_columns
  self.columns.select { |c| !non_versioned_columns.include?(c.name) }
end

- (Object) without_locking(&block)

Turns off optimistic locking for the duration of the block

  Foo.without_locking do
    @foo.save
  end


551
552
553
554
555
556
557
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 551

def without_locking(&block)
  current = ActiveRecord::Base.lock_optimistically
  ActiveRecord::Base.lock_optimistically = false if current
  result = block.call
  ActiveRecord::Base.lock_optimistically = true if current
  result
end

- (Object) without_revision(&block)

Executes the block with the versioning callbacks disabled.

  Foo.without_revision do
    @foo.save
  end


529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 529

def without_revision(&block)
  class_eval do 
    CALLBACKS.each do |attr_name|
      alias_method "orig_#{attr_name}".to_sym, attr_name
      alias_method attr_name, :empty_callback
    end
  end
  block.call
ensure
  class_eval do 
    CALLBACKS.each do |attr_name|
      alias_method attr_name, "orig_#{attr_name}".to_sym
    end
  end
end