Module: Sequel::Plugins::Timestamps

Defined in:
lib/sequel/plugins/timestamps.rb

Overview

The timestamps plugin creates hooks that automatically set create and update timestamp fields. Both field names used are configurable, and you can also set whether to overwrite existing create timestamps (false by default), or whether to set the update timestamp when creating (also false by default).

Usage:

# Timestamp all model instances using +created_at+ and +updated_at+
# (called before loading subclasses)
Sequel::Model.plugin :timestamps

# Timestamp Album instances, with custom column names
Album.plugin :timestamps, create: :created_on, update: :updated_on

# Timestamp Artist instances, forcing an overwrite of the create
# timestamp, and setting the update timestamp when creating
Artist.plugin :timestamps, force: true, update_on_create: true

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(model, opts = OPTS) ⇒ Object

Configure the plugin by setting the available options. Note that if this method is run more than once, previous settings are ignored, and it will just use the settings given or the default settings. Options:

:allow_manual_update

Whether to skip setting the update timestamp if it has been modified manually (default: false)

:create

The field to hold the create timestamp (default: :created_at)

:force

Whether to overwrite an existing create timestamp (default: false)

:update

The field to hold the update timestamp (default: :updated_at)

:update_on_create

Whether to set the update timestamp to the create timestamp when creating (default: false)



32
33
34
35
36
37
38
39
40
# File 'lib/sequel/plugins/timestamps.rb', line 32

def self.configure(model, opts=OPTS)
  model.instance_exec do
    @allow_manual_timestamp_update = opts[:allow_manual_update]||false
    @create_timestamp_field = opts[:create]||:created_at
    @update_timestamp_field = opts[:update]||:updated_at
    @create_timestamp_overwrite = opts[:force]||false
    @set_update_timestamp_on_create = opts[:update_on_create]||false
  end
end