lib/vestal_versions.rb in laserlemon-vestal_versions-0.5.0 vs lib/vestal_versions.rb in laserlemon-vestal_versions-0.6.0

- old
+ new

@@ -6,11 +6,11 @@ base.extend ClassMethods end module ClassMethods def versioned - has_many :versions, :as => :versioned, :order => 'versions.number ASC', :dependent => :destroy do + has_many :versions, :as => :versioned, :order => 'versions.number ASC', :dependent => :delete_all do def between(from_value, to_value) from, to = number_at(from_value), number_at(to_value) return [] if from.nil? || to.nil? condition = (from == to) ? to : Range.new(*[from, to].sort) all( @@ -35,34 +35,39 @@ when Symbol, Date, Time then at(value).try(:number) end end end - after_save :create_version, :if => :needs_version? + after_create :create_initial_version + after_update :create_initial_version, :if => :needs_initial_version? + after_update :create_version, :if => :needs_version? include InstanceMethods alias_method_chain :reload, :versions end end module InstanceMethods private + def needs_initial_version? + versions.empty? + end + 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? - versions.create(:changes => attributes, :number => 1) - else - versions.create(:changes => changes, :number => (last_version + 1)) - end + def create_initial_version + versions.create(:changes => nil, :number => 1) + end + def create_version + versions.create(:changes => changes, :number => (last_version + 1)) reset_version end public def version