Module: RevisionSan

Extended by:
ActiveSupport::Concern
Defined in:
lib/revision_san.rb,
lib/revision_san/diff.rb

Defined Under Namespace

Modules: ClassMethods Classes: Diff

Instance Method Summary collapse

Instance Method Details

#compare_against_revision(revision) ⇒ Object

Return a RevisionSan::Diff object which compares this revision to the specified revision.

artist = Artist.create(:name => 'van Gogh')
artist.update_attribute(:name => 'Vincent van Gogh')

revision_1 = artist.fetch_revision(1)
diff = revision_1.compare_against_revision(2)
diff.name # => '<ins>Vincent </ins>van Gogh'


12
13
14
# File 'lib/revision_san/diff.rb', line 12

def compare_against_revision(revision)
  Diff.new(self, fetch_revision(revision))
end

#create_new_revisionObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/revision_san.rb', line 39

def create_new_revision
  if changed?
    record = self.class.new(:revision_parent_id => id)
    attributes.except('id', 'revision_parent_id').each do |key, value|
      record.send(:write_attribute, key, changes.has_key?(key) ? changes[key].first : value)
    end
    record.save(:validate => false)
    self.revision += 1
  end
end

#fetch_revision(revision) ⇒ Object



32
33
34
35
36
37
# File 'lib/revision_san.rb', line 32

def fetch_revision(revision)
  revision = revision.to_i
  return self if self.revision == revision
  sub_query = revision_parent_id.blank? ? "revision_parent_id = #{id}" : "(id = #{revision_parent_id} OR revision_parent_id = #{revision_parent_id})"
  self.class.find_without_current_revisions :first, :conditions => "#{sub_query} AND revision = #{revision}"
end

#revisionsObject



28
29
30
# File 'lib/revision_san.rb', line 28

def revisions
  self.class.find_without_current_revisions(:all, :conditions => { :revision_parent_id => id }, :order => 'id ASC') + [self]
end