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

Defined in:
lib/acts_as_versioned.rb

Instance Method Summary collapse

Instance Method Details

#create_versioned_table(create_table_options = {}) ⇒ Object

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



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/acts_as_versioned.rb', line 351

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 and %(updated_at updated_on).include?(col.name)
    self.connection.add_column versioned_table_name, col.name, col.type, 
      :limit => col.limit, 
      :default => col.default
  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
  end
    
  if updated_col.nil?
    self.connection.add_column versioned_table_name, :updated_at, :timestamp
  end
end

#drop_versioned_tableObject

Rake migration task to drop the versioned table



382
383
384
# File 'lib/acts_as_versioned.rb', line 382

def drop_versioned_table
  self.connection.drop_table versioned_table_name
end

#find_version(id, version) ⇒ Object

Finds a specific version of a specific row of this model



322
323
324
325
326
# File 'lib/acts_as_versioned.rb', line 322

def find_version(id, version)
  find_versions(id, 
    :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version], 
    :limit => 1).first
end

#find_versions(id, options = {}) ⇒ Object

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



329
330
331
332
333
# File 'lib/acts_as_versioned.rb', line 329

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

#non_versioned_fieldsObject

An array of fields that are not saved in the versioned table



346
347
348
# File 'lib/acts_as_versioned.rb', line 346

def non_versioned_fields
  [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
end

#versioned_classObject

Returns an instance of the dynamic versioned model



341
342
343
# File 'lib/acts_as_versioned.rb', line 341

def versioned_class
  "ActiveRecord::Acts::Versioned::#{versioned_class_name}".constantize
end

#versioned_columnsObject

Returns an array of columns that are versioned. See non_versioned_fields



336
337
338
# File 'lib/acts_as_versioned.rb', line 336

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