Module: ActiveVersion::Revisions::HasRevisions::ClassMethods
- Defined in:
- lib/active_version/revisions/has_revisions.rb,
sig/active_version/revisions.rbs
Instance Method Summary collapse
- #apply_revision_table_name!(klass) ⇒ Object
- #class_revision_enabled? ⇒ Boolean
-
#create_snapshots(opts = {}) ⇒ Object
Create snapshots for all records.
-
#has_revisions(options = {}) ⇒ Hash[Symbol, untyped]
Declare that a model has revisions.
-
#has_revisions? ⇒ Boolean
Check if model has revisions configured.
- #normalize_identity_columns(value) ⇒ Object
-
#normalize_revision_options(options) ⇒ Hash[Symbol, untyped]
Normalize revision options.
- #register_revision_column_mappings_from_destination(revision_klass) ⇒ Object
- #revision_class ⇒ Class
- #revision_class_name ⇒ String
-
#revision_on_update ⇒ Object
Manual callback installation methods.
- #revision_record? ⇒ Boolean
- #setup_revision_associations ⇒ Object
-
#setup_revision_callbacks(options) ⇒ Object
Set up callbacks based on options.
-
#with_revisions { ... } ⇒ Object
Enable revisions for a block.
-
#without_revisions { ... } ⇒ Object
Disable revisions for a block.
Instance Method Details
#apply_revision_table_name!(klass) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/active_version/revisions/has_revisions.rb', line 146 def apply_revision_table_name!(klass) = ActiveVersion.registry.config_for(self, :revisions) || {} custom_table_name = [:table_name] return klass unless custom_table_name && klass.respond_to?(:table_name=) klass.table_name = custom_table_name.to_s klass end |
#class_revision_enabled? ⇒ Boolean
220 221 222 |
# File 'lib/active_version/revisions/has_revisions.rb', line 220 def class_revision_enabled? @class_revision_enabled != false end |
#create_snapshots(opts = {}) ⇒ Object
Create snapshots for all records
185 186 187 188 189 190 191 |
# File 'lib/active_version/revisions/has_revisions.rb', line 185 def create_snapshots(opts = {}) scope = opts[:only_missing] ? where.missing(:revisions) : all scope.find_each do |record| record.create_snapshot!(opts) end end |
#has_revisions(options = {}) ⇒ Hash[Symbol, untyped]
Declare that a model has revisions
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_version/revisions/has_revisions.rb', line 93 def has_revisions( = {}) # Store custom revision class if specified if [:as] @custom_revision_class = [:as] = .except(:as) end # Normalize and store options self. = () # Register options before resolving revision FK so custom foreign_key # is visible while associations are being built. ActiveVersion.registry.register(self, :revisions, ) # Ensure association is set up after custom class option is known. @revision_associations_setup = false setup_revision_associations if respond_to?(:setup_revision_associations) # Initialize enabled state (default to true) @class_revision_enabled = true # Set up callbacks based on options setup_revision_callbacks() end |
#has_revisions? ⇒ Boolean
Check if model has revisions configured
119 120 121 |
# File 'lib/active_version/revisions/has_revisions.rb', line 119 def has_revisions? .present? end |
#normalize_identity_columns(value) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/active_version/revisions/has_revisions.rb', line 139 def normalize_identity_columns(value) return nil if value.nil? return value.map(&:to_s) if value.is_a?(Array) value.to_s end |
#normalize_revision_options(options) ⇒ Hash[Symbol, untyped]
Normalize revision options
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/active_version/revisions/has_revisions.rb', line 124 def () { on: Array.wrap([:on] || [:update]), if: [:if], unless: [:unless], auto: .fetch(:auto, true), only: Array.wrap([:only] || []).map(&:to_s), except: Array.wrap([:except] || []).map(&:to_s), foreign_key: normalize_identity_columns([:foreign_key]), identity_resolver: [:identity_resolver], table_name: [:table_name], error_behavior: [:error_behavior] } end |
#register_revision_column_mappings_from_destination(revision_klass) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/active_version/revisions/has_revisions.rb', line 155 def register_revision_column_mappings_from_destination(revision_klass) return unless revision_klass.respond_to?(:revision_column_for) version_column = revision_klass.revision_column_for(:version) return unless version_column ActiveVersion.column_mapper.register(self, :revisions, :version, version_column) end |
#revision_class ⇒ Class
6 |
# File 'sig/active_version/revisions.rbs', line 6
def revision_class: () -> Class
|
#revision_class_name ⇒ String
7 |
# File 'sig/active_version/revisions.rbs', line 7
def revision_class_name: () -> String
|
#revision_on_update ⇒ Object
Manual callback installation methods
180 181 182 |
# File 'lib/active_version/revisions/has_revisions.rb', line 180 def revision_on_update before_update :create_revision_before_update, if: :should_create_revision? end |
#revision_record? ⇒ Boolean
5 |
# File 'sig/active_version/revisions.rbs', line 5
def revision_record?: () -> bool
|
#setup_revision_associations ⇒ Object
8 |
# File 'sig/active_version/revisions.rbs', line 8
def setup_revision_associations: () -> untyped
|
#setup_revision_callbacks(options) ⇒ Object
Set up callbacks based on options
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_version/revisions/has_revisions.rb', line 163 def setup_revision_callbacks() # Remove existing callbacks first if they exist if respond_to?(:_update_callbacks) callbacks = _update_callbacks.select { |cb| cb.filter == :create_revision_before_update } callbacks.each { |cb| skip_callback(:update, :before, :create_revision_before_update) } end # If auto is false or on is empty, don't install callbacks automatically return if [:auto] == false || [:on] == [] # Install callbacks for specified events if [:on].include?(:update) before_update :create_revision_before_update, if: :should_create_revision? end end |
#with_revisions { ... } ⇒ Object
Enable revisions for a block
212 213 214 215 216 217 218 |
# File 'lib/active_version/revisions/has_revisions.rb', line 212 def with_revisions revision_was_enabled = class_revision_enabled? enable_revisions yield ensure disable_revisions unless revision_was_enabled end |
#without_revisions { ... } ⇒ Object
Disable revisions for a block
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/active_version/revisions/has_revisions.rb', line 194 def without_revisions callbacks = if respond_to?(:_update_callbacks) _update_callbacks.select { |cb| cb.filter == :create_revision_before_update } else [] end callback_installed = callbacks.any? skip_callback(:update, :before, :create_revision_before_update) if callback_installed yield ensure # Restore callback if it was set up if callback_installed && && [:auto] != false && [:on].include?(:update) set_callback(:update, :before, :create_revision_before_update, if: :should_create_revision?) end end |