Sunday, July 29, 2007

Dynamic Date in Rails Fixture

While I was happily creating a fixture to test my newly created Article model, I found out that the <%= Time.now %> that I used in the fixture was dumping nil’s into the db. ;-(

That was suppose to work, at least long time ago… I still recall reading the first Rails book… The rails api didn’t say much about the dates in dynamic fixtures…

Well, let’s be a little skeptical, and say… Time.now.strftime(“%Y-%m-%d %H:%M:%S”).

Ha! It worked!

But it’s a kinda too long…

Hum… what else does google has to offer?

There was a ticket open with the same issue: Time.now in a fixture return nil. Guess I am not the first ;-)

<%= Time.now.to_s(:db) %>

There, this is much prettier ;-)

Oh, here is a bonus link for more Dynamic Fixture stuff!

Saturday, July 28, 2007

Turning off Activerecord timestamp

Context: I was trying to increment an article’s views column every time when a reader has viewed the article.

Problem: Whenever I increment the article.views, article.updated_at will be updated by the rails through magic column. That’s not what I want, since updated_at should mean it was last updated by the author of the article, not the reader?!

I could stay with the new meaning of the updated_at, really last_viewed_at, but being a little stubborn, I decided to try out my luck on google.

Ref 1: hacking activerecord’s automatic timestamps

A lot of good stuff, but a lot of errors, too… didn’t work for me.

Ref 2: Turning off Magic Columns

Ahh… But it’s not so thread safe, is it? ;-) The good thing that I learned from Ref 1.

Ref 3: Database Conventions

A lot of good info, but doesn’t totally solve my problem. At least I know that meta class is very useful in this threading scenario. ;-)

Ref 4: Seeing Metaclasses Clearly

Aha… That’s really tricky ;-)

Ref 5: So how do I use remove_method?

Ref 6: This one is coming from me ;-)
  def increment_views
class << self
def record_timestamps; false; end
end

self.increment! :views

class << self
remove_method :record_timestamps
end
end

Javascript get the local file path in Mozilla

This function gives you the local file path of the file (nsIFile).

function getLocalFilePath(file) {
// file is nsIFile
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var fileHandler = ios.getProtocolHandler("file")
.QueryInterface(Components.interfaces.nsIFileProtocolHandler);
var URL = fileHandler.getURLSpecFromFile(file);
return URL;
}

Thursday, July 19, 2007

svn ignore files and directories

Ignore files and directories
The svn:ignore property can be set on filename patterns and directories.

svn propset svn:ignore PATTERN directory PATH
svn propset svn:ignore directory PATH

Edit property setting
Subversion properties can be edited using svn’s propedit function.

svn propedit svn:ignore PATH
svn propget svn:ignore PATH

PATH is assumed current working directory if not specified. If PATTERN is not specified, an editor window will open and let you edit patterns.

Wednesday, July 18, 2007

ActiveRecord Session for Rails

I know that this is probably all over the Internet, this is just an entry for my own convenience.

  • Uncomment the ‘config.action_controller.session_store = :active_record_store’ from the app/config/environment.rb
  • rake db:sessions:create
  • rake migrate

Restart your server, and you are done!