Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

record a logstalgia video

Cause logstaglia is so cool you may want to record a video. We're lucky: Logstalgia has a parameter for an ppm-stream output: --output-ppm-stream FILE. We can pipe this to ffmpeg or avconv to record a h264 encoded video.

record command when using ffmpeg (for e.g. with ubuntu 12.04)

cat some_acces.log | logstalgia --sync -1920x1080 --output-ppm-stream - | ffmpeg -y -r 60 -f image2pipe -vcodec ppm -i - -vcodec libx264 -preset ultrafast -pix_fmt yuv420p -crf 1 -threads 0 -bf 0 logstalgia.mp4

record command when using avconv(…

IIFEs in Coffeescript

In Javascript often use Immediately Invoked Function Expessions (or IIFEs) to prevent local variables from bleeding into an outside scope:

(function() {
  var foo = "value"; // foo is scoped to this IIFE
})();

In Coffeescript an IIFE looks like this:

(->
  foo = "value" # foo is scoped to this IIFE
)()

There is also a shorthand syntax with do:

do ->
  foo = "value" # foo is scoped to this IIFE

You can also use do with arguments to c…

Repeats

Difference between respond_to/format and params[:format]

To return non-HTML responses (like XLS spreadsheets), we usually use the

respond_to do |format|
  format.xls do
    # send spreadsheet
  end
end

This is often, but not always the same as checking for params[:format] == :xls, so don't rely on this when e.g. one format checks for authorization and the other doesn't.

params[:format] is only set when a user explicitly puts a .xls at the end of the URL.

The format.xls block also responds when the user's browser requests the application/excel MIME type.

If Intern…

Useful tricks for logging debugging information to the browser console

During debugging you might pepper your code with lines like these:

console.log('foo = ' + foo + ', bar = ' + bar)

I recommend to use variable interpolation instead:

console.log('foo = %o, bar = %o', foo, bar)

This has some advantages:

  1. It's easier to write
  2. Variables are colored in the console output
  3. You don't need to stringify arguments so the + operator doesn't explode
  4. If the variable is a structured object like a D…
External contentRepeats

Vertical align anything with just 3 lines of CSS

Note: We have card with all CSS centering options. You probably want to head over there and get an overview over what techniques are available for your use case and browser requirements.


See attached link. The gist is:

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

Works in all web browsers and IE9+.

Auto-destruct in 58 days

Updated: Jasmine: Testing complex types for equality

Added an example for using Jasmine's nice equality logic in your own matchers.

Repeats

Why your Cucumber feature loses cookies when run under Selenium or capybara-webkit

When your Cucumber feature seems to forget cookies / sessions when you run it with Selenium or capybara-webkit, check if the test travels in time like here:

Given the date is 1981-10-20
When I sign in
Then I should see "Welcome!"

What happens here is that the Rails application serving pages runs in 1984, but the process running your Firefox or Webkit still lives today. This gap of 27+ years will expire most Cookies immediately.

If all you need is to freeze the time to a date, a workaround is to travel to the future instead.

Git 2.6+: How to pull with dirty index

First, decide if you want to pull with rebase. There are some implications of changing this, so think before you do it.


If you do, you can tell git to stash automatically before pulling:

git config --global rebase.autoStash true

Now on pull, git will a) stash, b) pull and c) reapply your working tree. Note that applying your changes may lead to unresolvable conflicts!

Bundler: Install gems behind a proxy

To install gems Bundler needs to be able to talk to https://api.rubygems.org.

If you are behind aproxy you can use the https_proxy environment variable:

https_proxy=http://myproxy:123 bundle install

Note that if there is no https_proxy env variable, Bundler will also look for a http_proxy env variable.

With Capistrano

Ideally the server you're deploying on exports an https_proxy variable for all shells.

If you don't have control over the server setup, you can also add this to your Capistrano config:

Repeats

RSpec: be_true does not actually check if a value is true

Don't use be_true to check if a value is true. It actually checks if it anything other than nil or false. That's why it has been renamed to be_truthy in recent RSpec versions.

The same thing holds for be_false, which actually checks if a value is not "truthy".

If you want to check for true or false in RSpec 2, write this instead:

value.should == true
value.should == false

If you want to check for true or false in RSpec 3+, write this instead:

```
expec…

Repeats

Rails: Namespacing controller classes without affecting routes

When you want to namespace your controller classes (group some controllers into a folder) but leave the corresponding routes unaffected, you can use the scope method like follows:

scope module: 'proxy' do
  resources :properties
end

AJAX requests with PATCH or DELETE method will not redirect with GET

When you make an AJAX request PATCH /foo and the /foo action redirects to /bar, browsers will request PATCH /bar. You probably expected the second request to be GET /bar, since we're used to redirects always resulting in a GET request.

# Browser sends
PATCH /foo HTTP/1.1

# Server responds
HTTP/1.1 302 Found
Location: /bar

# Browser follows redirect
PATCH /bar

# Server responds
HTTP/1.1 404 Not Found

This is also true on some browsers for DELETE, or in general for any AJAX call that is neither GET or `POST…

Repeats

Preloaded associations are filtered by conditions on the same table

When you eagerly load an association list using the :include option, and at the same time have a :condition on an included table, two things happen:

  1. Rails tries to load all involved records in a huge single query spanning multiple database tables.
  2. The preloaded association list is filtered by the :condition, even though you only wanted to use the :condition to filter the containing model.

The second case's behavior is mostly unexpected, because pre-loaded associations usually don't care about the circumstances under which the…

Auto-destruct in 56 days

Updated: Stop writing "require 'spec_helper'" in every spec

Added note that parallel_tests brings its own .rspec_parallel and ignores .rspec.

Centering with CSS vertically and horizontally (all options overview)

There are a million ways to center <div>s or text in CSS, horizontally or vertically. All the ways are unsatisfying in their own unique way, or have restrictions regarding browser support, element sizes, etc.

Here are two great resources for finding the best way of aligning something given your use case and browser support requirements:

How to center in CSS
A long form that lets you define your use case and browser support requirements, then shows you the preferred way of aligning.

[Centering CSS: A com…

How to generate a Rails-compatible query string

From Rails 3.0.9, there is a method Hash#to_query that will turn a Hash into a query string:

>> {:a => "a", :b => ["c", "d", "e"]}.to_query
=> "a=a&b%5B%5D=c&b%5B%5D=d&b%5B%5D=e"
>> CGI.unescape _
=> "a=a&b[]=c&b[]=d&b[]=e"
Repeats

How Rails and MySQL are handling time zones

When working with times and dates in Rails applications, you need to deal with the following problem:

  • In Rails, Time objects have a time zone. You can get the zone name by doing time_object.zone.
  • This zone is considered when doing time calculations, e.g. 10 AM CEST minus 8 AM UTC is zero.
  • A datetime in MySQL does not have a zone. It just stores the literal string "2010-05-01 12:00:00".
  • That means that Rails must make assumptions about timestamps loaded from and written to MySQL.

Rails has two completely different modes …

3019 cards