3505 cards
View
Repeats

Calculate values for mixed length units

calc() lets you mix CSS units. Ever wanted to give an element "the container's width minus 20px on each side"? Here you go:

width: calc(100% - (20px * 2));

Supported by all modern browsers and IE9+.

Pivotal Tracker now supports story description templates

When writing a story description in Pivotal Tracker, there is now a tiny button at the bottom of the description field where you can paste a template.

You can define your own templates by following "More" at the top, then "Templates".
We recommend you define a template following our suggested story description.

Note that the three default templates currently can not be removed or changed.

External contentDeprecated

Almost complete guide to flexbox (without flexbox)

Flexbox is great, but you don't get reliable support in Internet Explorer.

The attached article shows how to accomplish many Flexboxish effects without actually using flexbox statements.

Do not use "flex: 1" inside "flex-direction: column" when you need to support IE11

Flexbox is awesome. Most of it even works in IE11, but flex: 1 won't work reliably in Internet Explorer.

Example

Consider the following HTML and CSS.

<div class="container">
  <div class="child">
    foo
  </div>
  <div class="bar">
    bar
  </div>
</div>
.container {
  display: flex;
  flex-direction: column;
}

.child {
  flex: 1;
}

See it in action at Plunker.

Background

flex: 1 is a shortcut to flex-grow: 1 + flex-shrink: 1 + flex-basis: 0. A ba…

Auto-destruct in 43 days

Updated: How to test your website for different versions of Internet Explorer or Edge

  • Updated link to Microsoft's VirtualBox images
  • Removed IETester method
  • Wrote about BrowserStack
Repeats

Hints for debugging MySQL InnoDB deadlocks

Deadlocks only occur if two transactions in separate threads compete for the same rows in the database. They usually (but not necessarily) only happen when trying to update or otherwise lock several rows in different order.

Solving deadlocks is potentially complicated, so here are a few pointers:

  • MySQL should always detect the deadlock right when it happens, and will throw an error to one of the offending threads. This error states the SQL statement that this thread was currently waiting for, and that tried to acquire one of the competin…
Repeats

Always, always declare your associations with symbols

Never ever declare your associations with a string, especially when doing metaprogramming. A common mistake is something like

# WRONG
class Page < ActiveRecord::Base
  %w[main sub].each do |type|
    belongs_to "#{type}_title"
  end
end

Always append a to_sym, otherwise you'll have all kinds [of fun](/makandra/11601-unsa…

Rails < 5: How to get after_commit callbacks fired in tests

If you use transactional_fixtures or the database_cleaner gem with strategy :transaction, after_commit callbacks will not be fired in your tests.

Rails 5

Rails 5 has a fix for this issue and no further action is needed.

Rails 3+

Add the gem test_after_commit to your test group in the Gemfile and you are done. You don't need to change the database strategy to deletion (which might lead to longer test runs).

Note:

Most …

Repeats

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

Rails >= 4

Starting from Rails 4 the inverse_of is tried to be determined automatically.

  1. If you don't use options setting the inverse_of is not needed.
  2. If the inverse is wrong or missing because of some options, you need to set the inverse_of manually.

Our recommendation is to always set inverse of if you use any option. Still you can find out if the inverse_of is set correctly:

```
Forum.reflect_on_association(:posts).has_inverse? # => false # missing and needs to be set manually
Forum.reflect_on_association(:p…

Project management best practices: The story tracker

In general, the tracker should always be the definitive source of truth of what needs to be done as part of a project. If you identify a task that needs to be done, you should create a story. If you learn something that contradicts an existing story, change it.

The tracker can contain two types of stories: Developer stories, and non-developer stories.

Non-developer stories

Non-developer stories should be clearly marked. They usually belong to the PM (or maybe people from the operations team). Those story can take all forms, could just…

Repeats

Collect all values for a given column in an ActiveRecord scope

The Edge Rider gem allows you to collect an array of values for one field of your ActiveRecord elements:

>> User.active.collect_column(:id)
=> [1, 5, 23, 42]

It also comes with a :distinct option in case you are only interested in unique values:

>> Article.collect_column(:state, distinct: true)
=> ['draft', 'published']

In modern Railses (since 3.2) you can also use ActiveRecord's pluck met…

Repeats

Rails: How to use prepend to avoid monkey patches in modules

Let's say you have a gem which has the following module:

module SuperClient

  def self.foo
    'Foo'
  end
  
  def bar
    'Bar'
  end

end

For reasons you need to override foo and bar.

Keep in mind: Your code quality is getting worse with with each prepend (other developers are not happy to find many library extensions). Try to avoid it if possible.

  1. Add a lib/ext/super_client.rb to your project (Folder or file needs to be required in an initializer)
  2. Add the extension, which overrides both methods (prepend i…

HTML file inputs support picking directories

HTML's <input type="file"> accepts a single file. You can allow multiple files via <input type="file" multiple>.
But sometimes, selecting multiple files is not enough and can be cumbersome for the user. Enter webkitdirectory:

<input type="file" webkitdirectory multiple>

Using webkitdirectory switches the browser's file picker to select a directory. All files inside that directory, and inside any nested subdirectories, will be selected for the file input.

This can be useful when users want to upload all files from a nested dire…

Repeats

Ruby: __FILE__, __dir__ and symlinks

Ruby's __FILE__ keyword returns the path to the current file. On popular for this are Ruby binaries:

#!/usr/bin/env ruby
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'my_cli'
MyCli.run!

However, if you create a symlink to this file, this will no longer work. __FILE__ will resolve to the path of the symlink, not to its target.

One solution is to use File.realpath(__FILE__).

In Ruby 2+ you can also use this:

$LOAD_PATH << File.expand_path('../lib', __dir__)

__dir__ is simply a shortcut for `Fi…

Rails: How to find out which model validation failed

If validations failed for a record, and you want to find out if a specific one failed, you can do this via Rails.
You should rarely need this, as your error messages are usually good enough to display all necessary information to users.

As an example, consider the following model which uses two validations on the email attribute.

class User < ApplicationRecord
  validates :email, presence: true, uniqueness: true
end

And let's assume we have a blank user:

user = User.new
user.valid? # => false

The record's errors then…

External content

The Definitive Guide to Cookie Domains

Restricting access to cookies is essential for security in many web apps. For example, the session ID, the secret token used to identify a particular session, is typically stored in a cookie. Cookies have several important settings. Previously, I discussed the secure flag. This time, let’s dive into the cookie domain.

The cookie domain is an important security feature, probably even more important than the secure flag. It tells the browser that this cookie must only be sent to matching domains. Matching however, can happen in several w…

Repeats

Nested ActiveRecord transaction pitfalls

When working with custom transactions you may encounter unexpected behaviour if you try to roll back your changes.

tl;dr

Not all databases support nested transactions. Therefore, Rails will sometimes silently ignore a nested transaction and simply reuse the other transaction. However, a ActiveRecord::Rollback within the nested transaction will be caught by the block of the nested transaction, but will be ignored by the outer transaction, and not cause a roll back!
To avoid this unexpected behaviour, you have to explicitly tell rai…

External contentRepeats

How to write a good changelog

We want to keep a changelog for all gems we maintain. There are some good practices for writing a changelog that adds value, please stick to these.

  • Add a notice to the README's contribute section about the changelog
  • For every release update the changelog
  • Note the date format yyyy-mm-tt

What is a changelog?

A changelog is a file which contains a curated, chronologically ordered list of notable changes for each version of a project.

Why keep a changelog?

To make it easier for users and…

This website uses cookies to improve usability and analyze traffic.
Accept or learn more