module Sequel::Plugins::Touch

The touch plugin adds a touch method to model instances, which saves the object with a modified timestamp. By default, it uses the :updated_at column, but you can set which column to use. It also supports touching of associations, so that when the current model object is updated or destroyed, the associated rows in the database can have their modified timestamp updated to the current timestamp.

Since the instance touch method works on model instances, it uses Time.now for the timestamp. The association touching works on datasets, so it updates all related rows in a single query, using the SQL standard CURRENT_TIMESTAMP. Both of these can be overridden easily if necessary.

Usage:

# Allow touching of all model instances (called before loading subclasses)
Sequel::Model.plugin :touch

# Allow touching of Album instances, with a custom column
Album.plugin :touch, column: :updated_on

# Allow touching of Artist instances, updating the albums and tags
# associations when touching, touching the +updated_on+ column for
# albums and the +updated_at+ column for tags
Artist.plugin :touch, associations: [{albums: :updated_on}, :tags]