Heads up: RSpec-Mocks' #stub_const will define intermediate modules that have not been loaded yet

The issue: You are using stub_const to change a constant value for your test.

stub_const "SomeClass::CONST", 'test'

All of a sudden, tests fail with undefined method 'some_method' for #<SomeClass:0x00000000101433a8>.

The reason

When using stub_const before the Class containing the constant has been loaded, a module is automatically created with the name.

Since RSpec does no autoloading, it will create a SomeClass module by itself. This is arguably a good idea.

As a workaround, use stub_const in your Rails specs li…


jQuery promises: done() and then() are not the same

jQuery's deferred objects behave somewhat like standard promises, but not really.

One of many subtle differences is that there are two ways to chain callbacks to an async functions.

The first one is done, which only exists in jQuery:

$.ajax('/foo').done(function(html) {
  console.debug("The server responded with %s", html);

There is also then, which all promise libraries have:

$.ajax('/foo').then(function(html) {
console.debug("The server resp…


Writing a README for a Rails project

A Rails project should have a README that gives the reader a quick overview of the project. Its size will vary as projects differ in complexity, but there should always be some introductory prose for a developer to read when starting on it.


That's already the main purpose of a project README: Give a new developer a quick overview of the project. In sketching this outline, the README should notify the reader of any peculiarity he needs to know of.

Remember that in a few months, you'll be a kind of "new developer" yourse…

How to use cookies with curl

When making requests using curl, no cookies are sent or stored by default.
However, you can tell curl to re-use cookies received earlier (or forge your own cookies).

There are 2 command line switches you need to use:

  • -c will write cookies to a given file
  • -b will read cookies from a given file


The remote server sets a "foo" cookie to value "bar". We tell curl to store them to a file at /tmp/cookies using the -c switch.

$ curl -c /tmp/cookies

You may look at the file, …


Beware: Many browsers define window.event

Some browsers define window.event, which will return a copy of the "current" event. However, this is not defined by the W3C. Most importantly, Firefox does not support it, neither do recent versions of IE.

You should completely avoid accessing the global event and instead pass the event object around that the event handler receives. The easiest protection against accidentally accessing window.event is just never naming the event event, but e or evnt or so.

function brokenInFirefox() {
event.preventDefault(); // Break…


How to: Context-dependent word expansion in RubyMine

One of the many useful features of TextMate is autocompletion of words. If I were in TextMate right now, I could write "au[tab]", and it would complete it to "autocompletion". RubyMine can do this, too. When you write a word (e.g. a variable name), just hit ALT + / repeatedly and it will offer all completions for the letters you typed. This action is called Cyclic Expand Word in RubyMine / IntelliJ IDEA.

This feature keeps you from mistyping variable names, saves you keystrokes and speeds up development. ~10 keystrokes to the price …


Action Mailer Previews (since Rails 4.1)

Since Rails 4.1 there is a preview-function to see what an e-mail will look like.

All you need to do is implement a preview-class in spec/mailers/previews (if you are using RSpec):

class NotifierPreview < ActionMailer::Preview
  def welcome

and then a preview will be available in the browser at http://localhost:3000/rails/mailers/notifier/welcome as well as a list of previews at http://localhost:3000/rails/mailers .

Note: If your preview model is not loaded you might need to change…

Error during Rails 5 upgrade: Environment data not found in the schema

This error is raised because your old database does not have a configured environment yet, which Rails 5 enforces.

If this error occurs while migrating your parallel test databases, make sure to update the parallel_tests gem first: current versions fix this. If you're still using Cucumber < v3, the latest version of parallel_tests will be 2.18.0.

How to find and replace empty cells in Libre Office Calc

To find and replace (CTRL + H) empty cells in Libre Office Calc you can use a regular expressions (also called "Finds an empty paragraph" in context of a spreadsheet):


It is also possible to just enter a regular expression in the search (CTRL + F), but there is no option to only search in the current selection like in the screenshot above. Use find and replace instead if needed.


CarrierWave: How to remove GIF animation

When accepting GIF images, you will also accept animated GIFs. Resizing them can be a time-consuming task and will block a Rails worker until the image is processed.

Save yourself that trouble, and simply tell ImageMagick to drop any frames but the first one.

Add the following to your uploader class:

process :remove_animation


def remove_animation
  if content_type == 'image/gif'
    manipulate! { |image| image.collapse! }

You may also define that process for specific versions only (e….

Rails: Migration helper for inserting records without using models

You should avoid using application models in your migrations. But how else would you create records in a migration?

The simplest alternative is to write plain SQL. Since INSERT statements are no pleasant write for Rubyists, here is a simple wrapper:


The helper method below takes a table name and a hash of attributes, which it inserts into the specified table. Copy it over to your migration and profit!


def insert_record(table, **attributes)


Testing regular expressions visually

Developing complex regular expressions quickly blows my mind. Here are some online regex editors that help you by highlighting matching text and capture groups:


Check that an element is hidden via CSS with Spreewald

If you have content inside a page that is hidden by CSS, the following will work with Selenium, but not when using the Rack::Test driver:

Then I should not see "foobear"

This is because the Rack::Test driver does not know if an element is visible, and only looks at the DOM.

Spreewald offers steps to check that an element is hidden by CSS:

Then "foo" should be hidden # "should not be visible" in Spreewald < 1.2.3

You can also check that an element is visible:

Then "foo" shou...

Remember: LoDash syntax is a bit different from UnderscoreJS

Since we are using LoDash instead of UnderscoreJS in recent/current projects, you should keep in mind that their syntax is a bit different.


In UnderscoreJS, methods always return a value:

x = [1, 2, 2]
_.uniq(x) // => [1, 2]
_(x).uniq() // => [1, 2]

If you want to chain multiple calls, you need to start off with a _.chain, and call value() to terminate the chain.

_.chain(x).uniq().map(function(i) { return i + 10 }).reverse().value() // => [12, 11]


External contentRepeats

Create and send any HTTP request using the Postman request builder

Talking with APIs makes more fun using Postman. Otherwise you might want to read: How to send HTTP requests using cURL

Postman ist available as Chrome or Mac App.

How does it helped me?
- Editing multiline JSON bodies is much more comfortable than in the terminal
- Saving named request in a collection
- Syntax highlighting when writing JSON bodies
- History with all my request
- Multiple environments

CSS: Giving text lines a background-color (with configurable line padding and margin)

The gist is:

  • wrap the text with a span
  • use line-height for the spacing between lines ("margin")
  • use box-shadow to control the line background size ("padding")



  box-shadow: 0 0 0 10px #fff
  background-color: #fff
  box-decoration-break: clone # Fix Firefox
  line-height: 2.2

→ [jsfiddle](…

Jasmine: Expecting objects as method invocation arguments

To check if a method has been called in Jasmine, you first need to spy on it:

let spy = spyOn(window, 'alert')
expect(window.alert).toHaveBeenCalledWith('Important message')

To expect an object of a given type, pass the constructor function to jasmine.any():


To expect an oject with given key/value properties, use `jasmine.objectContaini…

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