Posted by Paolo
Thu, 03 Jan 2008 20:53:00 GMT
Yesterday we spent all day dealing with a strange bug… something I want to share with you.
Basically we have an app which runs from CDROM and stores its data in a sqlite3 database file inside the user home. We manage the updates from a remote website downloading and swapping the sqlite database file on the fly.
What we do is something like this:
# download the new DB file
new_data = URI.parse('http://foo.com/new.db').read
File.open("#{home}/new.db", 'w') {|f| f.write(new_data)}
# connect to the downloaded DB file
ActiveRecord::Base.estabilish_connection :adapter => 'sqlite3',
:database => "#{home}/new.db"
# do something with the updated DB
# eg: read data for your updates and use it
# reconnect the original DB
ActiveRecord::Base.remove_connection
ActiveRecord::Base.estabilish_connection :production
# remove the downloaded DB file
rm "#{home}/new.db"
We tought that calling remove_connection would suffice to disconnect the new.db database, but when we tried to delete the file (the last line) we sadly discovered that under Windows it was kept locked by our own process even if no database connections were active on it. The problem seems to be inside the sqlite_adapter that once connected to a sqlite database, never calls the close method, leaving it locked forever.
So how do we close those open connections?
Thanks to ObjectSpace we can inspect all the sqlite database descriptors (SQLite3::Database instances) in our application and close them by hand. Voilà, this way we can safely delete our temporary database files, without cluttering our user’s home dir.
def disconnect_all_sqlite3
sqlite3_descriptors.each {|c| c.close }
end
def sqlite3_descriptors
open_descriptors = []
ObjectSpace.each_object(SQLite3::Database) {|x| open_descriptors << x}
open_descriptors
end
Just put a disconnect_all_sqlite3() call before the ActiveRecord::Base.estabilish_connection :production and everything will start working as it should!
Socialize it:
Posted in Ruby + Rails | Tags Ruby On Rails, sqlite3 | 2 comments | no trackbacks
Posted by Paolo
Sat, 08 Sep 2007 01:29:00 GMT
Are you interested in Rails Workshops, training courses, or related events around the world and you don’t know how to find them?
Geoffrey Grosenbach (author of great peepcode screencasts and many other things) has set up rubyonrailsworkshops.com, a calendar for Ruby on Rails events. You can subscribe to your country’s RSS feed and finally stay up to date with interesting stuff happening near you.
Thanks Geoffrey for adding our workshop too!
Socialize it:
Posted in The outer world | Tags Ruby On Rails, workshop | no comments | no trackbacks
Posted by Paolo
Mon, 03 Sep 2007 14:14:00 GMT
Hi guys, I just gotten back from my one-week vacation in Egypt and imagine what I found on my email inbox? Wow… a bunch of people are giving us feedback on the new Tabnav widget just a few days after its public release.
So let’s dig some changes between the old version and the new one:
Tabnav didn’t highlight with URLs beginning with a slash
Let’s consider this scenario:
add_tab do |t|
t.named 'Cat'
t.links_to :controller => '/cats'
end
This didn’t correctly highlight on :controller => 'cats' beacuse of the leading slash of the controller parameter. Now thanks to Cédric Deltheil this issue as been solved. I’ve never experienced this problem myself… It’s difficult to fix something if you don’t know it’s broken :-) (Cédric pointed me to an old Peter Salomon comment rearding this issue. I didn’t get notified about that comment… so I apologize. I start hating typo, I promise we’ll move to a new blogging system soon).
Where’s my beloved show_if method?
show_if was a very useful method in the old Tabnav. You could set a condition to show or hide specific tabs (eg Administration tabs). Well, we got rid of that.
With the new Widgets version the Tabnav definition is backed up by a partial, no more a specific model. That allows us to use pure ruby conditional code to add tabs whenever we want to. And that’s just because the tabnav definition gets reevaluated for every request.
One example is better that a thousand words: look at the if modifier at the end of the 2nd tab:
<%
render_tabnav :main, :generate_css => true do
add_tab do |t|
t.named 'Home'
t.links_to :controller => 'home'
end
add_tab do |t|
t.named 'Users'
t.links_to :controller => 'users'
end if current_user.admin?
end
%>
You can also enclose one or more add_tab inside a if/unless block like this:
if @admin
add_tab do |t|
t.named 'Users'
t.links_to :controller => 'users'
end
add_tab do |t|
t.named 'Customers'
t.links_to :controller => 'customers'
end
end
It’s clear now that the show_if method wasn’t really necessary and was difficult to use because of the procs. Now you can just use your view’s params/variables/helpers as conditions.
True dynamic tabs
The facts that Tabnav is now a helper leads to another big improvement. You can now have true dynamic tabs. The default generated Tabnav now generates something like this:
render_tabnav :main,
:generate_css => true do
controller_names.each do |name|
add_tab do |t|
t.named name.camelize
t.titled "Go to #{name.camelize}"
t.links_to :controller => name
end
end
end
What does that mean? It dynamically generates a tab for every controller, just like the scaffold does with column names! Of course you’ve got to customize that, but it shows the power that this version brings to you.
You can now have a tab for every user like 37signals’s Highrise does! Just imagine something like this:
render_tabnav :contacts,
:generate_css => true do
@contacts.each do |contact|
add_tab do |t|
t.named contact.name
t.links_to :controller => 'contacts', :action => 'show', :id => contact.id
end
end
end
No more start/end
Another little improvement: Tabnav’s content are now specified as a block.
<%= start_tabnav :main %>
...
<%= end_tabnav %>
#new widget version
<% tabnav :main do %>
...
<% end %>
It’s just a lot more Rubysh now…
Hope you enjoy the improvements. If you experience issues just drop us a line.
A big thank you to Cédric Deltheil, Mark Swinson, Denis Bruléand and Glenn West for their feedback!
Socialize it:
Posted in Ruby + Rails | Tags plugin, Ruby On Rails, tabnav, widgets | 5 comments | no trackbacks
Posted by Paolo
Sat, 18 Aug 2007 23:44:00 GMT
03 sep 07 UPDATE: see a few changes we’ve made to the new version reading this post!
Hi guys, I’m working on my RailsConfEurope presentation during these days. I decided to fix a few Tabnav issues and write a few more components we often use.
I ended up writing a new Rails plugin named Widgets. It aims to offer a consistent set of rails helpers which can help you insert nice components in your rails app with no effort at all.
It already contains a more flexible version of the Tabnav and a new Navigation widget (it allows you to define those little links you usually find in the top-right corner of webapps with the same powerful highlighting engine of the Tabnav).
I’m going to add more widgets before my speech in Berlin, so don’t consider this plugin as final. I’d like you to try this out and give me feedback…
just run:
ruby script/plugin install svn://svn.seesaw.it/widgets/trunk
and then run the tabnav generator:
ruby script/generate tabnav
or the navigation one…
ruby script/generate navigation
They should give you all the info you need to get started by yourself. Please send us feedback via email at staff@seesaw.it. We’re currently experiencing issues with the comment spam system, I promise we’ll fix it asap!
Thanks for reading.
Socialize it:
Posted in Ruby + Rails | Tags plugin, Ruby On Rails, railsconfeurope, tabnav, widgets | 8 comments | no trackbacks
Posted by Paolo
Fri, 20 Apr 2007 02:35:00 GMT
Here’s the video of my speech at LRUG, and the discussion about REST.
Thanks to skillsmatter for making it available in such a short time.
Enjoy it! Suggestions and comments are always appreciated as usual…
Socialize it:
Posted in Ruby + Rails | Tags lrug, Ruby On Rails, ruby, speech, tabnav | 2 comments | no trackbacks
Posted by Paolo
Wed, 18 Apr 2007 01:07:00 GMT

Hi guys, I just got back from my one-day trip to London where I’ve just given a speech about our little Tabnav plugin.
I had a chance to talk at the April LRUG meeting and I have to admit it’s been a beautiful experience.
It seems that Ruby and Rails are getting momentum in London, the meeting was well organized and really interesting and the conference room (kindly offered by skillsmatter) was a perfect fit for the event.
We started with my talk, I had the difficult task to warm up the crowd… and you know what I mean if you’re not a native English speaker.
Luckily enough I made it pretty decently and started explaining how to use our beloved plugin, interleaving the speech with some mind-refreshing gag… (Mr.Monkey knows what I mean). At first I was kind of worried about the crowd reaction.. It’s been my first speech inEnglish and didn’t know if I could really express myself clearly. Laughs in response to my gags and questions after the speech confirmed me that everything went very well, and in the end I could gather suggestions on various ways to improve the Tabnav.
Then we continued with a discussion about REST and how it’s been implemented in Rails 1.2. I already discussed this topic with a few co-workers and we’re using it extensively here at SeeSaw so I was aware of the fact that’s a topic that would need more than and hour to be properly dissected and analyzed.
My feeling was right, the discussion generated a nice debate but was impossible to get to a conclusion. It’s really difficult to say if all this RESTful stuff in good or not. For sure the REST implementation is not giving programmers that “right feeling” we were used to have with ActiveRecords or RJS. I’d like to see that topic not only under the technical point of view but also from the “programmer happiness and sustainable productivity” perspective. Does REST make programmers happier? Is my productivity more sustainable using this approach? I think these questions will still remain unanswered for a while.
A good tradition about LRUG meetings is the “after-meeting” also known as “the pub around the corner”... You can imagine what happens when you mix passionate geeks and beer. You can find a few pictures of the event here, the slideshow and the video of the event will be online soon. I want to personally thank Murray, Jonathan, Dan, James, Eleanor, Piers, Daniel, Paul, Alec and all those guys whose name I cannot remember.
A special mention to my friend Jason Lee who let me surf his couch last night, after a 2am banana split (He is also the author of http://big.first.name, the software LRUG uses to print tag names during their meetings).
Hope to see the LRUG guys soon again…
Do you want some more links? here we are: http://www.lrug.org, LRUG Mailing list, Skillsmatter RLUG page.
Socialize it:
Posted in Ruby + Rails | Tags london, Ruby On Rails, rlug, ruby, seesaw | 4 comments | no trackbacks