July 30th, 2009 by Andrea Franz
Many people found out a rails generator inside my web-app-theme project and asked me how to use it.
Here an example, starting from scratch with a new rails app that manages music Albums.
rails cool_albums
cd cool_albums
script/generate scaffold Album name:string artist:string date:date
rake db:migrate
After creating the first controller with a scaffold or with your hands, start creating a theme:
script/plugin install git://github.com/pilu/web-app-theme.git
script/generate theme application --app_name="My Cool Albums" --theme="drastic-dark"
The first argument (“application”) is the name of the layout that the generator will create (application.html.erb).
The –app_name option specifies the name used as page title, and with the –theme specifies which theme to use among all the available themes inside the plugin.
Now remove the default index.html created by rails and the layout created by the scaffold:
rm app/views/layouts/albums.html.erb
rm public/index.html
Add the following line in your routes.rb to set the default page of the application:
map.root :controller => :albums
Start the server

Ok, the layout has been successfully created, but we need to apply a theme for each one of the views generated by the scaffold.
script/generate themed albums album --layout=application --with_will_paginate
With the first 2 arguments I specified the controller path (albums) and the model used (album).
The –layout options is used by the themed generator to know where to add the Albums menu link.
Since we want to use will paginate (we set the –with_will_paginate option), we need to change one line in our albums controller from:
to:
@albums = Album.paginate(:per_page => 10, :page => params[:page])
Here a trick to show form error messages inside the auto generated forms, you can add the following lines in your environment.rb:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<label/
%|<div class="fieldWithErrors">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>|
else
html_tag
end
end
Ok, restart your server and you are done.

Feel free to fork the project from github to improve the generator or to add a new theme.
March 17th, 2009 by Andrea Franz
Terror is a micro feed aggregator made with sinatra and bundled as a gem. It’s very simple to use, just install the gem:
gem sources -a http://gems.github.com
sudo gem install pilu-terror
Create a new project
terror new_aggregator_name
cd new_aggregator_name
Start the server
thin start -C config/thin.yml
Run the feeds fetcher
rake feeds:fetch # run it as a cron job
You can browse the source and fork it on github.

Filed under: Development,
Ruby,
Sinatra |
Tags: Ruby,
Sinatra |
Comments Off
January 15th, 2009 by Andrea Franz
Some people in the past had problems using the Gallery extension on Dreamhost. I didn’t try Dreamhost but someone from the Radiant list solved that problem following these instructions. Check it out if you are on Dreamhost too.
May 2nd, 2008 by Andrea Franz
In my current rails project I need to upload photos and save some exif data taken from them. I use attachment_fu as uploading system that let me choose which image processor to use. Using rmagick and mini_magick I can extract exif data with the following code:
# rmagick
image = Magick::ImageList.new(filename).first
puts image['EXIF:Model'] # The camera model used to take the picture
# mini_magick
image = MiniMagick::Image.from_file(filename)
puts image["EXIF:Model"]
The problem is that I can’t do the same thing with image_science, because it has no methods that return exif data, so I want to add a method to the ImageScience class to do that.
Looking the FreeImage documentation I found some helpful functions, FreeImage_GetMetadata and FreeImage_TagToString. With these 2 functions I’m able to get an exif tag and convert it to a readable string. Each one of the available tags belongs to one of the following meta models:
FI_ENUM(FREE_IMAGE_MDMODEL) {
FIMD_NODATA = -1,
FIMD_COMMENTS = 0, // single comment or keywords
FIMD_EXIF_MAIN = 1, // Exif-TIFF metadata
FIMD_EXIF_EXIF = 2, // Exif-specific metadata
FIMD_EXIF_GPS = 3, // Exif GPS metadata
FIMD_EXIF_MAKERNOTE = 4, // Exif maker note metadata
FIMD_EXIF_INTEROP = 5, // Exif interoperability metadata
FIMD_IPTC = 6, // IPTC/NAA metadata
FIMD_XMP = 7, // Abobe XMP metadata
FIMD_GEOTIFF = 8, // GeoTIFF metadata
FIMD_ANIMATION = 9, // Animation metadata
FIMD_CUSTOM = 10 // Used to attach other metadata types to a dib
};
Ok, now I can extract the model of the camera:
FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Model", &tag);
printf(FreeImage_TagToString(FIMD_EXIF_MAIN, tag, NULL));
As you can see, I need to pass the model of the “Model” tag. But if I don’t know which model to use, I can loop through all of them until the returned value of the FreeImage_GetMetadata function is not NULL:
for(model = 0; model < 11; model++) {
if(FreeImage_GetMetadata(model, bitmap, tagName, &tag))
return rb_str_new2(FreeImage_TagToString(model, tag, NULL));
}
Finally I can write a ruby module that extends ImageScience and adds the ability to get an exif tag:
module ImageScienceExifData
def [](key)
if key =~ /^EXIF:(\w+)?/
get_exif($1)
end
end
inline do |builder|
if test ?d, "/opt/local" then
builder.add_compile_flags "-I/opt/local/include"
builder.add_link_flags "-L/opt/local/lib"
end
builder.add_link_flags "-lfreeimage"
builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
builder.include '"FreeImage.h"'
builder.prefix <<-"END"
#define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
END
builder.c <<-"END"
VALUE get_exif(char *tagName) {
GET_BITMAP(bitmap);
FITAG *tag = NULL;
const char *value;
int model;
for(model = 0; model < 11; model++) {
if(FreeImage_GetMetadata(model, bitmap, tagName, &tag))
return rb_str_new2(FreeImage_TagToString(model, tag, NULL));
}
return Qnil;
}
END
end
end
ImageScience.send(:include, ImageScienceExifData)
ImageScience.with_image(filename) do |img|
puts img["EXIF:Model"]
end
The output with the picture I used is the following :
NIKON
COOLPIX S3
2006:12:10 12:09:17
It doesn’t work with all the exif names but for now it’s ok for my needs. The next step is to add the code above in my rails application and use it with attachment_fu. I’ll write another post about that soon.
March 27th, 2008 by Andrea Franz
Some weeks ago Casper Fabricius sent me a patch for the Radiant Newsletter extension. He added a statistics system to track how many times sent emails are opened. I have finally found the time to apply it and make a commit to my repository. Thank you very very much for your work Casper! I’ll write an article about this extension as soon as possible.