Archive for the ‘Ruby’ Category

Keeping track of Rails Boot progress

March 2, 2010

Ruby on Rails can take a long time to boot so in some cases it can be useful to provide some kind of progress reporting (f.x. if you are considering using Rails in an embedded application).

RoR does not provide any direct means to monitoring the progress of the boot-process, so a little work is necessary. Changing the ruby source files involved in the boot process is not ideal from a maintenance standpoint and even if one does so, the progress report turns out to be very uneven. The best approach I could come up with was a Kernel hook which keeps track of when files are require‘d. Both easy to do and works surprisingly well for fine-tuned progress reporting.

# Hook into require so that we can track startup progress
module Kernel
 alias org_untracked_require_myAppName require
 def require(file, *extras)
 v=org_untracked_require_myAppName(file, *extras)
 $progress_coordinator.addWorkDelta(1,'Rails required '+file.to_s)
  unless progress_coordinator.nil? || file.nil? || !v
 v
 end
end

Translating models in ruby on rails

July 3, 2009

I had to translate a model in Ruby on Rails. The example (in: http://guides.rails.info/i18n.html#translations-for-active-record-models) was:

activerecord:
  models:
    user: Dude
  attributes:
    user:
      login: "Handle"

Well that didn’t work for me. My only difference was that my model name consisted of two words, so in my yml file I had the following:

activerecord:
  models:
    email_template: Email template
  attributes:
    email_template:
      name: Name
      subject: Subject
      body: Body

But this did not work in the “error_messages_for”. Well the attribute names did, but not the model name that remained untranslated.

After an hour of Googling I didn’t have any answers until I tried to ad a space instead of the “underscore” in the model name. THAT WORKED. So now my yaml file looks like the following. Notice the inconsistencies in the model name!

activerecord:
  models:
    email template: Email template
  attributes:
    email_template:
    name: Name
    subject: Subject
    body: Body

Looking at the source code for the helper method “error_messages_for” we find the thing bothering me here:

          I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
            header_message = if options.include?(:header_message)
              options[:header_message]
            else
              object_name = options[:object_name].to_s.gsub('_', ' ')
              object_name = I18n.t(options[:object_name].to_s, :default => object_name, :scope => [:activerecord, :models], :count => 1)
              locale.t :header, :count => count, :model => object_name
            end

(line 199 of active_record_helper.rb)

object_name = options[:object_name].to_s.gsub('_', ' ')

?!?! They are replacing the underscores instead of doing something like:

eval(resource_name.classify).human_name

Has someone else noticed this?! And what are your fixes?
Currently I have to write the model name twice in my yaml files. This is not DRY. My file ended up looking like:

  activerecord:
    models:
      # used by EmailTemplate.human_name
      email_template: "Email template"
      # used by error_messages_for (go figure?!?)
      email template: "Email template"
    attributes:
      email_template:
        name: Name
        subject: Subject
        body: Body

Installing gems on passenger rails site

February 4, 2009

I just created a new server running a rails app using the wonderful Passenger plugin

After I’d install all the gems needed, I still got errors like :
“no such file to load — RMagick (MissingSourceFile)”

I tried doing a:
require ‘RMagick’

in both IRB and the console and here it loaded fine. Then I suddenly remembered the last time I installed a server (I quickly forget silly errors since it’s so easy to just google them), so I thought I’d post the solution here if I ever forgot it again.

The problem is that I’m also using the Ruby Enterprise Edition and all the gems had been installed to the “old” Ruby version. Not the Enterprise Edition. So installing the gems using the gem binary that lies under the Ruby Enterprise Edition fixed the error:

sudo /opt/ruby-enterprise-1.8.6-20090113/bin/gem install rmagick

(remember to update to the correct version)
Won’t forget that again.

C# 4.0 specs mentions Ruby in line 4

November 6, 2008

We are currently seeing Microsoft being more and more aware of Ruby and Ruby on Rails world. They’ve launched there own MVC framework and they are working on IronRuby.

Now a whitepaper on the future specs of C# has seen the light of day and I’m proud to say that Ruby is mentioned in line 4 (if you don’t count the introduction). They are implementing more dynamic structures in the language.

The following is a quote from the whitepaper. They are talking about “dynamic” objects:

“Some examples include
a) objects from dynamic programming languages, such as Python and Ruby”

