module Sequel::Plugins::Touch::InstanceMethods

Public Instance Methods

after_create() click to toggle source

Touch all of the model's touched_associations when creating the object.

Calls superclass method
# File lib/sequel/plugins/touch.rb, line 89
def after_create
  super
  touch_associations
end
after_destroy() click to toggle source

Touch all of the model's touched_associations when destroying the object.

Calls superclass method
# File lib/sequel/plugins/touch.rb, line 95
def after_destroy
  super
  touch_associations
end
after_update() click to toggle source

Touch all of the model's touched_associations when updating the object.

Calls superclass method
# File lib/sequel/plugins/touch.rb, line 101
def after_update
  super
  touch_associations
end
touch(column=nil) click to toggle source

Touch the model object. If a column is not given, use the model's touch_column as the column. If the column to use is not one of the model's columns, just save the changes to the object instead of attempting to a value that doesn't exist.

# File lib/sequel/plugins/touch.rb, line 110
def touch(column=nil)
  if column
    set(column=>touch_instance_value)
  else
    column = model.touch_column
    set(column=>touch_instance_value) if columns.include?(column)
  end
  save_changes
end

Private Instance Methods

touch_association_value() click to toggle source

The value to use when modifying the touch column for the association datasets. Uses the SQL standard CURRENT_TIMESTAMP.

# File lib/sequel/plugins/touch.rb, line 124
def touch_association_value
  Sequel::CURRENT_TIMESTAMP
end
touch_associations() click to toggle source

Update the updated at field for all associated objects that should be touched.

# File lib/sequel/plugins/touch.rb, line 129
def touch_associations
  model.touched_associations.each do |assoc, column|
    r = model.association_reflection(assoc)
    next unless r.can_have_associated_objects?(self)
    ds = send(r.dataset_method)

    if ds.send(:joined_dataset?)
      # Can't update all values at once, so update each instance individually.
      # Instead if doing a simple save, update via the instance's dataset,
      # to avoid going into an infinite loop in some cases.
      send(r[:name]).each{|x| x.this.update(column=>touch_association_value)}
    else
      # Update all values at once for performance reasons.
      ds.update(column=>touch_association_value)
    end
  end
end
touch_instance_value() click to toggle source

The value to use when modifying the touch column for the model instance. Uses Time/DateTime.now to work well with typecasting.

# File lib/sequel/plugins/touch.rb, line 149
def touch_instance_value
  model.dataset.current_datetime
end