lib/glue/revisable.rb in og-0.31.0 vs lib/glue/revisable.rb in og-0.40.0
- old
+ new
@@ -1,13 +1,13 @@
module Glue
# Revision support for Og-managed classes.
#
# class Article
-# is Revisable
-# property :body, String, :revisable => true
-# property :title, String
+# is Revisable
+# property :body, String, :revisable => true
+# property :title, String
# end
#
# Generates the Revision class:
#
# class Article::Revision
@@ -22,11 +22,11 @@
# article.rollback(4)
module Revisable
# The revision of the revisable object.
- property :revision, Fixnum
+ attr_accessor :revision, Fixnum
# This mixin is injected into the dynamically generated
# Revision class. You can customize this in your application
# to store extra fields per revision.
@@ -35,52 +35,50 @@
property :create_time, Time
# Override to handle your options.
- def initialize(obj, options = {})
+ def initialize obj, options = {}
revision_from(obj)
@create_time = Time.now
end
- def revision_from(obj)
- for prop in obj.class.properties.values
- # gmosx, FIXME: test against primary key, not oid.
- unless prop.symbol == :oid
- instance_variable_set "@#{prop}", obj.send(prop.to_s)
+ def revision_from obj
+ for a in obj.class.serializable_attributes
+ unless a == obj.class.primary_key
+ instance_variable_set "@#{a}", obj.send(a.to_s)
end
end
end
- def revision_to(obj)
- for prop in obj.class.properties.values
- # gmosx, FIXME: test against primary key, not oid.
- unless prop.symbol == :oid
- obj.instance_variable_set "@#{prop}", self.send(prop.to_s)
+ def revision_to obj
+ for a in obj.class.serializable_attributes
+ unless a == obj.class.primary_key
+ obj.instance_variable_set "@#{a}", self.send(a.to_s)
end
end
end
alias_method :apply_to, :revision_to
end
- def self.included(base)
+ def self.included base
super
base.module_eval %{
class Revision < #{base}
include Revisable::Mixin
- belongs_to #{base}
+ refers_to #{base}
end
}
base.has_many :revisions, base::Revision
end
# Can accept options like owner or a comment.
# You can specialize this in your app.
- def revise(options = {})
+ def revise options = {}
if self.revision.nil?
self.revision = 1
else
self.revision += 1
end
@@ -90,17 +88,17 @@
end
alias_method :revise!, :revise
# Rollback to an older revision.
- def rollback(rev, options = {})
+ def rollback rev, options = {}
self.revise(options) { |obj| get_revision(rev).apply_to(obj) }
end
# Return a revision.
-
- def get_revision(rev)
+
+ def get_revision rev
return self if rev.to_i == self.revision
self.revisions.find_one(:condition => "revision=#{rev}")
end
# Return the last revision.
@@ -117,8 +115,5 @@
alias_method :revisions_count, :revision_count
end
end
-
-# * George Moschovitis <gm@navel.gr>
-# * Dirk Barnikel <dirk.barnikel@gmx.de>