module Irwi::Extensions::Models::WikiPage
  
  module ClassMethods
    
    def find_by_path_or_new( path )
      self.find_by_path( path ) || self.new( :path => path, :title => path )
    end
    
  end
  
  module InstanceMethods
    
    # Retrieve number of last version
    def last_version_number
      last = versions.first
      last ? last.number : 0
    end
    
    protected
        
    def create_new_version
      n = last_version_number
      
      v = versions.build
      v.attributes = attributes.slice( *v.attribute_names )
      v.comment = comment
      v.content = ''
      v.number = n + 1
      v.save!
    end
    
  end
  
  def self.included( base )
    base.send :extend, Irwi::Extensions::Models::WikiPage::ClassMethods
    base.send :include, Irwi::Extensions::Models::WikiPage::InstanceMethods
    
    base.send :attr_accessor, :comment, :previous_version_number
    
    base.belongs_to :creator, :class_name => Irwi.config.user_class_name
    base.belongs_to :updator, :class_name => Irwi.config.user_class_name
    
    base.has_many :versions, :class_name => Irwi.config.page_version_class_name, :foreign_key => Irwi.config.page_version_foreign_key, :order => 'id DESC'
    
    base.after_save :create_new_version
  end
  
end
module Irwi::Extensions::Models::WikiPage
  
  module ClassMethods
    
    def find_by_path_or_new( path )
      self.find_by_path( path ) || self.new( :path => path, :title => path )
    end
    
  end
  
  module InstanceMethods
    
    # Retrieve number of last version
    def last_version_number
      last = versions.first
      last ? last.number : 0
    end
    
    protected
        
    def create_new_version
      n = last_version_number
      
      v = versions.build
      v.attributes = attributes.slice( *v.attribute_names )
      v.comment = comment
      v.number = n + 1
      v.save!
    end
    
  end
  
  def self.included( base )
    base.send :extend, Irwi::Extensions::Models::WikiPage::ClassMethods
    base.send :include, Irwi::Extensions::Models::WikiPage::InstanceMethods
    
    base.send :attr_accessor, :comment, :previous_version_number
    
    base.belongs_to :creator, :class_name => Irwi.config.user_class_name
    base.belongs_to :updator, :class_name => Irwi.config.user_class_name
    
    base.has_many :versions, :class_name => Irwi.config.page_version_class_name, :foreign_key => Irwi.config.page_version_foreign_key, :order => 'id DESC'
    
    base.after_save :create_new_version
  end
  
end
Revert "Setting the content value in the versions to be an empty string"
Revert "Setting the content value in the versions to be an empty string"

This reverts commit 697d4bced3e312f790333022b2d1808af906113a.
