View
Repeats

Summarizing heredoc in ruby and rails

This card tries to summarize by example the different uses of heredoc.

  • In Ruby << vs. <<- vs. <<~
  • In Rails strip_heredoc vs. squish

strip_heredoc should be used for a text, where you want to preserve newlines. (multi-line -> multi-line)

squish should be used for a text, where you want to squish newlines. (multi-line -> one-line)

Ruby

def foo
  bar = <<TEXT
line1
line2
line3
TEXT
  puts bar.inspect
end
foo => "line1\nline2\nline3\n"

```
def foo
bar = «TEXT
line1
line2
line3
TEXT
puts bar.in…

Repeats

Invoices: How to properly round and calculate totals

While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals, it actually involves a lot of rules and caveats. It is very easy to create invoices where numbers don't add up and a few cents are missing. A missing cent is a big deal for an accountant, so it is important for your invoices to list correct numbers.

Note that this is not legal advice. Also note that while this note has a number of code examples in Ruby and MySQL, the concepts apply to all programming languages and data stores.

When …

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:

Awesome WM: workaround for gnome or mate panel stealing focus

If you use awesome 3.5, and a gnome or mate panel, the panel will often receive focus when you switch desktops. As a workaround:

  1. Add this rule to your rc.lua: { rule = { class = "Mate-panel" }, properties = { ontop = true, focusable = false } }
  2. Replace /usr/share/awesome/lib/awful with the attached file

Giving a presentation with a dual screen layout on linux

When giving a presentation with a projector it is sometimes better to use a dual screen layout instead of a cloned display. If you still want a preview of the projector screen on your primary screen, you can do this:

  1. Install x11vnc and a vnc viewer (e.g. xtightvncviewer).
  2. Connect the projector.
  3. In your system display settings, move the projector to the left or your primary screen (not strictly necessary, but I had weird clipping issues otherwise).
  4. Start a vnc server for your second display with

    ```
    x11vnc -clip xinera…

Repeats

Everything you know about html_safe is wrong

Back in the war, Rails developers had to manually HTML-escape user-supplied text before it was rendered in a view. If only a single piece of user-supplied text was rendered without prior escaping, it enabled XSS attacks like injecting a <script> tag into the view of another user.

Because this practice was so error-prone, the rails_xss plugin was developed and later integrated into Rails 3. rails_xss follows a different approach: Instead of relying…

A case for different breakpoints

The linked article states that CSS breakpoints should group "similar" screen sizes and thus be at:

  • 600px "narrow"
  • 900px "medium"
  • 1200px "wide"
  • (1800px) "huge"

By choosing these breakpoints, most device screens will be somewhere between two breakpoints, and not at the very edge of them.

The ranges could be called:

  • narrow (< narrow)
  • medium (narrow - medium)
  • normal (medium - wide)
  • wide (wide - huge)
  • huge (> huge)
Repeats

Don't forget: Automatically remove join records on has_many :through associations

Bad

# Given the following models

class Image < ActiveRecord::Base
  has_many :album_image
  has_many :albums, through: :album_images
end

class Album < ActiveRecord::Base
  has_many :album_image
  has_many :images, through: :album_images
end

# Join model
class AlbumImage < ActiveRecord::Base
  belongs_to :album
  belongs_to :image
end

Destroying a record in this setup will only remove the record itself, and leave orphaned join records behind.

```
image = Image.last
image.destroy # removes only the image record,

Howto: Free disk space when /boot is full

Easy mode

sudo apt-get autoremove should remove all but the current and previous Kernel.

Hard mode (for old Ubuntus)

uname -r prints currently used kernel version. You should not remove the current kernel, linux-image-generic and linux-image.

sudo apt-get clean
dpkg -l linux-image* 
uname -r
sudo apt-get remove linux-image-x.y.z-generic
sudo apt-get autoremove

Removing multiple kernel versions with one command (long and short version):

```
sudo apt-get remove linux-image-1.2.4-generic
sudo apt-get rem…

Repeats

Gatekeeping: Guide for developer

