botvector.net domain for sale $2k usd (negotiable). Em@il me t0: alex DOT creopolis at gmail DOT com

My Git activity

19 November 2007

Encoding problems

First dump initial, non-utf database:


mysqldump -uUSER -pPASSWORD --default-character-set=latin1 --skip-set-charset dbname > dumpfile.sql

then (linux) change all latin1 in dumpfile.sql to utf8

sed 's/latin1/utf8/g' dumpfile.sql > dumpfile_collate_utf8.sql

Optionally gzip and send to localpc
gzip -d dumpfile_collate_utf8.sql.gz


Recreate same database with utf8 collation and import dumpfile_collate_utf8:

mysql -uUSER -pPASSWORD --execute="DROP DATABASE dbname;
CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql -uUSER dbname < dumpfile_collate_utf8.sql


Now the next part, get on Rails


I've set RAILS_GEM_VERSION = '1.1.6' to be sure we are in non unicode.
1. Create controller with:

require 'iconv'
require 'singleton'
class ConverterController < ApplicationController
end

Inside create two actions:

def convertme
rows = Model.find(:all)
for row in rows
row.title = iconvert(row.title, "latin1", "utf8")
# replace latin1 with appropriate encoding
row.save!
end
end

def iconvert(str, encoding_from, encoding_to = "utf8")
i = Iconv.new encoding_to, encoding_from
utf_str = ""
begin
utf_str << i.iconv(str)
rescue Exception => e
utf_str << e.success
ch, str = e.failed.split(//, 2)
utf_str << "?"
logger.info "FAIL !! char:#{ch}"
retry
end
return utf_str
end


Point browser/console to /converter !

Thats IT !
What doing iconvert: its taking string, trying to convert it to utf-8, if conversion fails (on some japanese/korean) it put "?" instead. Thats because some non-utf encoding cannot be converted to utf-8(needs to be utf-16 or utf-32).

Also in case of large database conversion, i suggest to add column to database table, like "utf_converted" and do conditionally find(:all) and then row.utf_converted = true before save, just to be safe if something fail, to do not convert twice.

01 October 2007

Rails 2.0 :: Debugger

To tie it all together, we have a stream of improvements for Rails in general. My favorite amongst these is the return of the breakpoint in form of the debugger. It’s a real debugger too, not just an IRB dump. You can step back and forth, list your current position, and much more. It’s all coming from the gracious note of the ruby-debug gem. So you’ll have to install that for the new debugger to work.

To use the debugger, you just install the gem, put “debugger” somewhere in your application, and then start the server with —debugger or -u. When the code executes the debugger command, you’ll have it available straight in the terminal running the server. No need for script/breakpointer or anything else. You can use the debugger in your tests too.

More on Rails 2.0

Rails 2.0 :: Migrations

As you may know Rails 2.0 is on the way (however there will be several RC's), but there is new format of migration:
Before you’d write:


create_table :people do |t|
t.column, "account_id", :integer
t.column, "first_name", :string, :null => false
t.column, "last_name", :string, :null => false
t.column, "description", :text
t.column, "created_at", :datetime
t.column, "updated_at", :datetime
end

Now you can write:

create_table :people do |t|
t.integer :account_id
t.string :first_name, :last_name, :null => false
t.text :description
t.timestamps
end


More on Rails 2.0

20 September 2007

Painless PNG plugin MOD

Thru the pains of PNG in (IE <img>'ing png icons..

First i've started from HERE.
Go here and read some instructions that are simple ( i dont want to repost them here!)

I've been uploaded updated painless_png.zip completely modified and working to my blog.
With this modifications (by Naixn and some by me) you *dont* need any modification to your css/html/ruby code.

1.Unpack content of ZIP into vendor/plugin
2.Copy blank.gif from ZIP to public/images.

Restart server and enjoy !

DOWNLOAD HERE

09 September 2007

What is that ?

Does anyone can define this function ?

range.inject(Hash.new(0)){|h,e| h[e] += 1; h}


range is array of integers like: [1,22,12,8,45,3,etc..]

02 September 2007

:conditions => ["created BETWEEN...

Best way to get query by "from" date and "till date" is:


range = "created_at #{(12.months.ago..Time.today).to_s(:db)}"

Will result in creating String = "created_at BETWEEN "03-09-2006" AND "03-09-2007""
AND then

@data = MyModel.find(:all, :conditions => ["code=? AND #{range}", 2])

link_to_back helper

It will take referer, @params and redirect you back to same page you been before.

Useful then sorting search results, paging, editing different objects via same _form...
This is code for helper file, i prefer application helper.


def link_to_back (description = "Back")
referer = request.env["HTTP_REFERER"]
return false if !referer
getIt = request.env["REQUEST_URI"].split("?")[1]
if getIt.nil?
getIt = ""
else
getIt = "?" + getIt if !getIt.match(/\?/)
end
link_to description, referer + getIt
end


then just do:
<%= link_to_back %> or
<%= link_to_back "Cancel" %>

29 August 2007

Globalization AddOn Mk::2

Im sure everyone is enjoined himself with globalization plugin, what a features, what abilities..

However one thing is veeerry disappearing me, (or maybe i just dont know how-to), NO option to know status of translations…


Okay, imagine we have Staticpage model with some fields to be translated.
so in .rb >

translates :name, :abstract, :body

thats ok, rest of stuff is “automatically” mapped by globalization plugin.
BUT (or butt:) ! in views/staticpages/list.rhtml you dont have anything that tell you about translation in different languages.
so you need to shake a lizard and do some work, look here:
in model:

def translation_languages
reply = “”
LOCALES.each do | local |
iso639 = local[0]
flag = local[1].slice(3..4).downcase + “.png”
if iso639 != “en”
# do not display “en” language
n = ModelTranslation.find :all,
:conditions => [”globalize_languages.iso_639_1 = ? AND globalize_translations.item_id=? AND globalize_translations.language_id = globalize_languages.id”, iso639, id],
:joins => “INNER JOIN globalize_languages”
if n.size == 0 #|| Locale.language_code != iso639
tClass = “tr_incomplete”
tTitle = “Not translated”
else
tTitle = “Translated”
tClass = “tr_complete”
end
reply+= “span class=’#{tClass}’ title=’#{tTitle}’>img src=’/images/flags/#{flag}’ /> /span>”
end
end
return reply

end

when in list.rhtml:

<%= staticpage.translation_languages %>

will render all flags with “Translated” “Not Translated” titles.

you must have /images/flags/*.png appropriate flags, also you must have two css classes tr_incomplete and tr_complete.