I think this is a funny development of a compiled language. Ruby has excisted for 15 years but suddenly Microsoft finds it the source of great inspiration?!

When that is said the feature I’m currently looking most forward to in C# 4.0 is co- and contravariance. Now we can use generics for more than simple collections 🙂

If you want to read the Microsoft whitepaper, you can get it here

Building a Ruby on Rails application in a week

June 9, 2008

This post will (and perhaps some followups) describe how I did a web application in a week using the web framework Ruby on Rails

My company 41concepts is currently developing a rather large application using Ruby on Rails and during this I had a week where I had to wait for something being delivered by another developer. The application wasn’t really supposed to grow this large, but the feature list just seemed to grow the more we developed.

Because I had this week, I wanted to try to make a small Ruby on Rails application implementing an idea I’ve had for some time now.

I really like my Playstation 3 and if you ask my girlfriend this is also a bit too much sometimes. I generally like adventure and roleplaying games such as the Final Fantasy series and because I hate wasting my time, I usually follow a game walkthrough.

With this application I really wanted to give something back to the game walkthrough community, so I decided to create the site/application “My Walkthroughs“. The main idea is to let users of the site create their game walkthroughs and then share them in different format such as pdf, plain text and html. The idea comes from the fact that almost all walkthroughs are being done in plain ASCII text.

So the application should enable users to build a walkthrough through a web interface and publish it in various formats.

Ok… I had a week for this, so what did I do?

Planning

Well.. I must admit I went straight to implementation. This was a small site and really just a pet project.

Implementation

Design

I’m not really a Photoshop shark (I don’t even have Photoshop), so I found a free template:
http://www.opensourcetemplates.org/templates/preview/1303028527/

For icons I used the FamFamFam silk icon set that is simply astonishing:
http://www.famfamfam.com/

Finally I decided that I wanted some kind of Logo, so I invested in one from Logo Samurai:
http://www.logosamurai.com/

Cool css scalable buttons:
http://monc.se/kitchen/stew/buttons/btn.html

Took the confirmation boxes from:
http://www.templamatic.com/blog.asp?BlogID=18

Got the cool form hints from here
http://www.askthecssguy.com/2007/03/form_field_hints_with_css_and.html

Code

I used a lot of different plug-ins and gems. The fastest code to implement, is code you don’t write :). They are listed below:

Plug-ins

acts_as_bookmarkable
Suddenly I thought it would be a great idea if the user could bookmark other user’s walkthroughs

acts_as_commentable
Of course users should be able to comment each others walkthroughs, it wouldn’t quite get the web 2.0 feel without 🙂

acts_as_list (tutorial)
This is used for lists that should be sortable

acts_as_rated
This was added late, it turned out really nice with this tutorial, adding nice css/ajax support. Also note that all the averages are calculated when the entity is saved and not at render time

attachment_fu (tutorial)
This is used for all uploads and thumbnail generation. It works perfectly together with Amazon S3

auto_complete
Used for completing the game’s name when creating a new walkthrough

restful_authentication (basic use, great screencast)
The standard on authentication

simple_captcha
Used for registrations and other places where spam bots like to date

synch_s3_asset_host
This was used in the Capistrano recipe for synchronizing the public folder with the Amazon S3 asset hosts

Gems

htmldoc (tutorial)
I had to modify it though, when certain PDFs was generated, the gem reported false as return value but left nothing in the error collection. After some investigation I found out there was a bug in how the result from the command-line was parsed and changed line 186 from “case line” to “case line.strip” since there was some \r characters that the code didn’t quite expect. I’m not sure if this is only applicable to me, but now it works

mini_magick
Image manipulation for use with the attachment_fu plugin. This is not as heavy as the full rmagick since it uses the commandline. I wanted to use this with the simple_captcha plugin as well, but it can only modify existing images, not create new ones.

rmagick
Image manipulation for use with the simple_captcha plugin

