Module: ModelTimeline
- Defined in:
- lib/model_timeline.rb,
lib/model_timeline/rspec.rb,
lib/model_timeline/railtie.rb,
lib/model_timeline/version.rb,
lib/model_timeline/timelineable.rb,
lib/model_timeline/rspec/matchers.rb,
lib/model_timeline/timeline_entry.rb,
lib/model_timeline/configuration_error.rb,
lib/model_timeline/controller_additions.rb,
lib/model_timeline/generators/install_generator.rb
Overview
A module for tracking and recording changes to ActiveRecord models.
ModelTimeline provides functionality to create and maintain a history of model changes, including user attribution, timestamps, and additional contextual metadata.
Defined Under Namespace
Modules: ControllerAdditions, Generators, RSpec, Timelineable Classes: ConfigurationError, Railtie, TimelineEntry
Constant Summary collapse
- VERSION =
Current version of the ModelTimeline gem. Follows semantic versioning (semver.org/).
'0.1.4'
Class Attribute Summary collapse
-
.current_ip_method ⇒ Symbol
Gets the method name used to retrieve the client IP address.
-
.current_user_method ⇒ Symbol
Gets the method name used to retrieve the current user.
-
.enabled ⇒ Object
writeonly
Sets whether timeline recording is enabled.
Class Method Summary collapse
-
.clear_metadata! ⇒ Hash
Clears all metadata from thread-local storage.
-
.clear_request_store ⇒ Hash
Clears all data from the request store.
-
.configure {|self| ... } ⇒ void
Configures the ModelTimeline module.
-
.current_ip ⇒ String?
Gets the current IP address from thread-local storage.
-
.current_user ⇒ Object?
Gets the current user from thread-local storage.
-
.disable! ⇒ false
Disables timeline recording.
-
.enable! ⇒ true
Enables timeline recording.
-
.enabled? ⇒ Boolean
Checks if timeline recording is enabled.
-
.metadata ⇒ Hash
Gets the current metadata hash from thread-local storage.
-
.metadata=(hash) ⇒ Hash
Sets the metadata hash in thread-local storage.
-
.request_store ⇒ Hash
private
Gets the thread-local storage hash for the current request.
-
.store_user_and_ip(user, ip_address) ⇒ void
Stores the current user and IP address in thread-local storage.
-
.with_metadata(hash) { ... } ⇒ Object
Temporarily merges additional metadata for the duration of the block.
-
.with_timeline(current_user: nil, current_ip: nil, metadata: {}) { ... } ⇒ Object
Temporarily sets custom user, IP, and metadata for timeline entries.
-
.without_timeline { ... } ⇒ Object
Temporarily disables timeline recording for the duration of the block.
Class Attribute Details
.current_ip_method ⇒ Symbol
Gets the method name used to retrieve the client IP address
67 68 69 |
# File 'lib/model_timeline.rb', line 67 def current_ip_method @current_ip_method || :remote_ip end |
.current_user_method ⇒ Symbol
Gets the method name used to retrieve the current user
60 61 62 |
# File 'lib/model_timeline.rb', line 60 def current_user_method @current_user_method || :current_user end |
.enabled=(value) ⇒ Object (writeonly)
Sets whether timeline recording is enabled
47 48 49 |
# File 'lib/model_timeline.rb', line 47 def enabled=(value) @enabled = value end |
Class Method Details
.clear_metadata! ⇒ Hash
Clears all metadata from thread-local storage
193 194 195 |
# File 'lib/model_timeline.rb', line 193 def Thread.current[:model_timeline_metadata] = {} end |
.clear_request_store ⇒ Hash
Clears all data from the request store
158 159 160 |
# File 'lib/model_timeline.rb', line 158 def clear_request_store Thread.current[:model_timeline_request_store] = {} end |
.configure {|self| ... } ⇒ void
This method returns an undefined value.
Configures the ModelTimeline module
53 54 55 |
# File 'lib/model_timeline.rb', line 53 def configure yield self if block_given? end |
.current_ip ⇒ String?
Gets the current IP address from thread-local storage
151 152 153 |
# File 'lib/model_timeline.rb', line 151 def current_ip request_store[:ip_address] end |
.current_user ⇒ Object?
Gets the current user from thread-local storage
144 145 146 |
# File 'lib/model_timeline.rb', line 144 def current_user request_store[:current_user] end |
.disable! ⇒ false
Disables timeline recording
88 89 90 |
# File 'lib/model_timeline.rb', line 88 def disable! self.enabled = false end |
.enable! ⇒ true
Enables timeline recording
81 82 83 |
# File 'lib/model_timeline.rb', line 81 def enable! self.enabled = true end |
.enabled? ⇒ Boolean
Checks if timeline recording is enabled
74 75 76 |
# File 'lib/model_timeline.rb', line 74 def enabled? @enabled.nil? || @enabled end |
.metadata ⇒ Hash
Gets the current metadata hash from thread-local storage
165 166 167 |
# File 'lib/model_timeline.rb', line 165 def Thread.current[:model_timeline_metadata] ||= {} end |
.metadata=(hash) ⇒ Hash
Sets the metadata hash in thread-local storage
173 174 175 |
# File 'lib/model_timeline.rb', line 173 def (hash) Thread.current[:model_timeline_metadata] = hash end |
.request_store ⇒ Hash
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.
Gets the thread-local storage hash for the current request
127 128 129 |
# File 'lib/model_timeline.rb', line 127 def request_store Thread.current[:model_timeline_request_store] ||= {} end |
.store_user_and_ip(user, ip_address) ⇒ void
This method returns an undefined value.
Stores the current user and IP address in thread-local storage
136 137 138 139 |
# File 'lib/model_timeline.rb', line 136 def store_user_and_ip(user, ip_address) request_store[:current_user] = user request_store[:ip_address] = ip_address end |
.with_metadata(hash) { ... } ⇒ Object
Temporarily merges additional metadata for the duration of the block
182 183 184 185 186 187 188 |
# File 'lib/model_timeline.rb', line 182 def (hash) = .dup self. = .merge(hash) yield ensure self. = end |
.with_timeline(current_user: nil, current_ip: nil, metadata: {}) { ... } ⇒ Object
Temporarily sets custom user, IP, and metadata for timeline entries
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/model_timeline.rb', line 111 def with_timeline(current_user: nil, current_ip: nil, metadata: {}, &block) previous_user = ModelTimeline.current_user previous_ip = ModelTimeline.current_ip = ModelTimeline..dup ModelTimeline.store_user_and_ip(current_user, current_ip) if current_user || current_ip ModelTimeline.(, &block) ensure ModelTimeline.store_user_and_ip(previous_user, previous_ip) ModelTimeline. = end |
.without_timeline { ... } ⇒ Object
Temporarily disables timeline recording for the duration of the block
96 97 98 99 100 101 102 |
# File 'lib/model_timeline.rb', line 96 def without_timeline previous_state = enabled? disable! yield ensure self.enabled = previous_state end |