Davide Targa website

Programming, web development and open source

Note Sull’utilizzo Di ZopeSkel 3.0a1

Dopo l’installazione di ZopeSkel 3.0a1 con easy_install, ho notato la mancanza di alcuni template fondamentali come ad esempio il template per il buildout di plone 3 (plone3_buildout). Dopo varie ricerche sono giunto alla conclusione che probabilmente è un problema della versione (ancora in alpha) e ho risolto con un downgrade alla 2.13:

1
$ easy_install ZopeSkel==2.13

Riporto di seguito l’elenco dei template disponibili con la versione 2.13 di ZopeSkel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ paster create --list-templates
Available templates:
  archetype:          A Plone project that uses Archetypes
  basic_namespace:    A project with a namespace package
  basic_package:      A basic setuptools-enabled package
  basic_zope:         A Zope project
  kss_plugin:         A KSS plugin template
  nested_namespace:   A project with two nested namespaces.
  plone:              A Plone project
  plone2.5_buildout:  A buildout for Plone 2.5 projects
  plone2.5_theme:     A Theme for Plone 2.5
  plone2_theme:       A Theme Product for Plone 2.1 & Plone 2.5
  plone3_buildout:    A buildout for Plone 3 projects
  plone3_portlet:     A Plone 3 portlet
  plone3_theme:       A Theme for Plone 3
  plone_app:          A Plone App project
  plone_hosting:      Plone hosting: buildout with ZEO and any Plone version
  plone_pas:          A Plone PAS project
  recipe:             A recipe project for zc.buildout
  silva_buildout:     A buildout for Silva projects

Installazione E Utilizzo Di Virtualenv Con Pythonbrew

Qualche tempo fa ho parlato dell’installazione di pythonbrew per il setup e la gestione di più versioni di python sulla stessa macchina. Pythonbrew gesisce anche la creazione di ambienti di lavoro virtuali (venv) tramite virtualenv. Se si desidera utilizzare direttamente il tool virtualenv è necessario installarlo.

Installazione Di Più Versioni Di Python Con Pythonbrew

Per chi sviluppa in Python, può spesso essere utile avere a disposizione sulla stessa macchina più versioni dell’interprete. Installando Python per mezzo dei pacchetti di default della nostra distribuzione, la cosa diventerebbe molto difficile da gestire.

Ecco che in nostro soccorso arriva Pythonbrew, un programma che gestisce in modo automatico e intuitivo la compilazione e l’installazione di differenti versioni di Python sulla home dell’utente.

I18n for Non ActiveRecord (Tableless) Models

Let’s suppose to have a message model with the following attributes: name, email, content. This model is used to implement a contact form. Obviously this model doesn’t require a table, so you need to use a tableless model.

Paperclip Update Failure With Accepts_nested_attributes

I am using paperclip in a Rails 3.0.5 application.

I have the following models:

1
2
3
4
5
6
7
8
class Ad < ActiveRecord::Base
  belongs_to :category
  has_many :images, :dependent => :destroy
  accepts_nested_attributes_for :images,
                                :reject_if => lambda { |attr| attr[:image].blank? },
                                :allow_destroy => true
...
end

Every ad has many images. I manage images through paperclip gem. Here the model for Image:

1
2
3
4
5
6
7
8
9
10
11
12
class Image < ActiveRecord::Base
  belongs_to :ad
  has_attached_file :image,
                    :styles => {:thumb => "100x100>", :medium => "600x600>"}

  validates_attachment_size :image,
                            :less_than => 1.megabyte

  validates_attachment_content_type :image,
                                    :content_type => ['image/jpeg', 'image/png', 'image/gif']
...
end

The form for ad has a fields_for images. The standard update action in the ads controller should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
class AdsController < ApplicationController
  ...
  def update
    @ad = Ad.find(params[:id])
    if @ad.update_attributes(params[:ad])
      redirect_to ad_path(@ad), :notice => t(:ad_update_ok)
    else
      render 'edit'
    end
  end
  ...
end

Unfortunately this doesn’t work because of a bug in this version of Rails. The update action updates the ad but doesn’t remove the related images if the _destroy attribute of the params hash is true.

Get the Name of the Caller Method in Ruby

Sometimes, inside a ruby method may be useful to know which method was the caller. In other words you may want to know which method called another method. To do so, Ruby defines the caller method which returns some useful information about the method that called the current one.

Symbolize Hash Keys in Ruby

In a Rails application YAML files are often used to manage configuration parameters. YAML is a human readable data serialization file format. Let’s say we have a YAML file called constants.yml that contains some configurations for our rails application like the following:

1
2
name: "Albert"
surname: "Einstein"

YAML files can be loaded to be used in a rails application through an initializer. So let’s create a new file inside config/initializers and call it load_constants.rb. The content of this file can be something like this:

1
CONSTANTS = YAML.load_file("#{RAILS_ROOT}/config/constants.yml")

To have something more dynamic, this can be improved to load all the files contained inside an array and to create a constant named like the file. The code to achieve this is the following:

1
2
3
4
5
CONFIG_FILES = ["constants", "config"]
CONFIG_FILES.each do |file_name|
  tmp = YAML.load_file(File.join("#{Rails.root}","config","#{file_name}.yml"))
  eval("#{file_name.upcase} = tmp")
end

Now inside the application we have access to the contant. To get the values inside of it we can write:

1
2
CONSTANTS["name"]
=> "Albert"

In my opinion, using strings as hash keys is a bit unconfortable, don’t you tink? I prefer using symbols instead.

Helpful Git Aliases

If you use Git to Version Control your source code, you may find helpful to define some aliases for the most common tasks such commit, diff, … To define aliases inside git, you have to use the following commands. Here I give only a few examples:

1
2
3
4
$ git config --global alias.ci commit
$ git config --global alias.d diff
$ git config --global alias.l log
$ git config --global alias.s status

jQuery on Rails 3

Ruby on Rails is an open source web application framework for the Ruby programming language. The last version of this framework is the 3.0.0 released Aug 29, 2010. By default, when you start a new Rails application with rails new appname it uses prototype as javascript framework. Now prototype isn’t the best solution for all developers so you might want to use jQuery instead.

Enable Webgl Support in Firefox 4

WebGL is a Web-based Graphics Library. It extends the capability of the JavaScript programming language to allow it to generate interactive 3D graphics within any compatible web browser. Let’s see how we can enable it in Firefox 4!