simpleweb/mongoid_touch

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

mongoid_touch build status

A tiny mongoid extension to provide the touch method known from ActiveRecord to Mongoid::Document.

Install and Usage

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.

Exceptions for 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).

Contributing to mongoid_touch
Copyright

Copyright (c) 2011-2012 Christoph Grabo. See LICENSE.txt for further details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.