Module: Whodunit::Stampable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/whodunit/stampable.rb
Overview
Main concern for adding creator/updater/deleter tracking to ActiveRecord models.
This module provides automatic tracking of who created, updated, and deleted records. It intelligently sets up callbacks and associations based on available columns and soft-delete detection.
rubocop:disable Metrics/ModuleLength
Callback Methods collapse
-
#set_whodunit_creator ⇒ void
private
Set the creator ID when a record is created.
-
#set_whodunit_deleter ⇒ void
private
Set the deleter ID when a record is destroyed or soft-deleted.
-
#set_whodunit_updater ⇒ void
private
Set the updater ID when a record is updated.
Column Presence Checks collapse
-
#being_soft_deleted? ⇒ Boolean
private
Check if the current update operation is a soft-delete.
-
#creator_column? ⇒ Boolean
private
Check if the model has a creator column and it’s enabled.
-
#deleter_column? ⇒ Boolean
private
Check if the model has a deleter column and it’s enabled.
-
#updater_column? ⇒ Boolean
private
Check if the model has an updater column and it’s enabled.
Instance Method Details
#being_soft_deleted? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the current update operation is a soft-delete.
Uses ActiveRecord’s dirty tracking to detect if any soft-delete columns are being changed from nil to a timestamp, which indicates a soft-delete operation.
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/whodunit/stampable.rb', line 280 def being_soft_deleted? return false unless deleter_column? return false unless Whodunit::Current.user_id soft_delete_column = self.class.whodunit_setting(:soft_delete_column) return false unless soft_delete_column # Simple: just check the configured soft-delete column column_name = soft_delete_column.to_s attribute_changed?(column_name) && attribute_was(column_name).nil? && !send(column_name).nil? end |
#creator_column? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the model has a creator column and it’s enabled.
244 245 246 247 248 249 |
# File 'lib/whodunit/stampable.rb', line 244 def creator_column? return false unless self.class.model_creator_enabled? column_name = self.class.whodunit_setting(:creator_column).to_s self.class.column_names.include?(column_name) end |
#deleter_column? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the model has a deleter column and it’s enabled.
266 267 268 269 270 271 |
# File 'lib/whodunit/stampable.rb', line 266 def deleter_column? return false unless self.class.model_deleter_enabled? column_name = self.class.whodunit_setting(:deleter_column).to_s self.class.column_names.include?(column_name) end |
#set_whodunit_creator ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set the creator ID when a record is created.
This method is automatically called before_create if the model has a creator column. It sets the creator_id to the current user from Whodunit::Current.
198 199 200 201 202 |
# File 'lib/whodunit/stampable.rb', line 198 def set_whodunit_creator return unless Whodunit::Current.user_id self[self.class.whodunit_setting(:creator_column)] = Whodunit::Current.user_id end |
#set_whodunit_deleter ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set the deleter ID when a record is destroyed or soft-deleted.
This method is automatically called in two scenarios: 1. before_destroy for hard deletes (if deleter column exists) 2. before_update for soft-deletes (when being_soft_deleted? returns true)
It sets the deleter_id to the current user from Whodunit::Current.
232 233 234 235 236 |
# File 'lib/whodunit/stampable.rb', line 232 def set_whodunit_deleter return unless Whodunit::Current.user_id self[self.class.whodunit_setting(:deleter_column)] = Whodunit::Current.user_id end |
#set_whodunit_updater ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Set the updater ID when a record is updated.
This method is automatically called before_update if the model has an updater column. It sets the updater_id to the current user from Whodunit::Current. Does not run on new records (creation) or during soft-delete operations.
212 213 214 215 216 217 218 219 220 |
# File 'lib/whodunit/stampable.rb', line 212 def set_whodunit_updater return unless Whodunit::Current.user_id return if new_record? # Don't set updater on creation return if being_soft_deleted? # Don't set updater during soft-delete self[self.class.whodunit_setting(:updater_column)] = Whodunit::Current.user_id end |
#updater_column? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the model has an updater column and it’s enabled.
255 256 257 258 259 260 |
# File 'lib/whodunit/stampable.rb', line 255 def updater_column? return false unless self.class.model_updater_enabled? column_name = self.class.whodunit_setting(:updater_column).to_s self.class.column_names.include?(column_name) end |