Sending raw JSON data to a member action in a controller spec

This is what worked for me in a Rails 4:

# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json

HTML5: Allow to choose multiple files with a vanilla file picker

Modern browsers natively suppport file pickers that allow the user to choose multiple files at once. To activate this feature, set the multiple attribute:

<input type="file" name="images[]" multiple />

Or in a Rails view:

<%= file_field_tag "images[]", multiple: true %>

This works in IE10+.

Make sure that the field name ends in [] so your server-side code will parse the incoming files into an array. Obviously this naming convention is not compatible with default Rails nested attribute setters, so you'll need to write a form ...

Tagging in Rails 4 using Postgres Arrays

Usage:

class Document < ActiveRecord::Base
  scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) }
  scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) }
end

Document.create(title: "PostgreSQL", tags: ["pg","rails"])

Document.any_tags('pg')
Document.all_tags(['pg', 'rails'])

Migration:

class CreateDocuments < ActiveRecord::Migration
  def change
    create_table :documents do |t|
      t.string :title
      t.string :tags, array: true, default: []
      t.timestamps
    end
    add_index  :documents, :ta...

Iterate over any enumerable with an index

tl;dr: Use with_index


ActiveRecord's find_each with index

If you do not provide a block to find_each, it will return an Enumerator for chaining with other methods:

Person.find_each.with_index do |person, index|
  person.award_trophy(index + 1)
end

Ruby's map with index

Similarly, you may need an index when using other methods, like map, flat_map, detect (when you need the index for detection), or similar. Here is an example for map:

people...

How to fix: Font too small when reading e-mails in Thunderbird

In Thunderbird, you can set custom font faces and sizes for reading plain-text e-mails. However, Thunderbird sometimes "randomly" does not respect your choices.

This is actually not a bug, but a rather weird feature: Fonts are defined per encoding of e-mails.

Some e-mails will be considered Unicode, some Western (ISO 8859-1), and some maybe yet another encoding.

The advanced font settings dialog by default just opens on "Western". Choose a different encoding from the "Fonts for" dropdown menu and you'll see that your custom font sett...

Backup an entire website using wget

You need to copy an entire website? Use wget like this:

wget -kr http://host.tld/

This will fetch all content (-r for recursive) and rewrite links inside the documents to make them suitable for local viewing (-k).

pdfkit/wkhtmltopdf: When a header is invisible

If you're using the :header_html option in PDFKit (or the corresponding --header-html option in wkhtmltopdf), and the header remains invisible, you need to add this to your header HTML:

<!doctype html>

The same applies to footers via footer_html

I'm sorry.

Rails 4.1+: New locale files require a Spring restart

So you want to organize your I18n using multiple .yml files but your Rails 4.1 application simply won't use any extra files in development? Spring is to blame.

Halt spring by running:

spring stop

The next time you spawn a bin/rails console or similar, your new translations will work.

You will need to repeat the above every time you create a new .yml file or rename existing ones.

:(

Use a Bash function to alias the rake command to Spring binstubs or "bundle exec" fallback

There are different ways to run rake:

  • On Rails 4.1+ projects, you have Spring and its binstubs which dramatically improve boot-up time for Rake and similar. You need to run bin/rake to use them.
  • On older projects, you want to run "bundle exec rake" to avoid those ugly "already activated rake x.y.z" errors that hit you when different rake versions are installed for your current Ruby.

Here is a solution that gives you a plain rake command which uses a binstubbed bin/rake if available and falls back to bundle exec rake if necessar...

Linux: Running a program with a different locale than your default

When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.

I will use the date command and print the current weekday, just for the sake of an example.

Changing the locale using environment variables

Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.

$ date +%...

How Exchange handles multipart/alternative emails

In Rails, you can very easily send emails with HTML and plaintext bodies.

However, if you're trying to debug those using your normal email account, you might be out of luck: For some reason, Exchange servers will simply throw away the plaintext part of your mail, and just save the html part.

Change the existing order of an ActiveRecord scope

Use reorder to replace an existing order clause with a new expression.

Using rbenv on Ubuntu 18.04+

We will be installing rbenv and ruby-build from our own fork, not from the Ubuntu sources.

Installing rbenv

  1. Install rbenv:

    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
    

    For Bash:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    

    For ZSH:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(rbenv init -)"' >> ~/.zshrc
    

    Now reinitialize ...

Perform Sidekiq jobs immediately in development

# config/initializers/sidekiq.rb

# Perform Sidekiq jobs immediately in development,
# so you don't have to run a separate process.
# You'll also benefit from code reloading.
if Rails.env.development?
  require 'sidekiq/testing'
  Sidekiq::Testing.inline!
end

RSpec example groups can be named using symbols

Though nowhere to be found in the official docs, this works just fine.

describe Facebook::Post do
  it_behaves_like :time_series
end

shared_examples_for :time_series do
  # shared example code
end

Upgrade to LibreOffice 5.x on Ubuntu

Getting rid of your old LibreOffice

Remove your old LibreOffice:

sudo apt-get remove libreoffice*

You probably also want to get rid of the dead PPAs you might have installed for LibreOffice 4.x:

  • Open *Ubuntu Software Center
  • Go to Edit / Software Source ... / Other Software
  • Find and uncheck lines like http://ppa.launchpad.net/libreoffice/libreoffice-4-3/ubuntu. Note that you will probably have at least two lines pertaining to libreoffice.

Installing a new LibreOffice
-------------...

Git: Diff per word or character

By default git diff highlights whole lines as changes.

To diff on a word-by-word basis you can say:

git diff --color-words

To diff on a character-by-character basis you can say:

git diff --color-words=.