Browse Amazon S3 buckets with Ubuntu Linux

There are some frontends available, but they all suck, are no longer maintained or are hard to install.

As a surprisingly comfortable alternative I have found a command line tool s3cmd:

sudo apt-get install s3cmd

When you run s3cmd the first time it will ask you for your access key ID and secret access key. This information is cached somewhere so you only need to write them once. To reconfigure later, call s3cmd --configure.

Once you're done setting up, s3cmd gives you shell-like commands like s3cmd ls or `s3cmd del som...

rspec_candy 0.4.0 released

  • Now supports RSpec 3 and Rails 4
  • Drops support for state_machine, which has some issues with Rails 4 and isn't actively maintained

Add an alternative image source for broken images

Awesome hack by Tim VanFosson:

<img src="some.jpg" onerror="this.src='alternative.jpg'" />

ActiveRecord: Order a scope by descending value without writing SQL

Instead of this:

Image.order('images.created_at DESC')

You can write this:

Image.order(created_at: :desc)

Not only do you not have to write SQL, you also get qualified column names (created_at becomes images.created_at) for free.

Multiple order criteria

To add secondary order criteria, use a hash with multiple keys and :asc / :desc values:

Image.order(title: :asc, created_at: :desc)

PostgreSQL: How to change attributes of a timestamp

It's generally not trivial to change a datetime's seconds, minutes, etc in SQL. Here is how it works when speaking PostgreSQL.

Consider you have a timestamp column whose seconds you want to zero:

SELECT born_at FROM users;
       born_at
---------------------
 2015-05-01 13:37:42

You can the TO_CHAR function to convert date or time values into a string, and do your changes there:

SELECT TO_CHAR(born_at, 'YYYY-MM-DD HH24:MI:00') FROM users;
       to_char
---------------------
 2015-05-01 13:37:00

...

Rails has a built-in slug generator

Today I learned that Ruby on Rails has shipped with a built-in slug generator since Rails 2.2:

> "What Up Dog".parameterize
=> "what-up-dog" 

> "foo/bar".parameterize
=> "foo-bar" 

> "äöüß".parameterize
=> "aouss" 

Also see: Normalize characters in Ruby.

Custom loggers in Ruby and Rails

File logger

If you need to log to a file you can use Ruby's Logger class:

require 'logger'

log = Logger.new('log/mylog.log')
log.info 'Some information'
log.debug 'Debugging hints'
log.error StandardError.new('Something went wrong')

Logger does a number of things well:

  • Message type (info / debug / error) is logged
  • Log entries are timestamped
  • Writing log output is synchronized between threads
  • Logged errors are printed with full backtraces

If you don't like the output format, you can define a custom formatter.

I ha...

Dusen (>0.5) now with "exclude from search"

Dusen (our search gem) is now capable of excluding words, phrases and qualified fields from search.
E.g. search for

  • included -excluded
  • "search this" -"not that"
  • topic:"Yes" -topic:"No"

This will soon also work in makandra cards!

How to use CSS to rotate text by 90° in IE8 (and modern IEs)

If you want to rotate text, you can use CSS transforms in somewhat modern browsers to rotate the container element.
However, if you need to support IE8, transform is unavailable (if need only IE9 support, ignore the following and use -ms-transform).

Here is a solution that worked for me:

<div class="my-element">Hi!</div>

^

.my-element {
  display: inline-block;
  transform: rotate(90deg);
  -ms-writing-mode: tb-rl;
  -ms-transform: none;
}

This way, browsers will use CSS transforms when available -- w...

Linux: Kill a process matching a partial name

This is useful to kill processes like ruby my-script.rb:

pkill -f my-script.rb

With great power comes great responsibility.

How to enable WebGL in Chrome

Check your GPU state on chrome://gpu. If it reads "WebGL: Hardware accelerated" in the first list, you're set. Else:

  1. Make sure chrome://flags/#disable-webgl is disabled (there should be a link "Enable")
  2. If that does not help, try to additionally enable chrome://flags/#ignore-gpu-blacklist.

Multiline comments in indented Sass syntax

Write a // and indent every subsequent line by two spaces.

This is great for documenting BEM blocks!

//
  An action button
  ================
  
  Basic usage
  -----------
  
      <a href="/path" class="action">New booking</a>
      <button class="action">Save</a>
      <input type="submit" class="action">Save</a>
  
  Colors
  -------
  
      <a href="/path" class="action is-red">Primary</a>
      <a href="/path" class="action is-grey">Secondary</a>
  
  Small inline buttons
  --------------------
  
      <p>
        Recor...

FactoryBot: How to get the class from a factory name

When you want to look up a class for a given factory, do it like this:

>> FactoryBot.factories.find('admin').build_class
=> User

In older versions, you could do:

>> FactoryBot.factory_by_name('admin').build_class
=> User