module Sequel::Plugins::Touch::InstanceMethods
Public Instance Methods
Touch all of the model's touched_associations when creating the object.
# File lib/sequel/plugins/touch.rb, line 85 def after_create super touch_associations end
Touch all of the model's touched_associations when destroying the object.
# File lib/sequel/plugins/touch.rb, line 91 def after_destroy super touch_associations end
Touch all of the model's touched_associations when updating the object.
# File lib/sequel/plugins/touch.rb, line 97 def after_update super touch_associations 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.
# File lib/sequel/plugins/touch.rb, line 106 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
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 120 def touch_association_value Sequel::CURRENT_TIMESTAMP end
Update the updated at field for all associated objects that should be touched.
# File lib/sequel/plugins/touch.rb, line 125 def touch_associations model.touched_associations.each do |assoc, column| r = model.association_reflection(assoc) next unless r.can_have_associated_objects?(self) ds = public_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. public_send(assoc).each{|x| x.this.update(column=>touch_association_value)} else # Update all values at once for performance reasons. ds.update(column=>touch_association_value) associations.delete(assoc) end end end
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 146 def touch_instance_value model.dataset.current_datetime end