Latest from the Rocketship

We like writing about our personal experiences, our challenges and our take on programming and management. Enjoy!

LongreadsJanuary 15, 2019by Andrey Suhomozgy

Creating Sortable List with React, Redux, and Reselect

Will write simple React application with only one table and make it be sortable. But will keep sortable params inside Redux state and will use reselect lib to sort the collection.
Read more
TILJanuary 07, 2019by Dmitry Voronov

How to create zip files on the fly w/o Tempfile

There are many articles about how to archive files from the server and send a zip-file to a client without persisting it on the server. But usually they don't literally do it, because they use temporary files.

There is a simple way to do it without creating any file though. You just have to put files directly to Zip::OutputStream and then read from it. Btw pay attention: you must rewind the stream before reading it.


# some files objects

def download(files)

  zip_stream = Zip::OutputStream.write_buffer do |zip|

    files.each.with_index(1) do |file, index|

     # file name must be uniq in archive

      zip.put_next_entry("#{file.name}--#{index}.#{file.extension}")

      zip.write(file.read.force_encoding('utf-8'))

    end

  end

  # important - rewind the steam

  zip_stream.rewind

  send_data zip_stream.read, 

            type: 'application/zip', 

            disposition: 'attachment', 

            filename: 'files-archive.zip'

end

TILDecember 28, 2018by Alexander Spitsyn

How to implement inheritance in Grape resources?

Grape uses specific DSL to define endpoints in API, that’s why you can’t use base class’ instance methods in descendant resources. But there’s one trick:

There’s an #inherited class method in Ruby which is triggered every time some class inherits from ancestor class. It passes one argument - descendant class. Calling descendant’s #instance_eval method we can place any useful stuff inside a block: methods, helpers, before-do’s, etc, in this way evaluating it in context of subclass.


class Base < Grape::API

  def self.inherited(subclass)

    super



    subclass.instance_eval do

      helpers do

        def current_user

          @current_user ||= User.find(params[:user_id])

        end

      end

      # ...

    end 

  end

end



class DocumentsResource < Base

  post '/documents' do

    @document = current_user.documents.build

    # ...

  end

end

Note that it’s not real inheritance because Base class has not methods defined inside subclass#instance_eval block.

LongreadsMay 07, 2018by Dmitry Radionov

Active-support: Outside & Inside

Research of rails standard library ActiveSupport. Realization review and some useful functions.
Read more
LongreadsMarch 01, 2018by Eugene Komissarov

Protocol OAuth2: let’s play with Doorkeeper & Omniauth/OAuth2. Part 1.

Know your sandbox: shallow dive into OAuth2 protocol with Rails and Doorkeeper.
Read more
LongreadsFebruary 20, 2018by Igor Alexandrov

Ruby string literals VS Value objects. Overengineering?

A guide on how to get rid of string literals in Ruby with Rails 5 Attributes API
Read more
LongreadsFebruary 05, 2018by Dmitry Grushko

How to automate your workflow with VS Code?

How unified code helps us increase the efficiency of our team and what we use to write it.
Read more
LongreadsJanuary 29, 2018by Dmitry Voronov

Dry-rb and Trailblazer Reform

Why do we use dry-rb and Trailblazer Reform on the backend?
Read more
LongreadsJanuary 22, 2018by Igor Alexandrov

Rails 5 attributes API, value objects and JSONB

A guide on how to use value objects in your Ruby on Rails applications with PostgreSQL JSONB.
Read more