Beware of rails' reverse_order!

#reverse_order does not work with complex sorting constraints and may even silently create malformed SQL for rails < 5.

Take a look at this query which orders by the maximum of two columns:

Page.order('GREATEST(pages.published_from_de, pages.published_from_en) DESC').to_sql
# => SELECT "pages".* FROM "pages" ORDER BY GREATEST(pages.published_from_de, pages.published_from_en) DESC

Rails 4

Rails 4 will not immediately raise but creates malformed SQL when trying to use reverse_order on this query:

Pageorder('GRE...

Master the chrome history and autocomplete

1. Sometimes you have search entries in the autocomplete of the address bar, which are higher weighted than your bookmarks. Pressing SHIFT + DEL while searching removes them from the history immediately.

bootstrap.png


2. Sometimes you have search entries in the autocomplete of the address bar, which are all higher weighted than your recently visited sites. Add a search for your history, so you can get recent results first.

...

AngularJS Cheat Sheet (PDF)

This cheat sheet ... aims at providing a quick reference to
the most commonly used features in AngularJS.

Running awstats on a single logfile

AWstats is build to regularly run on webservers. If you want it to build a report once, here is the minimal configuration you need:

Put the following into the awstats config file (look into /etc/awstats/awstats.conf.local or look into /etc/awstats/awstats.conf how to do it on your system):

SiteDomain="yourdomain.de"
DirData="."
DNSLookup=0

Run the following to build a simple HTML page:

awstats -staticlinks -config="yourdomain.de"  -LogFile=your-logfile.log -output > report.html

This might take a second (it will take ...

Git error: "badTimezone: invalid author/committer line - bad time zone"

You might get the above error message when cloning certain git repositories (for example the rails repository). It indicates that there is a malformed timestamp in some commit, and your git installation is configured to validate it.

As a workaround, you can disable the validation using

git config --global fetch.fsckobjects false

This settings seems to be the default for most git installations anyways.

Pexels: Free stock photos

You can find great stock photos on pexels.com.

All pictures are free for personal and commercial use without attribution, and may be modified.

Detecting if a Ruby gem is loaded

Detect if a gem has been activated

A gem is activated if it is either in the current bundle (Gemfile.lock), or if you have manually activated it using Kernel#gem (old-school).

To detect if e.g. activerecord has been activated:

if Gem.loaded_specs.has_key?('activerecord')
  # ActiveRecord was activated
end

Detect if a particular gem version has been activated

To detect if e.g. activerecord ma...

Ubuntu Mate: Get rid of F12 drop-down terminal

To get your F12 key back for other shortcuts, stop Tilda:

killall tilda

To prevent Tilda from starting on boot you can remove it

sudo apt-get remove tilda

or disable auto start:

  • Go to Mate Control Center > Startup Applications
  • uncheck "Tilda"

makandra/gemika: Helpers for testing Ruby gems

We have released a new library Gemika to help test a gem against multiple versions of Ruby, gem dependencies and database types.

Here's what Gemika can give your test's development setup (all features are opt-in):

  • Test one codebase against multiple sets of gem dependency sets (e.g. Rails 4.2, Rails 5.0).
  • Test one codebase against multiple Ruby versions (e.g. Ruby 2.1.8, Ruby 2.3.1).
  • Test one codebase against multiple database types (currently MySQL or PostgreSQL).
  • Compute a matrix of all possib...

Fix "libmysqlclient.so.20: cannot open shared object file: No such file or directory"

This error can be caused by the mysql2 gem under mysterious circumstances. You need to remove it with gem uninstall mysql2 and then reinstall it (or just run bundle).

gem pristine mysql2 will not be enough.

Sass: How to convert an RGBA color to its RGB look-alike

Say you have an RGBA color that you need as a non-transparent color because of reasons.

Basically, this is possible. Just understand that you will convert your RGBA color for exactly one base background color as you are giving up transparency.
Most likely, your background is white, so you'll use #fff as that for examples below.

Simple approach

When your know the RGBA color's base RGB color (e.g. your brand color that you RGBA'd for some hover effect), you can simply use the mix function instead of rgba.

Before:

backgroun...

VCR fails if the same request is triggered multiple times

Same requests are recorded only once in vcr. Replaying a test fails, if you trigger the same request multiple times. The error message is somehow confusing, as your cassette contains the request:

An HTTP request has been made that VCR does not know how to handle

If you want to allow to match a request multiple times, you need to configure this explicit with allow_playback_repeats: true. Some exa...

Heads up: "localhost" may be IPv6 on new linuxes

I've encountered a Ubuntu 16.04 today, where localhost resolved to ::1 instead of 127.0.0.1.

This will not usually make a difference, but could be relevant for firewall policies.

What every coder should know about gamma

Good article about what "gamma correction" means for color processing, and what "sRGB" actually means.

You probably do not need to know this for web development, but it's an interesting read.

Migrations are versioned in Rails 5 | BigBinary Blog

Rails 5 migration classes look like this now:

class CreateUsers < ActiveRecord::Migration[5.0]

Mind the [5.0] at the end.

They do this to evolve the ActiveRecord::Migration API without breaking your historical migrations in db/migrate.

List of handy Chrome plugins

These are Chrome plugins that proved useful at makandra. Each is the best-in-class.

Dimensions

Auto-measure distances by moving your mouse.

Google Analytics Debugger

See details about Google Analytics events that you trigger on a page. Useful for debugging on production sites.

[JSON Formatter](https://chrome.google.com/webstore/detail/json-for...

Bundler returns different error codes depending on what went wrong

When you are calling Bundler from your shell scripts, you might find it useful that a failed bundle call returns a different error code depending on the type of problem.

A list of error codes can be found here.

Measuring sql query time of a piece of code using ActiveSupport::Notifications

ActiveSupport::Notifications provides an instrumentation API for Ruby. It is used throughout rails to publish instrumentation events that include information about each part of a request/response cycle.

Have a look at your application log file - yes, those are those events. The cool thing is that you can subscribe to those events.

There is also a convenience method that allows you to subscribe to those events only for the time of executing a block of code. Thus you can capture all sql queries that are triggered when executing your block....

How to disable material design in Google Chrome

A few releases back, Chrome started using a Material Design UI on desktop. If you don't like it (on some window managers or GTK themes it's ugly), you can disable it.

  1. Visit chrome://flags/
  2. Search (Ctrl+F) for "Material Design"
  3. Switch to "Non-Material"
  4. Restart Chrome

Chrome's default theme should now be pretty again.

Hack of the day: One-liner to run all changed Cucumber features

Similar to our snippet that runs all Cucumber features matching a given string, the following will run all modified or new Cucumber features by looking at your git status:

git status --short | grep -v '^ D ' | grep '.feature' | sed 's/.. //' | tr '\n' ' ' | xargs geordi cucumber

If you want to know what each of the above commands does, see [explainshell](http://explainshell.com/explain?cmd=git+status+--short+%7C+grep+-v+%27%5E+D+%27+%7C+grep+%27.feature%27+%7C+sed+%27s%2F..+%2F%2F%27+%7C+tr+%27%5Cn%27+%27+%27+%7C...

How to use Haml in your helpers

You know those helper methods that just render some HTML but look weird because of content_tags all over the place? You could also use Haml instead.

Example

Consider the following helper.

def greeting
  message = ''.html_safe
  message << 'Welcome to '
  message << content_tag(:span, Rails.env, class: 'greeting--location')
  content_tag :div, message, class: 'greeting'
end

That looks clumsy and is hard to read.

Wouldn't it be nicer to say something like this?

def greeting
  render_haml <<-HAML
 ...

Isolate Side Effects in Ruby

In this article, we’ll look at what side effects are, why they are problematic despite being necessary, and how to isolate them to minimise their drawbacks.

Minidusen: Low-tech record filtering with LIKE queries

We have a new gem Minidusen which extracts Dusen's query parsing and LIKE query functionality.

Minidusen can no longer index text in MySQL FULLTEXT columns, which was hardly used and didn't always help performance due to the cost of reindexing.

Minidusen is currently compatible with MySQL, PostgreSQL, Rails 3.2, Rails 4.2 and Rails 5.0.

Basic Usage

Our example will be a simple address book:

class Contact < ActiveRecord::Base
  validates_presence_of :name, :street, :city, :e...

We are no longer maintaining Dusen

If you were using Dusen for its query parsing and LIKE queries, we recommend to migrate to Minidusen, which extracts those parts from Dusen. Minidusen is compatible with MySQL, PostgreSQL and Rails 3.2, 4.2 and 5.0.

If you are looking for a full text indexing solution, we recommend to use PostgreSQL with pg_search.