This extension allows you to automatically track changes to your ActiveRecord models. It records changes to specified attributes whenever a record is saved.
Add this line to your application's Gemfile:
gem 'immosquare-active-record-change-tracker'
then Generate the migration to create the active_record_change_trackers
table:
rails generate immosquare_active_record_change_tracker:install
# create_table(:active_record_change_trackers) do |t|
# t.references(:recordable, :polymorphic => true, :foreign_key => false, :index => false, :null => false)
# t.references(:modifier, :polymorphic => true, :foreign_key => false, :index => false, :null => true)
# t.string(:event, :null => true, :limit => 10)
# t.text(:data, :null => true, :limit => 4_294_967_295)
# t.datetime(:created_at, :null => false)
# end
# add_index(:active_record_change_trackers, [:recordable_type, :recordable_id])
# add_index(:active_record_change_trackers, [:modifier_type, :modifier_id])
Then run the migration :
rails db:migrate
To enable history tracking for a model, add track_active_record_changes
to your model
class YourModel < ApplicationRecord
track_active_record_changes
# rest of your model code...
end
By default, changes to all attributes (except created_at
and updated_at
) will be tracked.
You can specify options to include or exclude specific attributes:
Exclude certain attributes :
class YourModel < ApplicationRecord
track_active_record_changes(except: [:attribute1, :attribute2])
# rest of your model code...
end
This will track changes to all attributes except :attribute1
and :attribute2
.
Include only certain attributes :
class YourModel < ApplicationRecord
track_active_record_changes(only: [:attribute3, :attribute4])
# rest of your model code...
end
This will track changes only to :attribute3
and :attribute4
.
Specify a modifier using a block :
The modifier can be specified by providing a block to track_active_record_changes, which allows you to capture dynamic context at the time changes are saved. (https://zainab-alshaikhli01.medium.com/activesupport-currentattributes-e3d43270207c)
class YourModel < ApplicationRecord
track_active_record_changes(except: [:attribute1]) do
## Your Logic to get the modifier
Current.admin.present? ? Current.admin : Current.user
end
# rest of your model code...
end
The gem is compatible with the paranoia gem (https://github.com/rubysherpas/paranoia) :
- If your model has
acts_as_paranoid
, then the deletion of a record will be recorded in theactive_record_change_trackers
table with the eventdestroy
, and the records ofcreate
andupdate
will be retained. - A really_destroy! command will completely delete the record from the
active_record_change_trackers
table. - Without this gem, the deletion of a record will not be recorded in the
active_record_change_trackers
table, and the records ofcreate
andupdate
will be deleted.
Each model that includes track_active_record_changes
automatically has access to its change history through the history_records
association. The history records are ordered by created_at
in descending order, meaning the most recent changes are listed first.
Example:
class YourModel < ApplicationRecord
track_active_record_changes
# rest of your model code...
end
Access change history :
your_model_instance.history_records
Bug reports and pull requests are welcome on GitHub at https://github.com/immosquare/immosquare-active-record-change-tacker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open-source under the terms of the MIT License.