aws-s3
Provides a nice api for Amazon S3

uuidtools
Used for generating GUIDs. Notice that it couldn’t find the MAC address on my virtual server at Slicehost, so I had to manually give it a MAC address

will_paginate
Great for pagination. If you want to use ajax paging – something we use in another application – this works perfectly

In total the model folder in the application consists of 11 models, 3 observers and 2 mailers. A small and simple app 🙂

The application use REST and search engine optimized urls everywhere which was something I really wanted from the start. Take e.g. the url for a walkthrough:

/walkthroughs/123456-resident_evil_5_ps3/summary

On this page the user gets a summary of the walkthrough and can download the walkthrough in three different formats using the URLs:

/walkthroughs/123456-resident_evil_5_ps3 (no extension => html)
/walkthroughs/123456-resident_evil_5_ps3.txt (plain text – ascii)
/walkthroughs/123456-resident_evil_5_ps3.pdf (well… pdf :))

The code looks at the last edited time of the walkthrough and generates a new one if needed. The output is placed on Amazon S3 and the user is redirected to this. It would be a bit nicer if the application streamed the file from S3 and then directly on to the user, but in order to save server resources, the user is redirected directly to the S3 url (which is currently not that nice).

Deployment

We are currently trying to do deployment on Amazon EC2 for our larger application, but with this one I really wanted to try out the new Passenger (mod_rails) Apache plugin, since it promised real easy rails deployment.

So I bought a slice over at Slicehost and pretty much followed this tutorial to the letter. It worked like a charm.

After this I set up Capistrano according to Slicehost’s own Capistrano documentation.

I also signed up for the Amazon S3 storage service for the walkthrough pdf and txt files, as well as the user avatars (from attachment_fu plugin). Furthermore I used the synch_s3_asset_host to synch the public folders with asset hosts in Amazon S3 during the capistrano deployment.

For tracking I added the site to our Google Analytics account.

Aftermatch

Well, I must admit that not everything was actually done in this one week. The following was added after week one:

  • The domain name (I forgot to start this process in the beginning of the week, and it takes a couple of days to process)
  • The logo from LogoSamurai took some days with revisions and all
  • The bookmarking functionality
  • The asset hosts in the capistrano recipe
  • Forgot password functionality (don’t know why this slipped in the first place)
  • Some minor bugfixes to the pdf and plain text rendering (I will probably keep improving this)
  • Caching (this is something I will need to monitor as the site gets traffic)
  • I normally write alot of tests, I must admit that I could do better with this app 🙂
  • I’ll probably optimize some database indexes along the way
  • A nicer system admin area using Active Scaffold or similiar

Price

So what did all this cost me!?

  • Hosting @ Slicehost $20 a month first bill was $60
  • Logo (LogoSamurai 3 designs unlimited revisions) $147
  • A bunch of great rails plugins – FREE (thanks…)

$207 and one week of work (plus some follow up described above).

Now I only need to get some users on the site (the hard part). I might write a bit about the bootstrapping process later.

So go to http://www.mywalkthroughs.com and give it a spin.

Man I love rails…

Introducing Railscheck – A Q/A verification tool for Ruby On Rails

April 15, 2008

The problem

It is a law of software development that the earlier one identifies a problem the cheaper (and less embarrassing) it is to fix. Hence, we developers strive to identify errors early on our own development machines aided by sound development methodologies, tools, and tests (at best automated).

A particular quality assurance challenge with dynamic technologies like Ruby/Rails, Javascript or view templates like ERB is that you have no compiler that checks for syntax errors in advance. So you have to have 100% test coverage (which is often too expensive to be realistic) in order to be assured that even simple syntax errors does not occur.

Furthermore in the case of Ruby On Rails, which is of heavily opinionated nature, there are many conventions that can be broken by mistakes, which may introduce errors that do no show up initially. This introduces additional testing/debugging work for the programmers (in particular for newcomers to RoR).

Happily Ruby On Rails is so productive a web technology that it more then compensates for these difficulties but even though would it not be nice with some automated tool to help out a bit?

