Downgrade Bundler in RVM

Confusingly, RVM installs the bundler gem into the @global gemset, which is available to all gemsets and Rubies.

You can get around this and install a particular bundler version like this:

rvm @global do gem uninstall bundler
rvm @global do gem install bundler -v 1.6.5

Bootstrap: How to avoid printing link URLs

By default, Twitter Bootstrap's print styles include printing links.

/* Bootstrap's way of printing URLs */
@media print {
  a[href]:after {
    content: " (" attr(href) ")";
  }
}

If you want to turn that off, you can do

/* Don't print link hrefs */
@media print {
  a[href]:after {
    content: none
  }
}

Angular: Caching API responses in Restangular

Restangular can make use of $http's built-in response cache.

# Cache response for single request
Restangular.one('accounts', 123).withHttpConfig({ cache: true }).get();

# Cache responses for all requests (be careful with that, you might work with stale data)
RestangularProvider.setDefaultHttpFields({ cache: true });

To invalidate cached responses e.g. on a state change in UI Router, you can do

@app.run ['$rootScope', '$cacheFactory', ($rootScope, $cacheFactory) ->
  $rootScope.$on '$stateChangeSuccess', ->
    $cacheF...

Angular: Binding an HTML value

To bind an HTML value to ng-bind-html, you need to mark it as "trusted" first. Among other ways, you can do so with a custom filter.

# Filter in Coffeescript...:
@app.filter 'htmlSafe', ['$sce', ($sce) -> $sce.trustAsHtml ]

# ...or JS:
app.filter('htmlSafe', [
  '$sce', function($sce) {
    return $sce.trustAsHtml;
  }
]);

# Usage:
<div ng-bind-html="item.value | htmlSafe"></div>

This is a replacement for the ng-bind-html-unsafe directive which has been removed in Angular 1.2.

:party:

How to install fonts in Ubuntu

  1. Put the font files (e.g. ttf) into ~/.fonts
  2. Run fc-cache -v

Or, if you prefer to use the GUI, open each font file and click the "Install" button in the top right of the preview window.

New Firefox and gem versions for our Selenium testing environment (Ubuntu 14.04+)

Firefox 5.0.1, which we were using for most Rails 2.3 projects, does not run on Ubuntu 14.04 any more. Here is how to update affected projects.

  1. Update (or create) .firefox-version with the content: 24.0
    If you haven't installed Firefox 24 yet, the next time you run tests with Geordi, it will tell you how to install it.

  2. On a Rails 2 project:

Pitfall: ResourceController overwrites where ActiveRecord enqueues

Defining one callback several times in the same class behaves different in ActiveRecord and ResourceController.
While in ActiveRecord the callbacks are enqueued, they overwrite each other in ResourceController.

ActiveRecord - a common practice

class Post < ActiveRecord::Base
  does 'post/behavior'
  before_validation :do_something
end

module Post::BehaviorTrait
  as_trait do
    before_validation :do_something_else
  end
end

do_something_else and do_something are executed before validation in exactly this order

ResourceC...

Accept nested attributes for a record that is not an association

Note: Instead of using the method in this card, you probably want to use ActiveType's nested attributes which is a much more refined way of doing this.


The attached Modularity trait allows you to accept nested attributes for a record that is not an association, e.g.:

    class Site < ActiveRecord::Base
      
      def home_page
        @home_page ||= Page.find_by_name('home')
      end
      
      does 'a...

How to install the `xelatex` binary on Ubuntu 14.04

Just install the texlive-xetex package:

sudo apt-get install texlive-xetex

Running integration tests without texlive-xetex will produce an error during xelatex execution:

RTeX::Document::ExecutableNotFoundError

Prevent LibreOffice from changing standard styles when you format individual characters

You select some characters, make them bold and suddenly your entire document is bold?

Here's how to fix that:

  • Right-click the style in the "Styles and Formatting" window
  • Select "Modify"
  • Go to the "Organizer" tab
  • Unselect "AutoUpdate"

You're welcome.

Scroll a textarea to a given position with jQuery

Browsers make this very hard. Even when you explicitely set the selection inside the textarea (e. g. using jquery-fieldselection) the browser will not scroll the textarea to this selection.

What you can do instead is abuse browsers' behavior that they scroll to the end of a textarea when it gets focus. So we set the textarea's value to the text before the position, then focus it, then reset it to its original value:

function scrollTextareaToPosition($textarea, position) {
  var text...

bower-rails can rewrite your relative asset paths

The asset pipeline changes the paths of CSS files during precompilation. This opens a world of pain when CSS files reference images (like jQuery UI) or fonts (like webfont kits from Font Squirrel), since all those url(images/icon.png) will now point to a broken path.

In the past we have been using the vendor/asset-libs folder ...

JavaScript: How to check if an object is NaN

JavaScript's NaN ("Not a Number") is hard to compare against. It never equals anything, not even itself:

NaN === NaN;        // false
Number.NaN === NaN; // false

There is the isNaN method, but it is not really what you are looking for:

isNaN(NaN)     // true
isNaN('hello') // true

Option 1: ES6

The Object.is() method determines whether two values are the same value. It even works for NaN:

Object.is(NaN, NaN) // true

Option 2: ES5

The example above shows that simply using isNaN would match other ...

Returning an empty ActiveRecord scope

Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this by calling none on your ActiveRecord model.

    MyModel.none # returns an empty ActiveRecord::Relation object

For older Rails versions you can use the attached initializer to get a none scope.