View

Highlight current navigation item with Staticmatic

StaticMatic is a nice tool to build simple static websites.
In case you want to have some nifty styles on the navigation item that is currently active, you can use this:

=link 'Aktuelles', :class => (current_page =~ /aktuelles/) ? 'current' : 'default'

Keep in mind that current_page gives you the full relative path of your page. raise current_path in case you're not sure.

I know there is an navigation helper out there. I did not use it and also did not want to migrate.

Test meta-refresh redirects with Cucumber

The step definition below allows you to write:

Then I should see an HTML redirect to "http://www.makandracards.com" in the page head

Capybara

Then /^I should see an HTML redirect to "([^\"]*)" in the page head$/ do |redirect_url|
  page.should have_xpath("//meta[@http-equiv=\"refresh\" and contains(@content, \"#{redirect_url}\")]")
end

To find meta tags with Capybara, you can also use page.find('meta', visible: false).

External content

Meny - A three dimensional and space efficient menu concept

Stuff like this is now possible in Webkit browsers, Firefox and IE10.

"rake gettext:findpo" running forever

Under certain circumstances gettext_i18n_rails will hit a near-infinite loop. This occured in Rails 2.3.5 with Haml 3.0.18 and fast_gettext 0.5.10.

gettext_i18n_rails's Haml-Parser compiles the Haml code and delegates the parsing to ruby_parser. Unfortunately, ruby_parser appears to be confused when a string contains both escaped chars (that is, any unicode characters as ndash, umlauts etc.) and #{} blocks, which makes it extremely slow.

The easiest "solution" we came up with was to replace all occurrences of UTF-8 chars wi…

Beware of magic adblocker rules

If a customer calls and tells you that she cannot see some content of her website beware of the following before starting tcpdump or other forensic debugging tools.

Some ad blockers like Adblock are shipped with filters that remove (amongst others)

  • divs named 'sponsor'
  • images with a class "banner" or the filename "banner.png"
  • images from directories named 'sponsor*'
  • Image names or class names that begin with adv_....

Renaming the div to something more specific to your site and renaming…

Boolean fields in migrations

If you want to update some records with boolean fields in a migration, always remember to set your values with field=#{quoted_true} and field=#{quoted_false}. The Rails methods quoted_false and quoted_true return the correct boolean representations for your database.

"command not found" bash function

If you type a command in your bash that doesn't exist you get this:

bash: foo: command not found

or if you have installed the command-not-found package on ubuntu/debian:

The program 'foo' can be found in the following packages:
* foobar
* barfoo
Try: sudo apt-get install <selected package>
-bash: foo: command not found

But you can customize this with the command_not_found_handle function which you can add to your .bashrc.

This is something I've tried:

function command_not_found_handle {
  ssh $...

Fix error: Invalid gemspec / Illformed requirement

When you get an error like this:

Invalid gemspec in [/opt/www/foo-project.makandra.de/shared/bundle/ruby/1.8/specifications/carrierwave-0.6.2.gemspec]: Illformed requirement ["#<YAML::Syck::DefaultKey:0x7fda6f84d2e8> 1.1.4"]

… the machine's Rubygems needs to be updated.

If that happens on your local machine

  • Manually remove the offending's gem files and specifications. The paths will be something like /usr/lib/ruby/gems/1.8/gems/your-broken-gem and `/usr/lib/ruby/gems/1.8/specificatio…
External content

Evening on Backbone.js/Views w/ Q&A with David Heinemeier Hansson - YouTube

Interesting interview with DHH, where he talks about how they made the new Basecamp feel very fast without using a lot of Javascript (most of Basecamp still lives on the server). The two tricks they used are PJAX and Russian Doll Caching.

How to fix gsub on SafeBuffer objects

If you have an html_safe string, you won't be able to call gsub with a block an match reference variables like $1. They will be nil inside the block where you define replacements (as you already know).

This issue applies to both Rails 2 (with rails_xss) as well as Rails 3 applications.

Here is a fix to SafeBuffer#gsub. Note that it will only fix the $1 behavior, not give you a safe string in the end (see below).

Example

def test(input) ...

How to deal with strange errors from WEBrick

If you get errors from your development WEBrick that contain unicode salad, you are probably requesting the page via SSL. \
Since WEBrick does not speak SSL, so change the URL in your browser to use http instead of https.

The error looked like this for me:
```text
[2012-09-06 11:19:07] ERROR bad URI \000\026\000\020\000'. [2012-09-06 11:19:07] ERROR bad URI \000\026\000\020\000'.
[2012-09-06 11:19:07] ERROR bad Request-Line \026\003\001\000�\001\000\000\177\003\001PHj�\031\006�L��'. [2012-09-06 11:19:07] ERROR bad URI \000\026\000...

See which MySQL database is currently in use

When you work in the MySQL console and you want to see which database is used, type:

SELECT database();

The result you see is the database you would activate with

USE database_name;

Manage ssh keys with Keychain

Keychain helps you to manage ssh and GPG keys in a convenient and secure manner. It acts as a frontend to ssh-agent and ssh add, but allows you to easily have one long running ssh-agent process per system, rather than the norm of one ssh-agent per login session.

This dramatically reduces the number of times you need to enter your passphrase. With keychain, you only need to enter a passphrase once every time your local machine is rebooted. Keychain also makes it easy for remote cron jobs to securely "hook in" to a long running ssh-agent p…

Paperclip: Move attachements from local storage to AWS S3

We frequently use the handy Paperclip Gem to manage file attachments.

If you need to move the files from local storage (i.e., your servers' harddisk) to Amazon S3, you can simply change settings for Paperclip to use the S3 storage adapter and use this script to migrate to S3. Put the snippet into a chore if you don't want to run that in the console.
YOUR_LOCAL_STORAGE_MODEL_DIRECTORY should be something like 'storage/your_model'.

Dir.glob(YOUR_LOCAL_STORAGE_MODEL_DIRECTORY**/*).each do |path|...
External content

defunkt/jquery-pjax

pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades.

pjax enhances the browsing experience - nothing more.

Hack of the day: Find all classes that define a method with a given name

If (for some reason that you don't want to ask yourself) you need to know all classes that define a method with a given name, you can do it like this:

def classes_defining_method(method_name)
  ObjectSpace.each_object(Module).select do |mod|
    (mod.instance_methods & [method_name.to_s]).any? && mod.instance_method(method_name).owner == mod
  end
end

So, when we want to know all classes that define a to_s on a plain IRB, this is what the result looks like:

>> classes_defining_method(:to_s)
=> [IRB::Conte...

How to test print stylesheets with Cucumber and Capybara

A print stylesheet is easy to create. Choose a font suited for paper, hide some elements, done. Unfortunately print stylesheets often break as the application is developed further, because they are quickly forgotten and nobody bothers to check if their change breaks the print stylesheet.

This card describes how to write a simple Cucumber feature that tests some aspects of a print stylesheets. This way, the requirement of having a print stylesheet is manifested in your tests and cannot be inadvertedly removed from the code.

Our example —…

View
3222 cards