Sha256: 7a3ed19740a0dc64e5bd813fe45605d1ba8a97aa170fd8ff1391b357af1f2f86

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

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.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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
irwi-0.1.11 lib/irwi/extensions/models/wiki_page.rb
irwi-0.1.10 lib/irwi/extensions/models/wiki_page.rb
irwi-0.1.9 lib/irwi/extensions/models/wiki_page.rb
irwi-0.1.7 lib/irwi/extensions/models/wiki_page.rb