Sha256: d1f1011def3c356292c305d406345f6509bc7b211370594b085352709e684855

Contents?: true

Size: 788 Bytes

Versions: 3

Compression:

Stored size: 788 Bytes

Contents

class Version
  include MongoMapper::Document
  include Comparable
  
  key :number, Integer
  key :versioned_type, String
  key :versioned_id, ObjectId
  key :changes, Hash
  timestamps!

  belongs_to :versioned, :polymorphic => true
  def changes
    read_attribute(:changes)
  end
  alias_attribute :version, :number

  def <=>(other)
    number <=> other.number
  end

  def previous
    find_related(:first, :number => {:$lt => number}, :order => 'number.desc')
  end

  def next
    find_related(:first, :number => {:$gt => number}, :order => 'number.asc')
  end

  protected

  def find_related(*args)
    options = args.extract_options!
    params = options.merge(:versioned_id => versioned_id, :versioned_type => versioned_type)
    self.class.find(args.first, params)
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
versioned-0.2.0 lib/version.rb
jacqui-versioned-0.1.1 lib/version.rb
mrkurt-versioned-0.1.1 lib/version.rb