Sha256: 75a1d68996fbb065386913f0bfdfee1305c45521637d23a27eefef744d53a2ec

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 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.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'

    if Irwi::config.page_attachment_class_name
      base.has_many :attachments, :class_name => Irwi.config.page_attachment_class_name, :foreign_key => Irwi.config.page_version_foreign_key
    end

    base.after_save :create_new_version
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
irwi-0.4.1 lib/irwi/extensions/models/wiki_page.rb
irwi-0.4.0 lib/irwi/extensions/models/wiki_page.rb