Cucumber: Removing uploaded files after test runs

Cucumber will clean up files you've uploaded in your Cucumber features automatically with the attached code. Put the file in features/support/.

RSpec matcher "be_sorted"

RSpec::Matchers.define :be_naturally_sorted do
  match do |array|
    array == array.natural_sort
  end
end

See RSpec: Where to put custom matchers and other support code. To use natural_sort, see this card.


This matcher is now part of Spreewald.

Cucumber step to test whether a select field is sorted

This step will pass if the specified select is sorted.

Then /^the "(.*?)" select should be sorted$/ do |label, negate|
  select = find_field(label)
  options = select.all('option').reject { |o| o.value.nil? }

  options.collect(&:text).each_cons(2) do |a,b|
    (a.text.downcase <=> b.text.downcase).should <= 0
  end
end

In conjunction with this custom matcher, the each_cons block can be replaced with:

options.should be_naturally_sorted

Usage

Then the "Organizations" select should be sorted...

English words that you cannot use in Coffeescript

Today in computer: In Coffeescript, on and yes are aliases for true. off and no are aliases for false.

Defining variables or functions with such a name will give you errors like this:

reserved word "true" can't be assigned
reserved word "false" can't be assigned

This will not be fixed.

Show backtrace for all Sidekiq threads

It might happen that your Sidekiq queue gets stuck, hanging at 0% CPU load.

When we inspected the process using strace we saw a blocking select system call.

You can get backtraces for each thread by sending the TTIN signal to the Sidekiq process like this:

kill -TTIN $process_id

Angular: Keeping attributes with invalid values in an ngModel

The Angular 1.2 way:

# By default, angular returns undefined for invalid attributes which removes
# the value from the form field's ngModel (which means it's not sent to the
# server, and old values would not be overwritten).
#
# This directive makes sure that form fields with an invalid value return an
# empty string instead of undefined.

for elementType in ['input', 'textarea', 'select']
  @app.directive elementType, ->

    priority: 1
    restrict: 'E'
    require: '?ngModel'

    link: (scope, element, attributes, ngModelControlle...

postgresql: Get all values for an enum

SELECT enum_range(NULL::portal) # Returns an array of all possible values
SELECT unnest(enum_range(NULL::portal)) # Unnests the array and returns a row for each value

Whereas portal is the enum type.

Heads up: LibreOffice Calc adds quotation marks when copying data from multi-line cells

Copy data from LibreOffice Calc

When you copy data from multi-line cells in LibreOffice Calc, quotation marks are automatically added, which you may not want.

For example with license keys:

09:46:24 [✔] pascal:~> icdiff license license_copy 
license                           license_copy                           
===== LICENSE BEGIN =====         "===== LICENSE BEGIN =====             
00000yIeXfhGSbt"yULWQR9n          00000yIeXfhGSbt""yULWQR9n        
olysFAv105bHmKOiqbxRX"Yr  ...

How to fix: "git diff" does not show umlauts (or other non-ASCII characters) properly

When using git diff, you might encounter weird characters where umlauts (or any other UTF-8) characters should be. It looks like this:

R<C3><BC>ckg<C3><A4>ngig # should be "Rückgängig"

However, not Git is to blame but the less command that Git uses to page your diff output.

You need to tell less to use UTF-8 (otherwise it tries to convert multibyte chars like "ü" which is represented by 2 bytes C3 and BC).

$ LESSCHARSET=UT...

Dynamic conditions for belongs_to, has_many and has_one associations

Note: Consider not doing this. Use form models or vanilla methods instead.


The :conditions option for Rails associations cannot take a lambda. This makes it hard to define conditions that must be evaluated at runtime, e.g. if the condition refers to the current date or other attributes.

A hack to fix this is to use faux string interpolation in a single-quoted :conditions string:

class User < ActiveRecord::Base
  has_many :contracts
  has_one :current_contract, :class_name => 'Contract', :conditions => '...

Underscore / LoDash: How to extend (or merge) into a new object

When using _.extend/_.assign or _.merge, you will modify the destination object.

object1 = { foo: 23, bar: 42 }
object2 = { bar: 99 }

_.extend(object1, object2) // => { foo: 23, bar: 99 }
object1 // => { foo: 23, bar: 99 }

If you do not want to do that, simply extend into a new object:

object1 = { foo: 23, bar: 42 }
object2 = { bar: 99 }

_.extend({}, object1, object2) // => { foo: 23, bar: 99 }
object1 // => { foo: 23, bar: 42 }

Note how our _.extend statement receives {} as its fir...

Managing vendor libraries with the Rails asset pipeline

The benefit of the Rails asset pipeline is that it compiles your stylesheets and javascripts to a single file, respectively. However, the consequences are startling if you don't understand them. Among others, the raw asset pipeline requires you to have all your asset libraries in the same folder, which quickly becomes confusing as your set of assets grows. To overcome this, we have two different solutions.

Custom solution

We are using a custom workaround to keep library files apart in their own directories. To avoid b...