Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Oct 09

BrainBuster Globalized !

Posted by Sandro Paganotti in Ruby on Rails - comments are closed digg this add to delicious

During the last week I created a small patch that allows Brain Buster Logic Capthca plugin to cooperate with Globalize by asking only questions that match the specified language.

To archive this I’ve changed ‘brain_buster.rb’ as follow:



  ...

  def self.find_random_or_previous(id = nil)
    @loc = (Locale.language_code.to_s.blank? ? "en" : Locale.language_code.to_s)
    if id.nil?
      self.find(random_id(first_id, count(:conditions=>["lang = ?",@loc])),:conditions=>["lang = ?",@loc])
    else
      find(id,:conditions=>["lang = ?",@loc])
    end
  end

  private
 ...

  # return first valid id
  def self.first_id
    if find(:all,:conditions=>["lang = ?",@loc], :order => "id").length == 0 
      @loc = "en" 
    end
    @first_id = find(:all,:conditions=>["lang = ?",@loc], :order => "id").first.id
  end


and then I created a migration which add a ‘lang’ column to the brain buster table:


class BusterAndLanguage < ActiveRecord::Migration
  def self.up
    add_column :brain_busters, :lang, :string, :default=>"en" 
  end

  def self.down
    remove_column :brain_busters, :lang
  end
end

That’s it; now we need just to fill the table with questions and answers for the languages we want to add to our application.

Comments

  • Rob Sanheim

    Posted on October 14

    Hey, thanks for this. I'll consider this as I find some time to work on a new release...I'd like to make it easy to globalize without adding a column to AR specifically for it....not sure what the best way would be.
  • Sandro

    Posted on October 15

    Hi Rob, thanks for the comment! The solution I wrote here was the faster for me to archieve my goal. Another good way could be using Globalize to let the admin translate both questions and answers but doing this way there are no possibilities to create questions that apply just to a particular country (eg: I would ask what was the first Italian president only to Italian people). Let me know which solution you will implement :)

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory