module ActiveRecord::Acts::Versioner::ActMethods::ClassMethods

Public Instance Methods

adapt_versioned_table() click to toggle source

If a column is added call this method to adapt the versioned table

# File lib/acts_as_versioner/acts_as_versioner.rb, line 266
def adapt_versioned_table
  not_versioned =  ["id", "action", versioned_foreign_key.to_s]
  versioned_columns = []
  self.connection.execute("show columns from #{versioned_table_name}").each { |col|
    versioned_columns << [col[0], col[1]] unless not_versioned.include?(col[0])
  }

  missing = []
  changed = []

  reset_columns = []
  self.connection.execute("show columns from #{table_name}").each { |col|
    reset_columns << [col[0], col[1]] unless not_versioned.include?(col[0])
  }

  reset_columns.each do |rc|
    found = versioned_columns.detect{ |wc| wc.first == rc.first }
    unless found.blank?
      changed << rc if rc.last.to_s != found.last.to_s
      versioned_columns.delete_if { |k| k.first == rc.first }
    else
      missing << rc
    end
  end
  
  # Add new column
  missing.each do |m|
    self.connection.add_column versioned_table_name, m.first, m.last
  end

  # Change column
  changed.each do |c|
    self.connection.change_column versioned_table_name, c.first, c.last
  end
  
  # Remove column
  versioned_columns.each do |vc|
    self.connection.remove_column versioned_table_name, vc.first
  end
end
add_column_to_table(table, column, type) click to toggle source
# File lib/acts_as_versioner/acts_as_versioner.rb, line 248
def add_column_to_table(table, column, type)
  tabelle = self.connection.execute("show columns from #{table} like '#{column}'")
  
  do_add = true
  for res in tabelle
    do_add = false if column.to_s == res.first.to_s
  end
  if do_add
    self.connection.add_column table, column, type
  end
end
create_versioned_table(create_table_options = {}) click to toggle source

Rake migration task to create the versioned table

# File lib/acts_as_versioner/acts_as_versioner.rb, line 220
def create_versioned_table(create_table_options = {})
  versioned_table_name = "#{self.to_s.underscore}#{ActiveRecord::Acts::Versioner::configurator[:default_versioned_table_name]}"
  puts table_name
  # create version column in main table if it does not exist
  add_column_to_table(table_name, ActiveRecord::Acts::Versioner::configurator[:default_versioned_created_at], :datetime)
  add_column_to_table(table_name, ActiveRecord::Acts::Versioner::configurator[:default_versioned_updated_at], :datetime)
  add_column_to_table(table_name, ActiveRecord::Acts::Versioner::configurator[:default_versioned_created_by], :integer)
  add_column_to_table(table_name, ActiveRecord::Acts::Versioner::configurator[:default_versioned_updated_by], :integer)


  # create versions table
  self.connection.create_table(versioned_table_name, create_table_options) do |t|
          t.column versioned_foreign_key, :integer
          t.column :action, :integer, :null => false, :default => 0
  end

  # clone the original table in order to create the versions table
  puts versioned_table_name
  not_versioned =  %w{id}
  self.versioned_columns.each do |col|
    unless not_versioned.include?(col.name)
          self.connection.add_column versioned_table_name, col.name, col.type,
                  :limit => col.limit,
                  :default => col.default
    end
  end
end
drop_versioned_table() click to toggle source

Rake migration task to drop the versioned table

# File lib/acts_as_versioner/acts_as_versioner.rb, line 261
def drop_versioned_table
  self.connection.drop_table versioned_table_name
end
resurrect(id) click to toggle source

You can resurrect a destroyed entry by its versioned foreign key

# File lib/acts_as_versioner/acts_as_versioner.rb, line 308
def resurrect(id)
   destroyed_version = self.versioned_class.where(self.versioned_foreign_key => id).last
   if destroyed_version && destroyed_version.action == 2
     model = self.new
     self.columns.map{|c| c.name}.each do |c|
       model[c] = destroyed_version[c] unless c == "id"
       model[c] = id if c == "id"
       model[c] = Time.now if c == "updated_at"
     end
   model.save
   return model if model.errors.blank?
   end
end
versioned_class() click to toggle source

Returns an instance of the dynamic versioned model

# File lib/acts_as_versioner/acts_as_versioner.rb, line 215
def versioned_class
  const_get versioned_class_name
end
versioned_columns() click to toggle source

Returns an array of columns that are versioned. See non_versioned_columns

# File lib/acts_as_versioner/acts_as_versioner.rb, line 210
def versioned_columns
  self.columns.select { |c| c.name }
end