Archive for the ‘Ruby’ Category

RESTful In-place form editing

Friday, May 9th, 2008

We have rewritten the rails in-place editor, described in the rails recipes book by Chad Fowler, in RESTful design. You can install it as a plugin (http://source.simplificator.com/svn/plugins/inplace/).

If you use special characters like umlauts, mute vowels or japanese characters patch your prototype.js with prototype-umlaut-patch.patch. The patch is needed because the original version of prototype.js doesn’t decode the mutated vowels correctly.

Patch: prototype-umlaut-patch.patch

Custom Date/Time formats in Ruby (on Rails)

Sunday, April 13th, 2008

In Switzerland we use a different formatting for date/time than in the USA (in fact most of Europe uses different formatting than the USA). Ruby (on Rails) is very centred on US coders and US applications and this can sometimes be a source of confusion and frustration. So let’s add some custom formats to Rails so we can not only to Date.today.to_s(:short) but also Date.today.to_s(:my_custom_date_format)

Rails has a built in set of formats which is stored in ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS and ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS and all you have to do is to add your own formats to this hash.
In config/initializers create a file date_formats.rb. This file we can use to register custom formats withouth messing up our environment.rb. If you need this often you could also extract it to a plugin so you can “install” your custom dateformats everywhere with a single script/install. In this file you can now merge your own formats to the existing ones or change existing formats:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:my_format => ‘%d. %m. %Y’)

The expressions you can use for formatting can be found in the source of Date.strftime. Or with this little piece of code:

>> (’a’..’z').each do |c|
?> expression = “%#{c}”
>> puts “#{expression} -> #{Date.today.strftime(expression)} / #{expression.upcase} -> #{Date.today.strftime(expression.upcase)}”
>> end
%a -> Sun / %A -> Sunday
%b -> Apr / %B -> April
%c -> Sun Apr 13 00:00:00 2008 / %C -> 20
%d -> 13 / %D -> 04/13/08
%e -> 13 / %E -> E
%f -> f / %F -> 2008-04-13
%g -> 08 / %G -> 2008
%h -> Apr / %H -> 00
%i -> i / %I -> 12
%j -> 104 / %J -> J
%k -> 0 / %K -> K
%l -> 12 / %L -> L
%m -> 04 / %M -> 00
%n ->
/ %N -> N
%o -> o / %O -> O
%p -> AM / %P -> am
%q -> q / %Q -> Q
%r -> 12:00:00 AM / %R -> 00:00
%s -> 1208044800 / %S -> 00
%t -> / %T -> 00:00:00
%u -> 7 / %U -> 15
%v -> 13-Apr-2008 / %V -> 15
%w -> 0 / %W -> 14
%x -> 04/13/08 / %X -> 00:00:00
%y -> 08 / %Y -> 2008
%z -> +0000 / %Z -> Z
=> “a”..”z”

For those who need even more you can register a Proc which will be called with the Date/Time to format as an argument:

>> proc = lambda do |what|
?> what.day - 1
>> end

Don’t forget to register your custom formats with Date and Time if you deal with Date and Time formatting!

Some Netbeans Shortcuts

Wednesday, April 9th, 2008

As written before we are pretty happy with Netbeans and it’s Ruby/Rails support. As a previous Eclipse user it took me a while to get used to the keyboard shortcuts (though some are the same as in Eclipse). Here are some of them i use most:

  • Close Document: Meta + W
  • Close all Documents: Shift + Meta + W
  • List Documents: Shift + F4
  • Next/Previous Document: Meta + PageDown / PageUp
  • Run File: Shift + F6
  • Debug File: Shift + Meta + F5
  • Move Line Up: CTRL + Shift + Arrow Up / Arrow Down
  • Ident/Unident: Tab / Shift Tab or CTRL + Shift + Arrow Right / Arrow Left

Oh and if your Mac started talking to you after experimenting with shortcuts: Apple + F5 will switch it of again :-)

Deploying Rails Applications with mod_rails

Friday, March 28th, 2008

Have not tried it yet but sounds promising: mod_rails preview (this green-hand-thing is pretty distracting! Can you create a new screencast without it ;-) )
And here is some more information on it. Anyone used it already ? Leave a comment or contact us!

globalize and haml

Wednesday, March 26th, 2008

If you are using HAML and Globalize then you might need to tweak plugins/globalize/lib/globalize/rails/action_view.rb so HAML templates are actually “transformed”:
Change

@@re_extension = /\.(rjs|rhtml|rxml)$/

to

@@re_extension = /\.(rjs|rhtml|rxml|erb|builder|haml)$/

and HAML works again.

ruby job sites

Tuesday, March 11th, 2008

For those of you out there looking for a RoR poisition there are tons of job websites specialized in Ruby/RoR

nicolepont.ch - fotografin

Monday, March 10th, 2008

Nicole Pont has her new website online. She is a photographer and has a small part of her portfolio online, I hope to see some more pictures soon.
The site is built on RoR and has a simple backend to manage the albums and pictures.

plugin load order and exclusion

Thursday, March 6th, 2008

Yesterday members of the swiss RoR group discussed their favourite plugins (a transcript should be available soon, i guess it will be in german though)
I couldn’t make it to the meeting but it reminded me that i should write down the troubles we had with the (excellent) ultrasphinx plugin by Evan Weaver in combination gibberish db (and virtual enums).

When ultrasphinx plugin is loaded then it scans the models and searches for use of the is_indexed() method. It then “loads” those models with constantize so the is_indexed call is actually executed and those models are configured for ultrasphinx. If any of these classes uses enums or gibberish, they will try initiate a connection to the DB. But what if the DB or the tables are not yet there (think rake db:create:all, rake db:migrate) ? A nasty exception with a missleading errormessage (expected models/bar.rb to define Bar).

Our quick and dirty hack was to not load the ultrasphinx plugin in some cases by messing around with environments.rb (see link below) and this worked.

Resources
Related: Plugin ordering and exclusion in Rails 2.0
Sphinxsearch: Sphinx

Ruby and EXIF Data

Monday, January 14th, 2008

In one of our projects we need to read and write exif data to TIFF and JPEG images. Seems like an easy task because there are so many libraries out there, even with ruby bindings. But if you go into the details of those libraries you’ll soon find that write support is often limited, especially for TIFF images. I found ExifTool to work best for us (since it supports writing to TIFF files) and after I spent an hour or so to create a wrapper for the command line application I stumbled over Mini-Exiftool a ruby gem, which i’ll be test-driving soon.

Resources:

  • Exifr (Exif Reader. Read EXIF information from JPEG and TIFF)
  • Exiv2 (C++ Library, Read and Write EXIF information. No write support for TIFF)
  • Ruby-Exiv2 (Ruby binding for exiv2)
  • Libexif (C Library)
  • Libexif-Ruby (Ruby interface for libexif, did not test it but I read somewhere that it only supports reading)
  • ExifTool (Perl LIbrary/CLI for reading/writing meta information)
  • Mini-Exiftool (a gem which uses the exiftool CLI)

Netbeans and HAML

Saturday, January 12th, 2008

Since a while now Netbeans is our primary Ruby On Rails IDE. In the Quevita project we use HAML for the view generation. First i was rather skeptic about HAML (who needs this ?, no tool support, what if you need to tweak the output for a specific browser…). Now, after some months of HAML I really like it. And with this Netbeans Plugin you get back syntax highlighting.

Resources: