0.0
No commit activity in last 3 years
No release in over 3 years
Mongoid Genesis will give you the ability to override attribute values without losing the original one.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0.1.0
>= 0.8.7
>= 2.0

Runtime

>= 2.0
 Project Readme

Mongoid Genesis

Build Status

Mongoid Genesis will give you the ability to override data in your model without losing the initial data.

Install

gem install mongoid-genesis

Rails 3

In your Gemfile:

gem 'mongoid-genesis'

Usage

Mongoid Genesis is compatible with any mongoid collection or embedded object.

Model integration

class Book
  include Mongoid::Document
  include Mongoid::Genesis
end

This will create an embedded object that will store the original data.

Basic structure

book = Book.new(:title => 'The Art of War', :author => 'Sun Tzu')
#=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">

book.genesis
#=> #<BookGenesis _id: 1>

Preserve the original attribute

book.write_and_preserve_attribute(:author, 'Sun Zi')
#=> #<Book _id: 1, title: "The Art of War", author: "Sun Zi">

book.genesis
#=> #<BookGenesis _id: 1, author: "Sun Tzu">

After preserving the original attribute, it will not be overwritten

book.write_and_preserve_attribute(:author, 'Sun Wu')
#=> #<Book _id: 1, title: "The Art of War", author: "Sun Wu">

book.genesis
#=> #<BookGenesis _id: 1, author: "Sun Tzu">

At all time, you can read the original attribute

book.read_attribute_genesis(:title)
#=> "The Art of War"

book.write_and_preserve_attribute(:title, 'The Art of Peace')
book.read_attribute_genesis(:title)
#=> "The Art of War"

You can restore the original attribute

book.restore_genesis(:author)
#=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">

book.genesis
#=> #<BookGenesis _id: 1, author: nil>

To update the original document without losing the current state

book.write_and_preserve_attribute(:title, 'The Art of Peace')
book.reverse_genesis

#=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">
#=> #<BookGenesis _id: 1, title: "The Art of Peace">

book.title = "The Art of War : Revisited"
book.reverse_genesis

#=> #<Book _id: 1, title: "The Art of Peace", author: "Sun Tzu">
#=> #<BookGenesis _id: 1, title: "The Art of War : Revisited">

Cheat Sheet

read_attribute_genesis(field_name)

Read the original attribute of the record. If the attribute wasn't overwritten, it will return the same thing as .read_attribute.

restore_genesis(field_name)

Restore the original value for the given field

reverse_genesis

Restore the record to its original state

write_and_preserve_attribute(field_name, value)

Overwrite the attribute with the value and saves the original value in the genesis object.

Copyright

Copyright (c) 2012 De Marque inc. See LICENSE for further details.