Use gem Recorder for db log 💪
Gem Recorder tracks changes of your Rails models
For start use this library in Rails 5.2 you need add gem to the Gemfile:
gem 'recorder', github: 'jetrockets/recorder', branch: 'rails_5_2'
Next:
> bundle install
> rails g recorder:install
> rails db:migrate
And final step -- add to model:
include ::Recorder::Observer
recorder async: false
And it's all 🎉
For an example - logs can be issued in the representer:
Recorder::Revision.where(item: object).order(id: :DESC).limit(10).map do |revision|
{
revision_id: revision.id,
userId: revision.user_id,
userEmail: revision.user&.email,
change: changes_type(revision)
}
end
def changes_type(revision)
if revision.data['changes'].include?('drive_file_id') && revision.data['changes'].size > 4 && revision.data['changes']['drive_file_id'].present?
'Object updated & Document generated'
elsif revision.data['changes'].include?('drive_file_id') && revision.data['changes'].size == 4
'Document generated'
elsif revision.data['changes'].include?('number') && revision.data['changes']['number'].nil?
'Object created'
else
'Object updated'
end
end
But of course this is an abstract example with my data 😏
Discover More Reads
Categories: