PostgreSQL: Ordering, NULLs, and indexes

When using ORDER BY "column" in PostgreSQL, NULL values will come last.

When using ORDER BY "column" DESC, NULLs will come first. This is often not useful.

Luckily, you can tell PostgeSQL where you want your NULLs, by saying

... ORDER BY "column" DESC NULLS LAST
... ORDER BY "column" ASC NULLS FIRST

Your indexes will have to specify this as well. In Rails, declare them using

add_index :table, :column, order: { column: 'DESC NULLS LAST' }

Multiple columns

When sorting by multiple columns, yo...

How to disable auto-complete on login forms

Disabling auto-complete in login forms is probably a bad idea, since it encourages weak passwords.

If you are still forced to implement this (maybe due to legal or policy requirements), this is how:

Prevent browsers from saving the password in the first place. Disabling autocomplete does not improve security.

How to prevent password saving:

To prevent the browser from saving passwords (and usernames), you need to:

  • copy username and password to hidden form fields before submitting the login form
  • c...

SmartUnderline

SmartUnderline is an open-source JavaScript library which uses clever tricks to draw underlines in a more beautiful and readable way.

We've not yet put this into a project, but its effect is very pretty. Please update this card when you use it.

Use Capybara commands inside an IFRAME

If you need to follow links, click buttons, etc. using Capybara inside an <iframe>, you can do it like this:

page.within_frame('iframe-id') do
  fill_in 'E-mail', with: 'foo@bar.com'
  fill_in 'Password', with: 'secret'
  click_button 'Submit'
end

Instead of the frame's [id] attribute you may also pass a Capybara::Node for an <iframe>.

If you're also using Cucumber you could make a meta-step like this:

When /^(.*?) inside the (.*?) frame$/ do |step_text, frame_id|
  page.within_frame(frame_id) do
    s...

Faster debugging with RubyMine macros

In my RubyMine I have recorded two macros for debugging and linked them to some keyboard shortcuts. Since I believe everyone could benefit from having those I wanted to share this.

The first one simply inserts

binding.pry

and the second one

.tap { |object| binding.pry }

for when you do not have a reference to the object you want to inspect.

In order to record a macro you simply follow the path Edit > Macros > Start Macro Recording.

Then you simply type binding.pry or whatever you want to record and stop recor...

ZenTest "invalid gemspec" / "Illformed requirement"

Today I ran into this:

Invalid gemspec in [/usr/local/rvm/gems/ruby-1.9.3-p194/specifications/ZenTest-4.9.3.gemspec]: Illformed requirement ["< 2.1, >= 1.8"].

You need a newer Rubygems version. Try this: gem update --system 1.8.29

Copy & Paste & The Web | CSS-Tricks

Insanely detailled guide about controlling copy & paste behavior using web technology in 2015.

Note that you can now trigger a copy action through Javascript, no Flash required.

Fix: Capybara is very slow when filling out fields in large forms

In large forms (30+ controls) new Capybara version become [extremely slow] when filling out fields. It takes several seconds per input. The reason for this is that Capybara generates a huge slow XPath expression to find the field.

The attached code patches fill_in with a much faster implementation. It's a dirty fix and probably does a lot less than Capybara's own fill_in so don't use it unless you are having problems with test suites that are unusable because of this...

input: A DOM event that is fired whenever a text field changes

If you're supporting IE9+, you can listen to input to see if a text field changes.

Other than change, it fires while the user is typing and doesn't wait until the user blurs the field.

Older workarounds included polling the field every X ms to see if its value changed.

Unfortunately input is not triggered for check boxes.

When should you use DateTime and when should you use Time?

The differences are subtle. You probably want to use Time, except when you want to use DateTime. See the attached article for details.

Rails 3 ActiveRecord::Persistence#becomes does not copy changed attributes

Note: ActiveRecord::Base#becomes has a lot of quirks and inconsistent behavior. You probably want to use ActiveType.cast instead.


This issue will be encountered when relying on attribute_was methods of ActiveModel::Dirty after casting a model which has defaults to a form model, for example.

In my case a record with an assignable_values legacy value beca...

Rails 4.1+ automatically detects the :inverse_of an association

Starting from 4.1, Rails automatically detects the inverse of an association, based on heuristics. Unfortunately, it does not seem to notify you when it fails to infer the :inverse_of, so you are better off to always manually set :inverse_of anyway.

Note that automatic inverse detection only works on has_many, has_one, belongs_to associations. Extra options on the associations will prevent the association's...

