View
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
...
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…

Hide a Rake task from the `rake -T` list

A Rake task appears in rake -T if it has a description:

desc 'Compile assets'
task :compile do
  ...
end

To not list it, simply omit the description:

task :compile do
  ...
end

You can also hide a Rake task that has been defined by someone else (like a gem) by removing the description:

Rake::Task['compile'].clear_comments

Or you can whitelist which tasks should be listed:

```
visible_tasks = %w(compile build package)
Rake::Task.tasks.each do |task|
visible_tasks.include?(task.name) or task.clear_comments
en…

Ruby: Writing specs for (partially) memoized code

When you're writing specs for ActiveRecord models that use memoization, a simple #reload will not do:

it 'updates on changes' do
  subject.seat_counts = [5]
  subject.seat_total.should == 5
  # seat_total is either memoized itself, or using some
  # private memoized method
  
  subject.seat_counts = [5, 1]
  subject.seat_total.reload.should == 6 # => Still 5
end

You might be tempted to manually unmemoize any memoized internal method to get #seat_total to update, but that has two disadvant…

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

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…

Repeats

How to create columns like "bigint" or "longtext" in Rails migrations, and what :limit means for column migrations

Rails understands a :limit options when you create columns in a migration. Its meaning depends on the column type, and sometimes the supplied value.

The documentation states that :limit sets the column length to the number of characters for string and text columns, and to the number of bytes for binary and integer columns.

Using it

This is nice since you may want a bigint column to store really long numbers in it. You can just create it by …

Repeats

Copy to clipboard without flash (clipboard.js)

We used zeroclipboard.js in some of our projects but now we switched to clipboard.js because it does not rely on flash. Flash support of the major browsers ends.

Some more advantages of clipboard.js:

  • it consists only of a single javascript file, so it does not trigger additional requests with rails
  • it automagically provides user feedback by selecting the text it has copied
  • it provides callbacks for success and error which make it easier to add custom behaviour after copying to the clipboard
  • i…

Useful puppetdb queries

The queries in this example are all executed executet via curl local on the puppetdb server. They're piped to jq, so you need jq installed to make use of these queries. Your puppetdb needs to support the query API v4.

get all hostnames of nodes including a specific class

curl -Gs http://localhost:8080/v4/resources/Class/$CLASSNAME | jq '.[].certname'
# example
curl -Gs http://localhost:8080/v4/resources/Class/Nginx | jq '.[].certname'

get hostnames of nodes wi…

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…

Aruba: Stubbing binaries

When testing your command line application with Aruba, you might need to stub out other binaries you don't want to be invoked by your test.

Aruba Doubles is a library that was built for this purpose. It is not actively maintained, but works with the little fix below.

Installation

Install the gem as instructed by its README, then put this Before block somewhere into features/support:

```
Before do
ArubaDoubles::Double.setup
prepend_environmen…

Repeats

Organize large I18n dictionary files in Rails 3+

If you're suffering from a huge de.yml or similiar file, cry no more. Rails lets you freely organize your dictionary files in config/locales.

My organization works like this:

  • config/locales/rails.de.yml modified Rails boilerplate
  • config/locales/faker.de.yml modified Faker boilerplate
  • config/locales/models.de.yml model names, attribute names, assignable_value labels
  • `config/locales/views.de.y…
External content

Subclassing module

Yesterday I stumbled across a talk in which the guy mentioned module sub-classing. I was curious what you can do with it and found his blog post with a cool example. It allows you to inject some state into the module you are including elsewhere. Check it out!

```
class AttributeAccessor < Module
def initialize(name)
@name = name
end

def included(model)
super
define_accessors
end

private

def define_accessors
ivar = "@#{@name}"
define_writer(ivar)
define_reader(ivar)
end

def define_writer(ivar)

View
3271 cards