Mongoid Cache Sweeper

I just needed to add a Cache Sweeper in a Rails 3.1 app for a Mongoid model.

Using a ActionController::Caching::Sweeper didn’t run for updates on the models (even though it should work for non-Mongoid-models), so I ended up using a Mongoid::Observer to invalidate the cache when a model was updated.

To seperate the observers from the models, I created a app/observers directory and used the following code to automatically load the observers:

# mongoid observers/sweepers
config.mongoid.observers = Dir["#{config.root}/app/observers/*.rb"].collect do |full_name|
  File.basename(full_name,'.rb').to_sym
end

This is the app/observers/city_observer.rb file:

class CityObserver < Mongoid::Observer
  def after_create(city)
    expire_cache_for(city)
  end

  def after_update(city)
    expire_cache_for(city)
  end

  def after_destroy(city)
    expire_cache_for(city)
  end

  private

  def expire_cache_for(city)
    # expire fragments/pages for city
    @c ||= ActionController::Base.new

    @c.expire_fragment("city_#{city.id.to_s}")
    if city.state.present?
      @c.expire_fragment("state_#{city.state.id.to_s}")
    end
    if city.country.present?
      @c.expire_fragment("country_#{city.country.id.to_s}")
    end

    # and expire the continent page cache
    @c.expire_page("/continents/#{city.country.continent.slug}")
  end
end

Since the expire_fragment/expire_page functions are not defined in the observer, I create a ActionController::Base object to call them on that.

With this, a model update expires cached pages

OS X: Ignore USB Storage device temporarily

I needed to ignore an USB storage device, or at least disable the automount feature for it, because I wanted the USB device to be available for VirtualBox.
There does not seem to be an device-specific ignore functionality, but in my case it was enough to temporarily unload the USB storage kext:

sudo kextunload /System/Library/Extensions/IOUSBMassStorageClass.kext

This will disable all USB storage media temporarily!
When you are finished using the USB storage device in VirtualBox, shut it down, and then do a

sudo kextload /System/Library/Extensions/IOUSBMassStorageClass.kext

to restore the USB storage functionality.

Open SSH Connections with Butler

How to use Butler, screen, SSH and some shell Skripts to open SSH Connections to some servers.
First, create a directory which will contain the script(s) to connect to the servers – I use ~/bin/Terminals. In that, place a file containing the following code:

#!/bin/bash
exec ssh -t `basename $BASH_SOURCE` screen -DR ; exit

The filename should be the servername to connect to, you can use ~/.ssh/config to define short aliases for your servers:

Host myshortalias HostName long.hostname.tld User ausername

This allows you to do ssh myshortalias to connect to the server in question. So, to connect to this server, name the file in ~/bin/Terminals myshortalias.
Make the myshortalias file executable: chmod 755 myshortalias
Try to open a connection via the myshortalias file: ./myshortalias — if this fails, you problably don’t have screen installed on the server – I really recommend to install it.
OK, if that works, disconnect from the server, and open the Butler Configuration.
Expand the “Hidden” elements, if they are not visible. Select “Add File” from the plus button. Navigate to the ~/bin/ folder and select the “Terminals” folder to add it to the Butler Configuration.
Select the “Terminals” container, open the “Triggers” tab, assign a Hot Key, change mode to “Opens a menu near the mouse”.
Select the “Terminals” folder, select the “Menu Filter” tab, change ‘Menus show…’ to “this folder’s content with X levels of recursion”, activate “Translate subfolders into menus”.
Select the “Options” tab and change the ‘Open with…’ setting to “Terminal” (or iTerm or …).
Pressing your hot key should now show a menu with the myshortalias script. You can add subfolders to the ~/bin/Terminals folder to group your servers. To add a server, just copy (or even better: hard-link) the myshortalias file to a new name which corresponds to a Host config in ~/.ssh/config.

Ikea Rahmengrössen

Die Grössen der IKEA Bilderrahmen Reslig und Ribba inklusive Passepartout-Grössen (jeweils in cm):
h2. Reslig:
http://www.ikea.com/de/de/catalog/products/10094060
|*Grösse*|*Preis*|
|21×30|4,99|
|30×40|7,99|
|40×50|8,99|
|50×70|12,99|
|70×100|16,99|
h2. Ribba:
http://www.ikea.com/de/de/catalog/products/20078333
|*Grösse*|*Passepartout*|*Preis*|
|13×18|9×14|5,99|
|14×14×4,5|-|6,99|4,5cm tief|
|18×24|12×17|6,99|
|23×23×4,5|12×12|9,99|4,5cm tief|
|30×40|20×29|9,99|
|30×88|12×17 (5*)|16,99|Passepartout für fünf 12×17 Bilder|
|40×50|29×39|11,99|
|50×23|12×17 (3*)|11,99|Passepartout für drei 12×17 Bilder|
|50×50×4,5|29×29|15,99|4,5cm tief|
|50×70|39×49|16,99|
|70×100|49×69|25,90|

Spanien war schön …

Letzte Woche gabs ein wenig Spanien-Urlaub:

GrössŸere Kartenansicht
Girona, Tossa de Mar (bzw. Camping Cala LLeva), Manresa, Monserrat, Barcelona-Sightseeing, Sant Pere Pescador (bzw. La Gaviota Camping), Cap de Creus und dann leider Rückflug.
Cala Lleva ist ein sehr schöner Campingplatz, alles auf Terassen in einem Pinien(?)-Wald, sehr schön gelegen — und ziemlich bergig. Vier Strandbuchten, die größte westliche war sehr nett!
La Gaviota ist ein kleiner, netter Campingplatz, direkt am 15km langen Sandstrand — was will man mehr?
Spanien .. ich komme wieder 😉
Bilder folgen nach und nach auf Flickr.

Wochenend-Fazits …

Man kann an einem Abend dank Digital-Kamera 611 Fotos schiessen
Man muss dann allerdings auch 611 Fotos sichten und bewerten
Man kann dank Digital-Kamera die 611 Bilder nachbearbeiten
Dummerweise entwickelt man auch den Ehrgeiz, alle 611 Bilder nachzubearbeiten
Erstaunlicherweise wurden die Bilder alle auf einem Akkusatz gemacht, sowohl der Kamera-Akku als auch die Eneloops im Blitz haben durchgehalten!