Skip to content

Commit

Permalink
find_version returns latest record if version is nil. Added versions_…
Browse files Browse the repository at this point in the history
…count delegate Versioned model.
  • Loading branch information
josh committed Mar 4, 2008
1 parent f54f087 commit 395b562
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/acts_as_versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ def previous
def next
self.class.after(self)
end

def versions_count
page.version
end
end

versioned_class.cattr_accessor :original_class
Expand All @@ -284,6 +288,11 @@ def self.included(base) # :nodoc:
base.extend ClassMethods
end

# Finds a specific version of this record
def find_version(version = nil)
self.class.find_version(id, version)
end

# Saves a version of the model if applicable
def save_version
save_version_on_create if save_version?
Expand All @@ -309,6 +318,10 @@ def clear_old_versions
end
end

def versions_count
version
end

# Reverts a model to a given version. Takes either a version number or an instance of the versioned model
def revert_to(version)
if version.is_a?(self.class.versioned_class)
Expand Down Expand Up @@ -438,7 +451,9 @@ def write_changed_attribute(attr_name, attr_value)

module ClassMethods
# Finds a specific version of a specific row of this model
def find_version(id, version)
def find_version(id, version = nil)
return find(id) unless version

find_versions(id,
:conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version],
:limit => 1).first
Expand Down

0 comments on commit 395b562

Please sign in to comment.