lib/vestal_versions.rb in laserlemon-vestal_versions-0.4.2 vs lib/vestal_versions.rb in laserlemon-vestal_versions-0.4.3
- old
+ new
@@ -9,14 +9,14 @@
module ClassMethods
def versioned
has_many :versions, :as => :versioned, :order => 'versions.number ASC', :dependent => :destroy do
def at(value)
case value
- when Version: value
- when Numeric: find_by_number(value.floor)
- when Symbol: respond_to?(value) ? send(value) : nil
- when Date, Time: last(:conditions => ['versions.created_at <= ?', value.to_time])
+ when Version then value
+ when Numeric then find_by_number(value.floor)
+ when Symbol then respond_to?(value) ? send(value) : nil
+ when Date, Time then last(:conditions => ['versions.created_at <= ?', value.to_time])
end
end
def between(from_value, to_value)
from, to = number_at(from_value), number_at(to_value)
@@ -29,20 +29,21 @@
private
def number_at(value)
case value
- when Version: value.number
- when Numeric: value.floor
- when Symbol, Date, Time: at(value).try(:number)
+ when Version then value.number
+ when Numeric then value.floor
+ when Symbol, Date, Time then at(value).try(:number)
end
end
end
after_save :create_version, :if => :needs_version?
include InstanceMethods
+ alias_method_chain :reload, :versions
end
end
module InstanceMethods
private
@@ -50,10 +51,11 @@
def needs_version?
!changed.empty?
end
def reset_version(new_version = nil)
+ @last_version = nil if new_version.nil?
@version = new_version
end
def create_version
if versions.empty?
@@ -67,10 +69,23 @@
end
public
def version
- @version ||= versions.maximum(:number)
+ @version ||= last_version
+ end
+
+ def last_version
+ @last_version ||= versions.maximum(:number)
+ end
+
+ def reverted?
+ version != last_version
+ end
+
+ def reload_with_versions(*args)
+ reset_version
+ reload_without_versions(*args)
end
def revert_to(value)
chain = versions.between(version, value)
return version unless chain.size > 1