Module: Glue::Revisable

Defined in:
lib/glue/revisable.rb

Overview

Revision support for Og-managed classes.

class Article

is Revisable
property :body, String, :revisable => true
property :title, String

end

Generates the Revision class:

class Article::Revision

article.revisions

article.revise do |a|

a.title = 'hello'
a.body = 'world'

end

article.rollback(4)

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/glue/revisable.rb', line 63

def self.included base
  super
  
  base.module_eval %{
    class Revision < #{base}
      include Revisable::Mixin
      refers_to #{base}
    end
  }
  
  base.has_many :revisions, base::Revision
end

Instance Method Details

#get_revision(rev) ⇒ Object

Return a revision.



99
100
101
102
# File 'lib/glue/revisable.rb', line 99

def get_revision rev
  return self if rev.to_i == self.revision
  self.revisions.find_one(:condition => "revision=#{rev}")
end

#last_revisionObject

Return the last revision.



106
107
108
# File 'lib/glue/revisable.rb', line 106

def last_revision
  self.revisions(:order => 'revision DESC', :limit => 1).first
end

#revise(options = {}) {|_self| ... } ⇒ Object Also known as: revise!

Can accept options like owner or a comment. You can specialize this in your app.

Yields:

  • (_self)

Yield Parameters:



79
80
81
82
83
84
85
86
87
88
# File 'lib/glue/revisable.rb', line 79

def revise options = {}
  if self.revision.nil? 
    self.revision = 1 
  else
    self.revision += 1
  end
  self.revisions << self.class::Revision.new(self, options)
  yield(self) if block_given?
  self.save
end

#revision_countObject Also known as: revisions_count

The number of revisions.



112
113
114
# File 'lib/glue/revisable.rb', line 112

def revision_count
  self.revisions.count
end

#rollback(rev, options = {}) ⇒ Object

Rollback to an older revision.



93
94
95
# File 'lib/glue/revisable.rb', line 93

def rollback rev, options = {}
  self.revise(options) { |obj| get_revision(rev).apply_to(obj) }
end