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.

3 posts by Andrey Morozov @andr_morozov

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'


> 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|
    userId: revision.user_id,
    userEmail: revision.user&.email,
    change: changes_type(revision)

def changes_type(revision)
  if['changes'].include?('drive_file_id') &&['changes'].size > 4 &&['changes']['drive_file_id'].present?
    'Object updated & Document generated'
  elsif['changes'].include?('drive_file_id') &&['changes'].size == 4
    'Document generated'
  elsif['changes'].include?('number') &&['changes']['number'].nil?
    'Object created'
    'Object updated'

But of course this is an abstract example with my data 😏

Command for create zip archive without gem's πŸ“

class CreateZipCommand
  def call(files)
    # Create temp directory for files 
    tmp_dir = Dir.mktmpdir
    tmp_zip_path = File.join(tmp_dir, "")
    # Move files to the temporary folder you created above do |file|
      download_file(file, tmp_dir)
    # Go to the folder and archive the entire contents
    `cd #{tmp_dir} && zip #{tmp_zip_path} ./*`
    # Return zip path

=> "/var/folders/bk/0c864z710654sx555jpdpx9c0000gn/T/d20190126-7447-d27fpl/")

Most gems for working with archives eat a lot of memory when working with large files. This solution does not have these problems.

Make sure that the zip utility is installed on your computer - it don’t work without it

Webhook integration in development with Ngrok πŸš€

All begin when I’m using the Pipedrive (is a sales management tool designed to help small sales teams manage intricate or lengthy sales processes) webhook.

The solution was easy I just create an ngrok tunnel

> ngrok http 3000 

Session Status                online
Account                       Andrey (Plan: Free)
Version                       2.2.8
Region                        United States (us)
Web Interface       
Forwarding           -> localhost:3000
Forwarding           -> localhost:3000

and then sent the webhook to the generated address and it works. In local development I can user webhook from another api! It’s work πŸš€

To use it you need to do 4 steps:

  1. Register on ngrok
  2. Download ngrok from site
  3. Connect your account
  4. Run it πŸš€


In addition, this service has a ngrok-tunel gem that allows you to fully integrate it with your application. But that’s another story πŸ’Ž