Unfortunately, your typical Ruby development tools does not provide any kind of static verification checks since the problem appears to be just about unsolvable in the general case (because of the dynamic nature of Ruby).

But what about the smaller problem of static verification checks for Ruby On Rails projects only? The RoR domain is much smaller and highly standardized so what is impossible for the entire domain of Ruby programs should be partly possible in Ruby On Rails!

Railcheck

So inspired by LINT , the power of Ruby meta-programming and various RoR testing snippets on the web, I wrote this initial beta release of Railcheck which is a semi-static verifier for your Ruby on Rails projects.

Delivered as a free Ruby gem the Railscheck project provides a shell command task “railscheck” that you can run against your Rails projects to test for a number of typical bugs and inconsistencies. See the project site and linked readme file at railscheck.rubyforge.org for details about how to install and run the tool and much more.

Railscheck is a working beta. The gem works and is useful but has a limited feature set. Much more to come. Use the rubyforge tracker function on the Railscheck website to suggest features that you would like to be added (or bugs to fix). You are also very welcome to add code/tests to the project, which is open source. Indeed the project is now open to new contributing members.

Update:

Updated the gem with improved contributor documentation, an explicit gem dependency and a fix for a typing error in railscheck.rb file. Try again if you had problems.

Rails and file uploads

April 10, 2008

While there certainly are several good file upload plugins for Ruby on Rails. It is actually somewhat hard to find any good tutorials on how to do it yourself. I just wanted to post to great links that helped me figure the whole thing out 🙂

http://blog.vixiom.com/2006/07/26/rails-stringio-file-upload/

http://manuals.rubyonrails.com/read/chapter/78

(Now I also know where to look when I have to work with file uploads in the future)

Ruby Fools conference in Copenhagen

April 3, 2008

So we went to the Ruby Fools conference here in Copenhagen and this post kind of sums up my experience of the different speakers. The conference went over two days and had three tracks. I primarily focused on the Advanced Rails stuff. I’ll probably update this post once you can download the presentation slides (and video).

Tuesday:

Dave Thomas explained in his opening keynote why he is a Ruby Fool :). He’s gave a great performance about his passion for Ruby and went on to compare the sudden rush of developers coming to the platform with Rails to “golddiggers and prostitutes” (before Ruby was this nice little settlement, and suddenly everyone wanted to join). That of course hit pretty much 80% of the people sitting in on the keynote. Great stuff :D.

REST: A pragmatic introduction to the Web’s architecture by Stefan Tilkov. While REST is not that new to us, and the speak therefore didn’t provide much to us, Stefan was great to talk with and I threw a few ideas on him about some of our challenges with REST (like, what do you do when you’re implementing a dashboard with some functionality also found in other places of the system – expect a new blog post on this).

Tuning the Rails stack by James Cox. Since tuning involves turning alot of knobs al over, this is not an exact science, but he did give som nice pointers on e.g. MySQL tuning and also told a few scary stories on applications that didn’t scale. On a side note, he had the coolest presentation slides (well, not really slides, I think he said it was a flash movie :))

Advanced Ruby on Rails security by Heiko Webers. I’m having my doubts on what to write here. Let me first say that the content of the presentation was great and very “german”. It was “Do this/Don’t do this”. Great stuff. No room for interpretation and once the slides are downloadable I’ll probably run through every slide while looking on our own TBA application :). Unfortunately Heiko wasn’t the great presenter and while the slides was clear, he pretty much just read them out loud, but was in trouble whenever he had to explain something that wasn’t on them.

Meta-meta programming by Nic Williams – man I love this guy.

1) The presentation was close to useless when looking at the use cases where this can be applied.
2) The subject was VERY technical

And yet… This was perhaps one of the greatest presentations due to the amount of humor and general relaxed style of Nic. Basically everyone knows meta programming, so meta-meta programming was the meta programming of meta programming. The easiest explained example in rails terms is a generator that generates generators (as I said… Not the most common use case :))

