Module: PaperTrail::ActiveRecordExt::BaseExtensions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/paper_trail/active_record/base_extensions.rb
Overview
Extensions to ActiveRecord::Base to better support PaperTrail
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#created_version ⇒ Object
module ClassMethods.
-
#paper_trail_update_column_using_value_from_changes(name, changes: changes(), touch: true) ⇒ Object
Workaround to prevent paper_trail from incorrectly recording a change from new value to new value.
-
#paper_trail_update_columns_using_value_from_changes(*names, changes: changes(), touch: true) ⇒ Object
Tested by: spec/lib/active_record/base_extensions/paper_trail_extensions_spec.rb.
Instance Method Details
#created_version ⇒ Object
module ClassMethods
100 101 102 |
# File 'lib/paper_trail/active_record/base_extensions.rb', line 100 def created_version versions.creates.first end |
#paper_trail_update_column_using_value_from_changes(name, changes: changes(), touch: true) ⇒ Object
Workaround to prevent paper_trail from incorrectly recording a change from new value to new value. Useful if you've already set the attribute to the new value and now you want to save that change to the database, with versioning.
107 108 109 |
# File 'lib/paper_trail/active_record/base_extensions.rb', line 107 def paper_trail_update_column_using_value_from_changes(name, changes: changes(), touch: true) paper_trail_update_columns_using_value_from_changes name, changes: changes, touch: touch end |
#paper_trail_update_columns_using_value_from_changes(*names, changes: changes(), touch: true) ⇒ Object
Tested by: spec/lib/active_record/base_extensions/paper_trail_extensions_spec.rb
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/paper_trail/active_record/base_extensions.rb', line 112 def paper_trail_update_columns_using_value_from_changes(*names, changes: changes(), touch: true) new_values = {} names.each do |name| (old_value, new_value = changes[name]) or next self.send("#{name}=", old_value) new_values[name] = new_value end new_values[:updated_at] = Time.now if has_attribute?(:updated_at) && touch paper_trail.update_columns new_values end |