Today I Learned

This project exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as selected friends of JetRockets. We hope you enjoy learning along with us.

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 😏


https://github.com/jetrockets/recorder

Looking for help? JetRockets has been an leader in Ruby on Rails since 2012. Rails is a core skill for each developer at JetRockets and we'd love to take a look at your project. Contact us and find out how we can help you.