Module: SoftwareHeretics::ActiveRecord::SimplyVersioned::InstanceMethods

Defined in:
lib/simply_versioned.rb

Overview

Methods that will be defined on the ActiveRecord model being versioned

Instance Method Summary collapse

Instance Method Details

#revert_to_version(version, options = {}) ⇒ Object

Revert the attributes of this model to their values as of an earlier version.

Pass either a Version instance or a version number.

options: except specify a list of attributes that are not restored (default: created_at, updated_at)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/simply_versioned.rb', line 89

def revert_to_version( version, options = {} )
  options.reverse_merge!({
    :except => [:created_at,:updated_at]
  })
  
  version = if version.kind_of?( Version )
    version
  elsif version.kind_of?( Fixnum )
    self.versions.find_by_number( version )
  end
  
  raise "Invalid version (#{version.inspect}) specified!" unless version
  
  options[:except] = options[:except].map( &:to_s )
  
  self.update_attributes( YAML::load( version.yaml ).except( *options[:except] ) )
end

#unversioned?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/simply_versioned.rb', line 120

def unversioned?
  self.versions.nil? || self.versions.size == 0
end

#version_numberObject



128
129
130
131
132
133
134
# File 'lib/simply_versioned.rb', line 128

def version_number
  if self.versions.empty?
    0
  else
    self.versions.current.number
  end
end

#versioned?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/simply_versioned.rb', line 124

def versioned?
  !unversioned?
end

#with_versioning(enabled, &block) ⇒ Object

Invoke the supplied block passing the receiver as the sole block argument with versioning enabled or disabled depending upon the value of the enabled parameter for the duration of the block.



110
111
112
113
114
115
116
117
118
# File 'lib/simply_versioned.rb', line 110

def with_versioning( enabled, &block )
  versioning_was_enabled = self.versioning_enabled?
  self.versioning_enabled = enabled
  begin
    block.call( self )
  ensure
    self.versioning_enabled = versioning_was_enabled
  end
end