Note: This has been a private card for some time, because it is heavily tailored to our specific needs and tools. While it will certainly not apply to all (especially larger teams), we thought it might still be helpful as a starting point, and so made it public. Compare also the Gatekeeping: Guide for gatekeeper card.


In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging serv…

Repeats

Speed up your websites: Put JavaScripts at bottom

For websites that don't do JavaScript rendering on the client, it's best practice to put script tags at the bottom of the HTML. This way, the page can start to render before scripts have been loaded and run.

The caveat is that you also have to move all other script tags from your views to the bottom of the page. This can be done with helpers.

How to implement

  1. Add the attached javascript_helper to your app.
  2. Move your `javascript_i…

How to fix: Bundler 1.13 breaks parallel_tests

When running tests via parallel_tests, you may encounter an error:

cannot load such file -- parallel_tests/gherkin/runtime_logger
Error creating formatter: ParallelTests::Gherkin::RuntimeLogger (LoadError)

This will happen when you upgrade Bundler to version 1.13.x and appears to be "by design" since there is a Bundler config option to restore previous behavior.

You can fix it by setting that flag. You should commit the resulting config file into the repository!

bundle config --local disable_exec_load true

There is a Git…

Repeats

Capybara will not find links without an href attribute

Capybara will fail to find <a> tags that are missing an href attribute. This will probably happen to you every now and then on JavaScript-heavy applications.

An example would be an AngularJS application where the following HTML actually works. [1]

<a ng-click="hello()">Hello</a>

Capybara will fail to find that link, even though looking it up via the DOM shows it:

>> find_link("Hello")
Capybara::ElementNotFound: Unable to find link "Hello"

>> find("a").text
=> "Hello"

To make find_link and click_link work…

Repeats

grep: regex

You can use three different versions of the regular expression syntax in grep:
- basic: -G
- extended: -E
- perl: -P

Difference between basic and extended:

In basic regular expressions the meta-characters '?', '+', '{', '|', '(', and ')' lose their special meaning; 
instead use the backslashed versions '\?', '\+', '\{', '\|', '\(', and '\)'.

Difference between extended (POSIX) and perl (PCRE): E.g. \d is not supported in POSIX.

This grep comman…

Repeats

Bash: Build and execute command lines on the fly with "xargs"

xargs is a powerful bash tool that can take input from $STDIN and pass it to a given command. I.e. you can do the following:

$> cat tmp/parallel_cucumber_failures.log
features/authentication.feature:33
features/backend/pages.feature:5
features/backend/pages.feature:60

$> cat tmp/parallel_cucumber_failures.log | xargs geordi cucumber
# Running features
> Only: features/authentication.feature:33 features/backend/pages.feature:5 features/backend/pages.feature:60
...

Fix external Displays switching not on when plugging notebook in docking station

If your external displays not switching on or showing a weird behavior (for e.g. all displays getting the same configuration all the time) you can fix it by switching off all external displays and re-enabling only one in the first step. Afterwards you can apply your whole configuration via xrandr. This behavior could be a bug in the kernel and may be fixed in linux 4.8.

Example display configuration

```
Screen 0: minimum 8 x 8, current 5760 x 1200, maximum 32767 x 32767
eDP1 connected 1920x1080+0+0 (normal left inverted right x axis…

Repeats

Make your Rails console (and irb) output better readable

Pour color on your Rails console with awesome_print. Turn confusing long strings into formatted output. Have objects and classes laid out clearly whenever you need it.

Put gem 'awesome_print', :group => :development into your Gemfile. Now on the Rails console you have the command ap that will give you a colored, formatted output of whatever you pass it. See the example output of the User class below.

For customization visit the repository on Github.

![awesome_print.png](https://makan…

Repeats

Capybara: Find an element that contains a string

There is no CSS selector for matching elements that contains a given string ¹. Luckily, Capybara offers the :text option to go along with your selector:

page.find('div', text: 'Expected content')

You can also pass a regular expression!

page.find('div', text: /Expected contents?/i)

Note that if your CSS selector is as generic as div, you might get a lot more results than you expect. E.g. a <div class="container"> that surrounds your entire layout will probably also contain that text (in a descendant) and Capybara wil…

View
3267 cards