High Performance Browser Networking: HTTP/2

HTTP/2 will make our applications faster, simpler, and more robust—a rare combination—by allowing us to undo many of the HTTP/1.1 workarounds previously done within our applications and address these concerns within the transport layer itself. Even better, it also opens up a number of entirely new opportunities to optimize our applications and improve performance!

HTTP/2 is here, let's optimize!

WebRTC HTTP/2 is here, let’s optimize! or, why (some) yesterday's best-practices are today's HTTP/2 anti-patterns.

Material icons - Google Design

Surprisingly exhaustive new icon set by Google.

Available as PNG, SVG and as a icon font.

Comment from Henning

I tried using the icon set in a project. I found the quality, selection and handling far worse than what we are used to in FontAwesome.

Exception Notifier: Foreground vs. background sections

Since version 2.6 exception notifier distinguishes between foreground and background sections. The reason is that with background jobs (e.g. methods that are called by a cron job) some variables are not available for exception notifier, e.g. @request and @kontroller.
Therefore you can configure foreground and background sections individually. Our default settings are documented in Get notified when your application raises an error.

**W...

Understanding Ruby Singleton Classes

Good article about ruby singleton classes.

Querying model errors in Rails 4

ActiveModel supplies an errors object that behaves similar to a Hash. It can be used to add errors to a record, as well as to query the record for registered errors. This object is returned when calling <object>.errors:

errors = @user.errors # => #<ActiveModel::Errors ...>

Here are some helpful messages of its API:


[<attribute name>]

Returns an array of error messages on that attribute. Example: errors[:name] => ['is missing']


add_on_blank(<attribute list>) (similarly add_on_empty)

Registers an error ...

Reverse-proxying web applications with Apache 2.4+

Note: Making a reverse proxy with nginx is much more straightforward.


A reverse proxy is a "man in the middle" server that tunnels requests to another server. You can use for things like:

  • Expose a local service that you cannot directly reach over the internet
  • "Change" the domain or path of a web application by rewriting them on the fly
  • Instantly change servers that respond to a name or ...

Gatekeeping: Guide for gatekeeper

If you're responsible for gatekeeping in a projects, here is a guide, what to do.
In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.

Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger teams), we think it is a helpful starting point.


First, read the [Gatekeeping for developers](https://makandracards.com/makandra/6579-gatekeeping-guide-for...

Manually trigger a delegated DOM event

When you register a delegated event using on (or the deprecated delegate / live), it is somewhat hard to manually trigger these events manually, e.g. for testing.

After trying jQuery's trigger to no avail, I had success by using native Javascript methods to create and dispatch an event. For instance, to trigger a mousedown event:

element = $('...').get(0);
event = new MouseEvent('mousedown', { view: window, cancelable: true, bubbles: true }...

Show details of TLS/SSL connections of remote hosts

sslscan is a nice tool to show details about TLS/SSL connections:

~> sslscan some-host-at.makandra.de

Testing SSL server some-host-at.makandra.de on port 443

  Supported Server Cipher(s):
    Failed    SSLv3  256 bits  ECDHE-RSA-AES256-GCM-SHA384
    Failed    SSLv3  256 bits  ECDHE-ECDSA-AES256-GCM-SHA384
    Failed    SSLv3  256 bits  ECDHE-RSA-AES256-SHA384
    Failed    SSLv3  256 bits  ECDHE-ECDSA-AES256-SHA384
    Rejected  SSLv3  256 bits  ECDHE-RSA-AES256-SHA
...

  Prefered Server Cipher(s):
    TLSv1  128 bits  ECDHE-RSA-A...

Upgrading from Capistrano 2 to 3

Capistrano 3 is a major rework of the framework and requires several adjustments to your deploy configuration files. The biggest change is that they moved away from their custom DSL and use Rake instead. For connecting with and operating on the servers, they bring a new gem SSHKit which does the heavy lifting. It's SSHKit's DSL that is used anywhere inside the Rake tasks. See #Resources at the bottom for examples.

Step 1: Upgrade guide

For migration from 2 to 3, follow this tutorial: [Capistrano 3 Upgrade Guide](https://semaphorec...

CucumberFactory 1.11 lets you use FactoryGirl traits

If you have FactoryGirl traits like this:

factory :movie do

  title 'Sunshine'
  year 2007
  
  trait :vintage do
    year 1951
  end
  
  trait :moody do
    title 'Interstellar'
  end

end

You can now call them from Cucumber Factory like this:

Given there is a movie (vintage, moody)