Linux: rename or change extension of multiple files

When you need to bulk rename files you can not call "mv *.foo *.bar" to change the extension of all .foo files to bar (because bash resolves wildcards and replaces them with the list of matched files).

This works on linuxes who use the Perl version of the rename command (like Ubuntu):

rename 's/\.foo$/\.bar/' *

You can also use this to rename other parts of the file, e.g. from flag_en.png, flag_de.png etc. to just en.png or de.png:

rename 's/^flag_//' *

Note that we used $ and ^ to explicitly look at the ...

Speed up file downloads with Rails, Apache and X-Sendfile

When you use the send_file method to send a local file to the browser, you can save resources on the application server by setting the :x_sendfile option to true. This option is activated by default for Rails 3, so you need to understand this.

What this option does is not to send any data at all, but rather set the local file path as a new response header:

X-Sendfile: /opt/www/awesome-project/shared/downloads/image.png

When the response comes back from Rails to...

Generate a strong secret from the shell

A good tool to generate strong passwords and secrets is "apg". You can get it with

sudo apt-get install apg

To create a strong secret for sessions, hashed Paperclip paths, etc. say

apg -m128 -a1 -E\'\"

Arguments explained:

  • The -m parameter defines the secret length
  • -a1 makes apg choose from all 7-bit ASCII characters instead of just the alphabet
  • -E\'\" excludes quote characters so you can easily paste the secret into a Ru...

Install the Paperclip gem on Ubuntu servers

You need to install the following packages before you can build the Paperclip gem:

sudo apt-get install imagemagick librmagick-ruby

Copying validation errors from one attribute to another

When using virtual attributes, the attached trait can be useful to automatically copy errors from one attribute to another.

Here is a typical use case where Paperclip creates a virtual attribute :attachment, but there are validations on both :attachment and :attachment_file_name. If the form has a file picker on :attachment, you would like to highlight it with errors from any attribute:

class Note < ActiveRecord::Base
  has_attached_file :attachment
  validates_attachment_presence :a...

Deliver Paperclip attachments to authorized users only

When Paperclip attachments should only be downloadable for selected users, there are three ways to go.
The same applies to files in Carrierwave.

  1. Deliver attachments through Rails

The first way is to store Paperclip attachments not in the default public/system, but in a private path like storage inside the current release. You should prefer this method when dealing with sensitive data.

Make ...

Make box shadows look the same in IE and other browsers

The box shadows created rendered in IE by CSS3PIE look darker and are blurred differently than in browsers that render box-shadow natively.

If possible, try to be OK with this. If not, make an IE-only stylesheet that uses a different color and blur radius:

// Real browsers:
+box_shadow("0 4px 10px #bbb")

// IE with PIE:
+box_shadow("0 5px 15px #888")

We should try to package this solution in a neat way so we don't need different stylesheets.

See also this [cross-browser box-shadow comparison]...

Recursively remove unnecessary executable-flags

Sometimes files attain executable-flags that they do not need, e.g. when your Windows VM copies them over a Samba share onto your machine.

From inside your Rails project directory call regularly:

geordi remove-executable-flags

Runs chmod -x on Ruby, HTML, CSS, image, Rake and similar files.


This script is part of our geordi gem on github.

Change Paperclip secrets the hard way

So you screwed up and copied Paperclip secrets from one project to another. Here is a semi-automatic, painful way to migrate your existing attachment files to new locations.

You need to follow this step by step, do not just copy the whole thing into the console!

# 1. Get old paths by doing something like this on the console:
old_paths = ModelWithAttachment.all.collect { |m| [m.id, File.dirname(m.image.path(:original)).gsub(/original$/, '') ] if m.image.file? }.compact.uniq

# 2. Now change the Paperclip secret on the co...

Change the color of a <hr> in all browsers

The following Sass will do it:

hr
  color: #ddd
  background-color: #ddd
  border: none
  height: 1px

Only allow pictures as Paperclip attachments

validates_attachment_content_type :image, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'

Reprocess only missing images with Paperclip

When a paperclip attachment gains a new style and you have many attachments, reprocessing can take ages. This is because all styles are being recomputed.

To create only missing images, patch Paperclip like this in your script that does the reprocessing:

Paperclip <2.3.2

class Paperclip::Attachment
  private

  def post_process_styles_with_extreme_lazyness
    @old_styles = @styles

    @styles = @styles.reject do |name, _|
      exists?(name)
    end

    post_process_styles_without_extreme_...

Convert colorspace of images attached with Paperclip

has_attached_file(
  :avatar,
  :styles => { :large => "300x300", :small => "100x100" },
  :convert_options => { all => "-colorspace RGB" }
)

Reprocess Paperclip attachments in one line

script/runner -e development 'Article.all.each { |a| a.image.reprocess! if a.image.exists? }; "done"'

Basic styles for flash notifications

.notice,
.error,
.information,
.warning {
    font-weight: bold;
}

.notice {
    color: #11bb00;
}

.error {
    color: #F53A31;
}

.information {
    color: #557;
}

.warning {
    color: #d07d2d;
}

Configuring Git with .gitconfig

Basic configuration

Please keep this config simple. It should be a starting point for new developers learning Git.

[user]
  name = Your Name
  email = your.name@domain.com

[branch]
  sort = -committerdate
[color]
   ui = auto
[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green
[color "diff"]
  whitespace = white reverse
  meta = blue reverse
  frag = blue reverse
  old = red
  new = green
[color "status"]
  added = green
  changed = yellow
  untracked = cyan
[interactive]
  singlekey = true # Do not requir...