Using merge on join for records filtering

# Returns all the accounts that have unread messages.
  def self.with_unread_messages
    joins(:messages).merge( Message.unread )

Bash shortcuts

CTRL-A Перемещение курсора в начало строки
CTRL-E Перемещение курсора в конец строки
CTRL-R Поиск по истории
CTRL-W Вырезать последнее слово
CTRL-U Вырезать всё до курсора
CTRL-K Вырезать всё после курсора
CTRL-Y Вернуть последнюю вырезанную строку
CTRL-_ Отмена
CTRL-L Очистка экрана терминала

Replace old rocket style ruby hash syntax with ruby-1.9 style bash script

find . -name \*.rb -exec perl -p -i -e 's/([^:]):(\w+)\s*=>/\1\2:/g' {} \;

Enchanted git log

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"

Hide file which is showing despite of gitignore

git update-index --assume-unchanged FILEPATH

Paste image into prawn pdf with StringIO and base-64 encoding

I getting BASE64_IMAGE_GOES_HERE from signature_pad js lib with getSignatureImage() method.

  def render
    pdf = Prawn::Document
    pdf.image[:data])), at: [10, cursor - 50], width: 200, height: 125

  def splitBase64(uri)
    if uri.match(%r{^data:(.*?);(.*?),(.*)$})
      return {
        type:      $1, # "image/png"
        encoder:   $2, # "base64"
        data:      $3, # data string
        extension: $1.split('/')[1] # "png"

Multi-column PDF generating with Prawn in ruby

For example, you need to generate a content (or table) with width 20-30% of page and it should not break the page if height is too big but should continue on the same page in the right side.

It will look like that:

Prawn has very useful methodfor doing that called ‘column_box’.

# Example of 2 columns table with some styling
def render_left_products_list
    column_box([-5, cursor], columns: 2, height: cursor, position: :left, width: bounds.width, spacer: 40) do

      @categories.each do |category|
        move_down 6
        table line_item_rows(category), position: :left, cell_style: { size: 7, text_color: "000000", borders: [:bottom], border_lines: [:dotted], padding: [2, 0, 4, 0] } do
          column(1).style align: :right
          style(row(0), text_color: HEADER_COLOR)
          align                = { 0 => :left, 1 => :right}
          self.width           = 285
          self.header          = true
          column(1).font_style = :bold
          row(0).size          = 8
          row(0).font_style    = :bold
          row(0).borders       = []

  def line_item_rows(category)
    [[, '']] + do |product|
      [, "#{format('%.2f', product.price)} USD"]