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

External content

Flat Icons & Icon Fonts | CSS-Tricks

Nice list of icon sets that come in the form of fonts.

I recommend Font Awesome.

Impressumspflicht bei Facebook & Twitter: Schnelle Info und Abhilfe

Jedes nicht rein private Profil auf Social Media Portalen muss ein Impressum haben, das heißt insbesondere Unternehmen und Freiberufler (§ 5 Abs.1 TMG). Wer das Impressum dort nicht unter einem eigene Button mit der Aufschrift “Impressum” oder “Kontakt” unterbringt, bietet eine große Angriffsfläche für Abmahner (Quelle: http://rechtsanwalt-schwenke.de/impressumspflicht-bei-google-plus-und-twitter/).

Hier also in kurzen Schritten, wie man ein Impressum erstellt und wie man es bei Facebook und Twitter einbindet:

#1. Impressum erstellen

Ic…

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.

Reset mysql root password

Stop the MySQL Server.

sudo /etc/init.d/mysql stop

Start the mysqld configuration.

sudo mysqld --skip-grant-tables &

Login to MySQL as root.

mysql -u root mysql

Replace YOURNEWPASSWORD with your new password!

UPDATE mysql.user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;

If this fails (ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)):
Login into the mysql database with the debian-sys-maint user (you find the password in /etc/mysql/d…

How to install GNOME classic desktop in ubuntu 12.04

Run

sudo apt-get install gnome-session-fallback

or (alias)

sudo apt-get install gnome-panel

from a terminal.
Then logout to reach the logon screen and click on the options button next to you name.
Select Gnome Classic, log in et voilà.

Scoping a sunspot solr search by text using a string field

Assuming the following sunspot setup of the post class:

class Post < ActiveRecord::Base
  searchable do
    text :title
    string :state
    integer :category_ids
  end
end

In Sunspot you can scope your search via the with method. It allows you to do stuff like:

Post.search {
  fulltext "your query", :fields => :title
  with(:category_ids).any_of([1,2,3,4,5])
}

If you want to scope your search based on a text field, you have to add another field of the type string (such as the state fi…

How to inspect controller filter chains in specs

Sometimes you need to look at the filter chain in specs. You can do it like that on Rails 2:

controller.class.filter_chain.map(&:method)

Note that we need to look at the controller's class since before_filter and after_filter stuff happens on the class level.

Also mind that the above code will give you all filters, both those run before and after an action. You can query before? and after? on the filter objects to scope down to only some of them:

controller.class.filter_chain.select(&:before?).map(&:method)

For Rails 3, …

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

Be careful when using Unicode symbols for graphical elements

There are many fun Unicode characters like ▲ or ☯. You might be tempted to use them for graphical elements in lieu for an image. After all they are so much easier to style and scale than a raster image!

Unfortunately you will discover that these symbols render very differently on Linux, Windows and MacOS. The reason for this is that the font you are using will probably not contain any characters outside the standard Latin-1 set. When browsers encounter a character not included in the current font, they use a fallback font for this one cha…

RSpec and Cucumber: Shorthand syntax to run multiple line numbers in the same file

This works in modern RSpecs (RSpec >= 2.x) and Cucumbers:

rspec spec/models/node_spec.rb:294:322
cucumber features/nodes.feature:543:563:579

Also your features should be shorter than that :)

Git: When committing, check the diff

When committing, you should always check the diff of your changes so you don't include any leftovers or irrelevant/bad changes. Any time spent on that is well invested as it saves you rejected merge requests or any mess you need to clean up later.

While you can just use git diff or git diff --cached (to diff staged changes), you can also have Git include the changes as a comment below the commit message:

git commit -v

That will open up the usual commit "file" in your preferred text editor, but it will include a diff below the s…

External content

ActiveRecord::SpawnMethods

Methods to remove e.g. order or conditions from an existing scope chain.

View
3163 cards