View

How to fix broken font collisions in wkhtmltopdf

If you are using PDFKit / wkhtmltopdf, you might as well want to use custom fonts in your stylesheets. Usually this should not be a problem, but at times they include misleading Meta-information that leads to a strange error in the PDF.

The setup

  • The designer gave you two fonts named something like BrandonText-Regular and BrandonText-Bold. (With flawed Meta-information)
  • You have a HTML string to be rendered by PDFKit
  • For demonstration purposes, this strin…
External contentRepeats

Thread-safe collections in Ruby

When using threads, you must make your code thread-safe. This can be done by either locking (mutexes) all data shared between threads, or by only using immutable data structures. Ruby core classes like String or Array are not immutable.

There's several gems providing thread-safe collection classes in Ruby.

thread_safe

The concurrent-ruby gem provides thread-safe versions of Array and Hash:

```
sa = Concurrent::Array.new # supports standard Array.new forms
sh = Concurre…

How to add a custom dictionary to Ruby Mine

  1. Download the dictionary from http://www.winedt.org/dict.html, e.g. http://www.winedt.org/dict/de_neu.zip
  2. unzip de_neu.zip
  3. mkdir ~/Documents/dic
  4. iconv -f UTF-16 -t UTF-8 de_neu.dic -o ~/Documents/dic/de_neu_utf8.dic
  5. In RubyMine: Go to Settings (CTRL + ALT + S) > Editor > Spelling > Dictionaries and add the folder ~/Documents/dic
External content

A Theme Switcher

Hack to implement an inverted "night mode" theme with a few lines of CSS.

Colors in images are preserved.

Association to polymorphic model does not determine inverse_of automatically

You need to set the :inverse_of option manually for relations that have an association to a polymorphic model. Otherwise you will not be able to save a record with a nested polymorphic association.

class Event < ApplicationRecord
  has_many :letters, as: :record
end

class Letter < ApplicationRecord
  belongs_to :record, polymorphic: true
end

```
event = Event.new.letters.build
event.save! # => ActiveRecord::Re…

Repeats

Project management best practices: Standup

If the project team consists of at least 2 members, do a daily standup. It should not take much longer than 15 minutes.

Format

Tell everyone else

  • what you did yesterday
  • what you intend to do today
  • where you might need help or other input
  • if there are new developments everyone needs to know about

A "still working on X, will probably be done today" is totally fine. No need to tell a long story.

If you are out of work, find a new story with the others.

If there a new stories in the backlog, look at them and

  • make sure ever…
Auto-destruct in 25 days

Updated: Your database tables should always have timestamps

For > Rails 4.2 you need to set the timestamps in the migration. Otherwise you will see:

ERROR: column "created_at" contains null values

This can be missed in development where you might have an empty table.

```
class AddMissingTimestamps < ActiveRecord::Migration
def change
add_timestamps :shows, null: true

update "UPDATE shows SET created_at = '#{Time.current}'"
update "UPDATE shows SET updated_at = '#{Time.current}'"

change_column_null :shows, :created_at, false
change_column_null :shows, :updated_at, fa...
Repeats

Simulate limited bandwidth

When testing mobile applications, it is sometimes helpful to simulate limited bandwith.

Chrome

  • Open the dev tools (CTRL+SHIFT+J or F12)
  • Open the network tab
  • In the row below the dev tool tabs, there's a throttling dropdown

Note that there is currently no way to limit the upload bandwidth.

Firefox

There is a utility called "trickle" that allows you to do limit the bandwidth available to a process.

Simply do

trickle -d 50 -u 20 firefox

to start a firefox with only 50 kB/s download and 20 kB/s uplo…

How to move all files in a folder to a new subfolder

Let's say you have a folder images and want to to move all files in there to a new subfolder public.

cd images
mkdir public
mv !(public) public

The !(public) excludes the new subfolder itself from being moved.

External content

Bundler 2 will rename Gemfile and Gemfile.lock

TL;DR: Bundler 2.0 will rename Gemfile to gems.rb and Gemfile.lock to gems.locked (sic).

The old filenames will be supported until the release of Bundler 3.0.

Some more breaking changes here.

Repeats

Rails: namespacing models with table_name_prefix instead of table_name

When you want to group rails models of a logical context, namespaces are your friend. However, if you have a lot of classes in the same namespace it might be tedious to specify the table name for each class seperately:

class Accounting::Invoice < ApplicationRecord
  self.table_name = 'accounting_invoices'
  ...
end

class Accounting::Payment < ApplicationRecord
  self.table_name = 'accounting_payments'
  ...
end

A replacement for the self.table_name-assignment is the table_name_prefix in the module definition:

```
module Accou…

Repeats

Your Cronjobs should not rely on a perfect schedule

Due to network or hardware failures, it can happen that one of your cronjobs will not run at the time you specify in the schedule. Your code should be built in a way that it can be re-run at a later time (when the failure is resolved).

For example, if you are synchronizing data with another service once every day, your cronjob should not only synchronize changes from the last 24 hours. If you do this and a network failure will delay the execution of your job by 5 hours, you will only synchronize changes from hour 6-29, but forget change…

Auto-destruct in 29 days

Updated: How to change the locale of a PostgreSQL cluster

  • Added instructions on backing up and restoring entire cluster data.
  • Card has been rewritten a bit.
External contentRepeats

Linux Performance Analysis in 60,000 Milliseconds

You login to a Linux server with a performance issue: what do you check in the first minute?

uptime
dmesg | tail
vmstat 1
mpstat -P ALL 1
pidstat 1
iostat -xz 1
free -m
sar -n DEV 1
sar -n TCP,ETCP 1
top
External content

How to exploit websites that include user input in their CSS

The linked article shows how to exploit websites that include unsanitized user input in their CSS.

Although the article often mentions React and CSS-in-JS libraries, the methods are applicable to any web app that injects user input into style tags or properties.

Also, sanitizing user input for CSS injection is much harder than sanitizing HTML.

Auto-destruct in 18 days

Updated: Rails: When to use :inverse_of in has_many, has_one or belongs_to associations

I have updated the section about Rails >= 4. We should always check manually if inverse_of is set correctly when using options in has_many, belongs_to and has_one. Or just set it always if we use any option, so we do not run into issues some days later and have to investigate time into this.

Repeats

RSpec: How to define classes for specs

RSpec allows defining methods inside describe/context blocks which will only exist inside them.
However, classes (or any constants, for that matter) will not be affected by this. If you define them in your specs, they will exist globally. This is because of how RSpec works (short story: instance_eval).

describe Notifier do
  class TestRecord
    # ...
  end
  
  let(:record) { TestRecord.new }
  
  it { ... }
end

# TestRecord will exist here, outside of the spec!

This will come bite you at least when you try to define a clas…

Repeats

How Ruby method lookup works

When you call a method on an object, Ruby looks for the implementation of that method. It looks in the following places and uses the first implementation it finds:

  1. Methods from the object's singleton class (an unnamed class that only exists for that object)
  2. Methods from prepended modules (Ruby 2.0+ feature)
  3. Methods from the object's class
  4. Methods from included modules
  5. Methods from the class hierarchy (superclass and its ancestors)

Example

Let's say we h…

View
3390 cards