acts_as_dictionary Rails plugin digg delicious su

Posted by tiendung
on Friday, September 05
A Rails plug-in created by Jason to treat any ActiveRecord's string field as a dictionary to do word suggesting & spelling checking. I helped to replace buggy hunspell gem with our own Ruby Hunspell binding.

As far as I know, Hunspell is the best open source spelling checking library. It is used by Firefox 3, Google Chrome, and Open Office. I also found Hunspell is easier to use than Aspell.

Get the plug-in at http://github.com/tiendung/acts_as_dictionary/tree/master

ActsAsDictionary
================

ActsAsDictionary creates Hunspell (http://hunspell.sourceforge.net/) dictionaries using data from ActiveRecord models and provides class methods to access these dictionaries.

Pre-requisites
==============

This plugin requires the Hunspell library and RubyInline gem.

To install Hunspell:

curl -O http://nchc.dl.sourceforge.net/sourceforge/hunspell/hunspell-1.2.7.tar.gz
tar -zxvf hunspell-1.2.7.tar.gz
cd hunspell-1.2.7
./configure
make
sudo make install

To install RubyInline:

gem install RubyInline

Setup
=====

Within a model, define the column you want to work with:

class Klass < ActiveRecord::Base
acts_as_dictionary :checks => :name
end

Or, if you want to create dictionaries using two or more columns

class Klass < ActiveRecord::Base
acts_as_dictionary :checks => [:name, :type]
end

Once done, you can proceed to generate basic Hunspell dictionary files with:

rake dict:generate

In the above example, the following files will be generated:

#{Rails.root}/dict/klass_names.aff
#{Rails.root}/dict/klass_names.dic
#{Rails.root}/dict/klass_types.aff
#{Rails.root}/dict/klass_types.dic

Each pair of these files (*.aff and *.dic) are Hunspell dictionary definition files which you can further configure if needed (see http://sourceforge.net/docman/display_doc.php?docid=29374&group_id=143754).

Note that if manually configure the files, running the dict:generate Rake task again will override your manual configurations.

Usage
=====

With this plugin, the following dynamic class methods are available (replace * with a column name you have specified to check):

>> Klass.find_by_*_with_spell_check("Naem")
=> #

>> Klass.suggest_*("Naem")
=> ["Name", "Name2"]

>> Klass.check_*("Name")
=> true


Tien Dung
http://free-and-happy.blogspot.com/

Rails plugin to combine CSS and Javascript files digg delicious su

Posted by jason
on Friday, July 27

While multiple CSS and Javascript files aid manageability and facilitate a modular style of development, HTTP performance takes a hit because multiple requests are sent to the server for each of these files.

When we first launched HBO Asia’s recently revamped site, we found that the response time for pages to load was somewhat slow and thought that the many separated Javascript and CSS assets we were using might be a contributing factor.

David Heinemeier Hansson’s keynote at Railsconf 2007 mentioned that the upcoming Rails 2.0 will have a feature to combine all Javascript and CSS assets to help with this problem. In the meantime, however, we made do by manually combining the assets.

True enough, after doing so, the site’s performance was a little better. However, maintainability was compromised and every time I updated the combined files, I would have to update the individual files as well (as other parts of the site that do not need all the assets might be referencing the individual files).

Tired of such unproductivity, I decided to search for a Rails plugin that can automate this process and found CachedAssets. I’m currently testing this on development and will push this out to production soon if all goes well.

Naturally sort strings with numbers using Rails digg delicious su

Posted by jason
on Monday, July 16

A recent project required natural sorting of strings with numbers in them, i.e. in the order:

Series 1 
Series 2 
Series 3 
Series 10 
Series 23 
Series 35 

Sorting these strings 'unnaturally' would result in the following:

Series 1 
Series 10 
Series 2 
Series 23 
Series 3 
Series 35 

I was kind of surprised that Ruby nor Rails had this feature built-in. However, thankfully, the open-source community helped me solve my problem without cracking my brains too much. What I found were these:

The first solution extends the Array object with two methods: natcmp and natcmp!. Both of which will naturally sort an array of strings:

array_of_strings = ["Series 10", "Series 23", "Series 3", "Series 35", "Series 2", "Series 1"] 
array_of_strings.natcmp! # => ["Series 1", "Series 2", "Series 3", "Series 10", "Series 23", "Series 35"] 

The second piece of code adds a class method to the String object, accepting two strings as parameters to be sorted, returning either 1 or -1 (similar to the <=> method). I eventually used this solution, as what I needed to sort was an array of ActiveRecord objects, which I could do as follows:

array_of_records = Series.find(:all) 
array_of_records.sort! {|x,y| String.natcmp(x.name, y.name)} 

On bugs and such... digg delicious su

Posted by jason
on Monday, June 25

I was stuck for several hours last week trying to solve a CSS problem that occurred with the following code:

div#content.home { .. }

div#content.secondary { .. }

The above CSS was applied to a page with this XHTML:

<div id="content" class="secondary">..</div>

The problem was that on IE6, the div#content.secondary selector does not affect anything on the div it was targeting. I tried adding margins, background colors, etc. but nothing would get any attributes declared within div#content.secondary to show up on IE6.

What I found out was: if I removed the div#content.home selector, everything works fine. Previously, when faced with such problems, it was usually the case that there was a silly syntax error somewhere, either within the CSS or XHTML. So I stripped the markup and styles down to its bare minimum (to try and see if I could spot any errors), validated the XHTML and CSS, but again, nothing worked.

I also tried switching the position of the selectors, putting div#content.secondary before div#content.home, and voila, it worked, albeit only for this page, as the problem reared its head on another page, with the div#content.home selector.

I was then several hours into this problem, and decided to leave it till the next day, when I would perhaps be mentally fresher and better primed to tackle the problem.

The next morning, after several attempts to solve the problem to no avail, I decided to Google the web to see if anything could be found and sure enough, I saw the following on css-discuss: Missing class assigments and Style rule based upon ID and Class?

Both the above links succinctly describe the problem as a bug on IE6 (for the record, this bug is fixed in IE7) so I won’t go into the details, but what I eventually did was not to use div#content.secondary at all and instead used div#content to set the styles I originally wanted for <div id="content" class="secondary"> and used div#content.home to override the styles I didn’t want to apply to <div id="content" class="home">..</div> (works because of CSS specificity).

This brings me to my next point: the problem of quickly identifying bugs that have already been solved by others before.

With situations as such, it isn’t immediately apparent what a particular bug is, until you narrow down the problem and have a few good keywords to search for. At this point, you’d probably be several hours into the problem already.

The same thing happened previously when I came across what I eventually found to be a bug within prototype.js (whereby Hash.toQueryString() would encode array values twice) and was even ready to submit a patch to Rails Trac, only to find that the problem was already solved.

Oh well, hope this post might help prevent you from spending the same amount of time I spent trying to solve the above-mentioned problems (that is, if you haven’t already spent the time narrowing down the problem(s) before landing here).