module Sequel::Plugins::Touch::InstanceMethods
Public Instance Methods
Source
# File lib/sequel/plugins/touch.rb 86 def after_create 87 super 88 touch_associations 89 end
Touch
all of the model’s touched_associations when creating the object.
Source
# File lib/sequel/plugins/touch.rb 92 def after_destroy 93 super 94 touch_associations 95 end
Touch
all of the model’s touched_associations when destroying the object.
Source
# File lib/sequel/plugins/touch.rb 98 def after_update 99 super 100 touch_associations 101 end
Touch
all of the model’s touched_associations when updating the object.
Source
# File lib/sequel/plugins/touch.rb 107 def touch(column=nil) 108 if column 109 set(column=>touch_instance_value) 110 else 111 column = model.touch_column 112 set(column=>touch_instance_value) if columns.include?(column) 113 end 114 save_changes 115 end
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.
Private Instance Methods
Source
# File lib/sequel/plugins/touch.rb 121 def touch_association_value 122 Sequel::CURRENT_TIMESTAMP 123 end
The value to use when modifying the touch column for the association datasets. Uses the SQL
standard CURRENT_TIMESTAMP.
Source
# File lib/sequel/plugins/touch.rb 126 def touch_associations 127 model.touched_associations.each do |assoc, column| 128 r = model.association_reflection(assoc) 129 next unless r.can_have_associated_objects?(self) 130 ds = public_send(r[:dataset_method]) 131 132 if ds.send(:joined_dataset?) 133 # Can't update all values at once, so update each instance individually. 134 # Instead if doing a simple save, update via the instance's dataset, 135 # to avoid going into an infinite loop in some cases. 136 public_send(assoc).each{|x| x.this.update(column=>touch_association_value)} 137 else 138 # Update all values at once for performance reasons. 139 ds.update(column=>touch_association_value) 140 associations.delete(assoc) 141 end 142 end 143 end
Update the updated at field for all associated objects that should be touched.
Source
# File lib/sequel/plugins/touch.rb 147 def touch_instance_value 148 model.dataset.current_datetime 149 end
The value to use when modifying the touch column for the model instance. Uses Time/DateTime.now to work well with typecasting.