Name: mongoid_touch
Owner: Simpleweb
Description: A tiny mongoid extension to provide the `touch` method known from ActiveRecord to Mongoid::Document.
Created: 2012-09-10 09:06:36.0
Updated: 2013-01-12 04:31:33.0
Pushed: 2012-09-10 09:12:11.0
Homepage: https://github.com/asaaki/mongoid_touch
Size: 118
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A tiny mongoid extension to provide the touch
method known from ActiveRecord to Mongoid::Document.
Gemfile:
"mongoid_touch"
(Or install it with gem install mongoid_touch
and then require it: require "mongoid_touch"
.)
In model:
s MyModel
clude Mongoid::Document
clude Mongoid::Timestamps
clude Mongoid::Document::Touch
... your code ...
The method .touch
can have an optional parameter for your custom Time based field, otherwise it will try to use the updated_at
field (include Mongoid::Timestamps
or Mongoid::Timestamps::Updated
in your model).
Use .touch!
if you want to get exceptions if touching fails.
Now you can use:
odel_instance.touch
odel_instance.touch! # will raise errors if touching fails.
This will update the updated_at
field if present.
odel_instance.touch(:modified_at)
odel_instance.touch!(:modified_at) # will raise errors if touching fails.
This will update the custom field modified_at
if present.
touch!
If the corresponding field is not present, mongoid_touch will raise a Mongoid::Errors::MissingField
.
If the object is frozen, an exception of Mongoid::Errors::FrozenInstance
is thrown.
If the underlying update_attribute
doesn't return true, Mongoid::Errors::DocumentNotUpdated
is thrown (unless mongoid itself throws an error).
mongoid_touch
Copyright (c) 2011-2012 Christoph Grabo. See LICENSE.txt for further details.