3521 cards
View

Minify Font Awesome fonts with webpack

Font Awesome 5 is a comprehensive solution for vector icons on your website.

Originally, Font Awesome came as an icon font (plus stylesheets), but recently it can also be used as a pure JavaScript solution (which will render icons as inline <svg> tags), or even as SVG sprites.

All solutions have their pros and cons:

Icon font:

  • little CPU load (no JavaScript)
  • fonts are relatively large
  • 1 extra HTTP request

Javascript + inline SVG:

  • higher CPU load (needs to watch the DOM via mutation observers to ad…
Repeats

Javascript: Comparing two arrays for equality

Don't Google this, you will lose all will to live. Instead use Object#isEqual from Underscore.js:

_.isEqual([1, 2], [2, 3]) // => false

Rbenv: How to remove a gem installed via Bundler from Github

Normally you can list all gems of the current ruby version with gem list, which also includes the gems of you Gemfile. These can be uninstalled with gem uninstall gemname.

List and uninstall a gem installed via Bundler from Github

This does not work for gems installed directly from Github. They do not appear in gem list.

Show all gems installed via Github by bundler:

ls ~/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/bundler/

Remove a gem installed via Github by Bundler:

```
rm -rf ~/.rbenv/versions/2.4.1/lib/ruby/gems/2….

Repeats

Your Rails sandbox console

Just found out about a great feature in Rails that seems to be around since Rails 2. Start a console with the --sandbox (or -s) parameter:

rails console --sandbox

All changes you make to the database will be rolled back on exit. Very safety, much great, so secure. Wow!

Drawing diagrams with draw.io

draw.io is a web application that lets you draw flow charts and other diagrams. It works much nicer than office suite tools like LibreOffice Draw.

Its UI is easy to use: Simply drag and drop shapes or arrows from the left-hand side onto your sheet. Drag start/end of arrows to shapes to automatically connect them. You can add text to shapes by selecting them and typing. To add colors, select a shape and use the right-hand panel.

While you can, you don't need to store your charts online. "Offline" storage can be files…

An incomplete guide to migrate a Rails application from paperclip to carrierwave

In this example we assume that not only the storage gem changes but also the file structure on disc.

A general approach

Part A: Create a commit which includes a script that allows you to copy the existing file to the new file structure.

Part B: Create a commit which removes all paperclip logic and replace it with the same code you used in the first commit

Part A

Here are some implementation details you might want to reuse:

  • Use the existing models to read the files from
  • Use your own carrierwave models to write t…
Auto-destruct in 59 days

Updated: Sun Java JVM/JRE on Ubuntu Linux

  • Added Java 10 installation guide
  • Added an instruction how to change the default java version

How to: Use different configurations for S3cmd

S3cmd is a free command line tool and client for uploading, retrieving and managing data in Amazon S3. S3cmd reads its configuration by default from ~/.s3cfg, which is created once you run s3cmd --configure.

If you have many configurations, we recommend to always specify the configuration you want to use. This prevents applying actions to the wrong bucket.

Examples:

```
s3cmd -c ~/.s3cfg-github-staging ls
s3cmd -c ~/.s3cfg-github-development …

Repeats

Take care of indentation and blank lines when using .erb for plain text emails

Building plain text emails with an .erb template doesn't allow you to indent code like you normally do in HTML mails.

DON'T

<%= 'foo' if bar %>

"\n" if bar is false

"foo\n" if bar is true


<%= nil %>

"\n"


<% if true %>
  <%= 'foo' %>
<% end %>  

" foo"


<%= 'foo' %>

<%= 'bar' %>

"foo\n\nbar\n"

DO

Write unindented code to get the expected result.

<% if bar %>
<%= 'bar' %>
<% end %>
<%= 'foo' %>
<%= 'bar' %> 
  • Use [Form Models](https://github.com/makandra…
Auto-destruct in 51 days

Updated: A quick introduction to CORS

Updated card with:

  • "common CSRF" note stating why the intro does not mention CSRF tokens etc
  • "preflight" CORS request description
  • "fonts are special" hint

RSpec: How to check if a string contains terms in a desired order

There seems to be no built-in matcher in RSpec to check if a string contains terms in the desired order. A simple workaround is to use a regular expression that also matches newlines (m-modifier).

Cons:

  • The readability when terms need to be escaped is bad
  • A failed spec has an error which needs manually action to be understood (Search if terms appear and in which order)

Example:

expect(ActionMailer::Base.last.to_s).to match(/Dear customer.*Account canceled.*You Awesome Company/m)
Auto-destruct in 45 days

Updated: better rails app restart with the passenger restart-app tool

Since passenger 5.0.10 you don't have to use sudo anymore and you shouldn't use it.

Repeats

Defining class methods with Modularity traits

There are two ways to define a class method from a Modularity trait. Note that the usual caveats regarding class method visibility apply.

Using define_method

The recommended way is to define a method on your module's singleton class:

# ruby 1.9+
module SomeTrait
  as_trait do
    define_singleton_method :foo do
      # ...
    end
 ...
Auto-destruct in 58 days

assignable_values now supports Array attributes

Assignable values can now also be used for array values. This works when you use Rails 5+ and PostgreSQL with an array column, or on older Rails versions and other databases using ActiveRecord's serialize.

To validate array values, pass multiple: true:

class Song < ActiveRecord::Base
  serialize :genres   # skip this when you use PostgreSQL and an array type column

  assignable_values_for :genres, multiple: true do
    ['pop', 'rock', 'electronic']
  end
end

In this case, every subset of the given values is valid, for examp…

Repeats

Test if two date ranges overlap in Ruby or Rails

A check if two date or time ranges A and B overlap needs to cover a lot of cases:

1. A partially overlaps B

2. A surrounds B

3. B surrounds A

4. A occurs entirely after B

5. B occurs entirely after A

This means you actually have to check that:

  • neither does A occur entirely after B (meaning A.start > B.end)
  • nor does B occur entirely after A (meaning B.start > A.end)

Flipping this, A and B overlap iff A.start <= B.end && B.start <= A.end

The code below shows how to implement this in Ruby on Rails. The example is a clas…

How to let passenger restart after deployment with capistrano

Phusion Passenger changed the way how it gets restarted several times. Through the project's history, these all were valid:

  • touch tmp/restart.txt
  • sudo passenger-config restart-app /path/to/app
  • passenger-config restart-app /path/to/app

You should not need to know which one to use. Instead, the capistrano-passenger gem will choose the appropriate restart mechanism automatically based on your installed the passenger version.

Installation

  1. Add to your Gemfile:

    gem 'capistr...
    

When defining methods in specs, you can super into previous definitions

When you def methods in RSpec tests, you can use super to call a method definition that was created in a surrounding describe/context block.

Example:

describe User do

  def build_user(attributes = {})
    User.new(attributes)
  end

  describe '#my_method' do
    it 'does something' do
      # ...
    end

    context 'when the user has an e-mail address' do
      def build_user(attributes = {})
        super({ email: 'user@example.com' }.merge(attributes))
      end

      it 'd...
Repeats

Debugging cucumber feature with javascript + firefox vnc

TL;DR Debugging problems with javascript errors in cucumber tests is sometimes easier in the browser. Run the test, stop at the problematic point (with Then pause from Spreewald 1.7+) and open VNC for Firefox.

Features:

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