Mongoid Genesis will give you the ability to override data in your model without losing the initial data.
gem install mongoid-genesis
In your Gemfile:
gem 'mongoid-genesis'
Mongoid Genesis is compatible with any mongoid collection or embedded object.
class Book
include Mongoid::Document
include Mongoid::Genesis
end
This will create an embedded object that will store the original data.
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>
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">
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">
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"
book.restore_genesis(:author)
#=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">
book.genesis
#=> #<BookGenesis _id: 1, author: nil>
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">
Read the original attribute of the record. If the attribute wasn't overwritten, it will return the same thing as .read_attribute.
Restore the original value for the given field
Restore the record to its original state
Overwrite the attribute with the value and saves the original value in the genesis object.
Copyright (c) 2012 De Marque inc. See LICENSE for further details.