Party keynote by Evan Phoenix. A couple of sponsors had provided food and beverages so of course there was a “party keynote” (Dave Thomas wanted to swap keynote with Evan :)). While I have a great respect for Evan, the keynote was close to “not relevant at all” to me. He basically explained how he does project management on the open source project Rubinius. I’ve haven’t been in a open source project before, but what he presented was pretty much “be nice and positive to people”. There was some debate about the policy on the project that once you committed your first patch you get full commit access to the project. While it certainly works for him, I’m still having my doubts.

Wednesday

Keynote: Ruby: Past, Present and Future by Yukihiro “Matz” Matsumoto. This whas great to here it from the creator himself. There weren’t many surprises about Ruby it self, but it was great to hear about what his thoughts was on other languages and why he then went on to create Ruby. The future part was a bit cutted off because he spent a little too long bitching about character encoding (UTF-8,16,32). It was clearly something that he’s spent a lot of time on with the 1.9 release.

Versioning your data model by Ole Friis Østergaard. The presentation explained 4-5 different plugins that had something to do with versioning (also an undo redo plugin that actually looks rather nice) – including his own new plugin Subversive. I kind of noted that this was actually one of the first presentations that actually showed “real live code”.

Adding full text search to your Rails application by Jørgen Erichsen. We’re currently also implementing the search enginge Solr into our own application so this presentation was a must see. While Jørgen went through the basics of the search engine and the acts_as_solr plugin (as well as Ferret and also briefly mentioned a couple of other solutions), he didn’t seem that knowledgeable about the subject when getting to stuff outside the basic behavior of the product.

The dark art of developing plugins by James Adam. This was great. James presentation was very pedagogical buildig a plugin step by step explaining every bit of the way. While plugin development aren’t that difficult to grap, it certainly put one or two thing in place for me. I’m looking forwards to his slides so I can wrap our authorization for our application up in a plugin.

After this I unfortunely had to leave because of another engangement. So I missed one speaker as well as the ending panel discussion.

Overall the conference was well planned and executed. You could perhaps argue that it is limited what you will learn in one hour presentations, but I could just have attended the workshops leading up to the conference. As a last note… The track introductions seemed a bit off = 30 minutes break – 15 minutes track introduction – 15 minutes break (though Glenn Vanderburg was quite good.

Using ExtJS tree on rails with the prototype adapter

February 22, 2008

UPDATE: Notice that the new version of ExtJS is now on Prototype 1.6, yeah…

So I’m doing a folder tree using the ExtJS javascript with the prototype library adapter. I’m pretty much doing this.

This was somewhat working. Every third or forth browser refresh the tree didn’t load any nodes. Very odd indeed. I debugged my way through using Firebug, but when this was on, it worked everytime. Since Firebug slows down the entire javascript environment I concluded that the error occured when something (or loading) happened too fast.

In the ext-all-debug.js (version 2.0.1) I tracked the error down to line 23754. The json result being evaluated is undefined (notice that this only happens some of the time). So… after hours of head scratching and putting alerts in both the prototype library and ExtJS library to see what was wrong, it hit me…

The version ExtJS I have, come bundled with prototype 1.5.0 while rails 2.0.2 comes with prototype 1.6.0.1. In my layout I simply included <%= javascript_include_tag :defaults %>, but after I changed this to:

<%= javascript_include_tag “application.js” %>
<%= javascript_include_tag “ext/adapter/prototype/prototype.js” %>
<%= javascript_include_tag “ext/adapter/prototype/effects.js” %>

Everything seemed to work. Now my only problem is that I now use Rails 2.0.2 with an old version of prototype. So can I use all the rails helpers?!?

Installing Mephisto (multi site) on DreamHost

January 7, 2008

The title might be a bit off, since I’m not providing you with a step by step guide. This post do however sum up my notes during the install.

I started by following excellent guide:
Mephisto and Dreamhost

After looking at several guides, this one was really the easiest to follow.

Now Mephisto was installed and I wanted to add multiple sites support and I followed the guide:
Setting up Mephisto for multiple sites

I did everything but couldn’t figure out why the site still used the site root (/public) as cache location. Well silly me. As it turned out I had to restart the fcgi process (something that I really thought I’ve already done). So… Remember to do a:

touch public/dispatch*

from the application root folder.


Design a site like this with WordPress.com
Get started