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

Defined in:
lib/rtiss_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



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/rtiss_acts_as_versioned.rb', line 532

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

  return if connection.table_exists?(versioned_table_name)
  
  self.connection.create_table(versioned_table_name, create_table_options) do |t|
    t.column versioned_foreign_key, :integer
    t.column version_column, :integer
    t.column deleted_in_original_table_flag, :boolean, :default => false
    t.column record_restored_column, :integer, :default => nil
  end

  self.versioned_columns.each do |col| 
    self.connection.add_column versioned_table_name, col.name, col.type, 
      :limit     => col.limit, 
      :default   => col.cast_type.type_cast_from_database(col.default),  # convert strings to ruby types
      :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.cast_type.type_cast_from_database(type_col.default),  # convert strings to ruby types
      :scale     => type_col.scale,
      :precision => type_col.precision
  end
  
  #self.connection.add_index versioned_table_name, versioned_foreign_key
end

#drop_versioned_tableObject

Rake migration task to drop the versioned table



568
569
570
# File 'lib/rtiss_acts_as_versioned.rb', line 568

def drop_versioned_table
  self.connection.drop_table versioned_table_name
end

#restore_deleted(id) ⇒ Object



572
573
574
575
# File 'lib/rtiss_acts_as_versioned.rb', line 572

def restore_deleted(id)
  version_record = versioned_class.where("#{versioned_foreign_key} = #{id}").order("version DESC").first
  version_record.restore
end

#restore_deleted_version(id, version) ⇒ Object



577
578
579
580
# File 'lib/rtiss_acts_as_versioned.rb', line 577

def restore_deleted_version(id, version)
  version_record = versioned_class.where("#{versioned_foreign_key} = #{id} and version = #{version}").first
  version_record.restore
end

#versioned_classObject

Returns an instance of the dynamic versioned model



527
528
529
# File 'lib/rtiss_acts_as_versioned.rb', line 527

def versioned_class
  const_get versioned_class_name
end

#versioned_columnsObject

Returns an array of columns that are versioned. See non_versioned_columns



522
523
524
# File 'lib/rtiss_acts_as_versioned.rb', line 522

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

#without_locking(&block) ⇒ Object

Turns off optimistic locking for the duration of the block

Foo.without_locking do
  @foo.save
end


610
611
612
613
614
615
616
617
618
# File 'lib/rtiss_acts_as_versioned.rb', line 610

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

#without_revision(&block) ⇒ Object

Executes the block with the versioning callbacks disabled.

Foo.without_revision do
  @foo.save
end


588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/rtiss_acts_as_versioned.rb', line 588

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