Sha256: f712060e675eb4e3894b12c610a06a9ec82aee470ed0e9f71144bb2b0c2b6161

Contents?: true

Size: 994 Bytes

Versions: 1

Compression:

Stored size: 994 Bytes

Contents

module Mongoid
  module Genesis
    extend ActiveSupport::Concern

    def self.included(base)
      Object.const_set("#{base.name}Genesis", Class.new do
        include Mongoid::Document
        include Mongoid::Genesis::Storage

        embedded_in :alter, :class_name => base.name, :inverse_of => :genesis
      end)

      base.embeds_one :genesis, :class_name => "#{base.name}Genesis", :inverse_of => :alter
    end

    def init_genesis
      self.genesis = "#{self.class.name}Genesis".constantize.new
    end

    def restore_genesis(field_name)
      self.genesis.restore(field_name)
    end

    def reverse_genesis
      self.genesis.reverse
    end

    def write_and_preserve_attribute(field_name, value)
      init_genesis if not self.genesis

      if value and self.genesis.read_attribute(field_name) == value
        self.restore_genesis(field_name)
      else
        self.genesis.preserve field_name
        self.write_attribute(field_name, value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-genesis-0.0.3 lib/mongoid/genesis.rb