Archive for the 'Ruby on Rails' Category

3 new I18n gems for your Rails 3 app

Monday, April 2nd, 2012

I’ve been busy building 3 new translation gems for a new application I’m working on. The first is i18n-country-translations. It breaks countries down into their standard country codes and adds translates for different locales. This gem can be easily incorporated into other gems or apps that need I18n country translations. Just add this to your Gemfile:

gem 'i18n-country-translations'

You can grab the source and more information here: https://github.com/onomojo/i18n-country-translations

The next gem I created uses the i18n-country-translations gem to make a country select that automatically translates the country names in the select drop down to the current locale. Just add this to your Gemfile:

gem 'i18n_country_select'

You can grab the source and find out more here: https://github.com/onomojo/i18n_country_select

Finally, no app that has any dates displayed to users can be complete without taking into account timezones. The last gem is i18n-timezones and just adds timezone translations which can easily be included into other gems or directly in your app. It also adds a convenient ActiveSupport::TimeZone override of to_s so that it takes the current locale into consideration. This makes it extremely easy to add timezone translations. Just add this to your Gemfile:

gem 'i18n-timezones'

You can grab the source and find out more here: https://github.com/onomojo/i18n-timezones

Upgrading Webster’s Classroom from Rails 2.3 to Rails 3.1

Monday, December 26th, 2011

I’ve recently tasked myself with performing a major upgrade for Webster’s Classroom. It includes both a complete site redesign and an upgrade to the latest version of Rails. We all know its a bit of a pain to upgrade from Rails 2 to Rails 3 but the longer you put off the upgrade the more painful it becomes. The app was written as a proof of concept many years ago and despite its growing user base it doesn’t have a strong enough foundation to allow it to continue expanding. We build it too quick and sloppy years ago and as a result we are testless and in need of major refactoring.

The lack of tests actually makes the upgrade easier in some respects because instead of having to hassle of fixing a bunch of sloppy tests I’m building everything from scratch both the app and the tests. The refactoring needed for this upgrade is like writing the application from scratch anyway. The existing code I’m using more or less as a rough sketch of feature requests and I have full intention of throwing all that code away if it makes sense to in this new context. Instead of trying to upgrade the inline Prototype Javascript to jQuery and UJS piece by piece, I’ve decided its best to just throw all the old Javascript out the window and start fresh. I’m committed to getting as close to 100% test coverage as possible these days so as I’m doing the upgrade and redesign I’m making damn sure there’s proper tests in place this time around.

I’ve upgraded a handful of apps to Rails 3 from Rails 2 already and in the past I’ve started off trying to upgrade within the current code base. I’ve had numerous problems with this approach and unless you’re really meticulous on every single file, you’re going to end up with a bunch of old junk files laying around and likely some missing new ones that you’ll want. This time I’ve taken a different approach and started off with a fresh default Rails 3.1 install and began moving over the legacy code file by file. I’m happy with taking this approach and instead of trying to take my Rails 2 code and upgrade it to Rails 3. Starting with a fresh Rails 3.1 code base and moving over code where it needs to be has given me the chance to move things into places they ought to be like moving code outside of the environment configs and into initializers. If I had just tried to upgrade the old code inline, I wouldn’t have spent the same amount of attention on cleaning up that mess. Once this upgrade it deployable, Webster’s Classroom is going to be in a position to really expand dramatically and rapidly.

Recent developments

Friday, December 9th, 2011

Its been quite some time since I posted anything so I feel a necessity to publish something. I’ve started many posts since the last one but I never really liked how they turned out so I discarded them. I’ve learned plenty of new things which I tried to share but never had enough time to devote towards a solid post about any of it. Considering that time seems to be always something I’m lacking, I will just post a random rant about what I’ve been up to.

I’ve started working on a Facebook app for a client called Redflag. Its an app that lets parents monitor their child’s Facebook accounts for potential bad friends. Its a fairly complex app written in both node.js and Ruby on Rails. Node.js was something I hadn’t worked with before this project but I really like it. Its a little confusing thinking in terms of asynchronous execution of code in our app because there’s a ton going on behind the scenes and its a little difficult to visualize how thing all tie back together through callbacks. The complexity of our node.js backend makes it extremely difficult to test well but we do have a small but growing suite of jasmine tests for it.

I’ve been continuing to work with San Francisco Soup Company on their kitchen management application and have hired another developer, Jose, to help out. He’s an excellent developer and writes some really solid code. We’ve migrated the site to Rackspace’s cloud and I’m really liking Rackspace over Amazon’s EC2 these days. Their web interface is much more usable and I can actually get ahold of someone when there’s a support issue. The server seems to run much faster than similar EC2 instances and is cheaper so I’m pretty happy with Rackspace so far. We’ve been working on refactoring the code and building a more comprehensive test suite which is coming along extremely well.

More interestingly, I’ve started partnering with different businesses to expand my development and design resources. These additional resources will allow me to expand Onomojo in new directions and produce better results than I could just by myself. My focus is on partnering with companies that have solid development processes or are at least adaptable enough to conform to my own methodologies and best practices. By partnering primarily with businesses instead of individual talent, I will be able to provide more consistant results for my clients without the HR headache that comes with managing a team of remote developers. I have an agressive growth goal for 2012 and with the partnerships I’ve been building I believe Onomojo is in a good position to realize those goals.

I’ve also made great progress towards getting back into graduate school. I am working on a plan that will let me continue being CEO of Onomojo but allow me the flexibility to get my PhD. I’ve retaken the GRE and got a great score. I’m still working on narrowing down my school choices and drafting my applications. I don’t expect to apply anytime soon though. I need to have my ducks in a row before I jump back into academia. I won’t be applying until next year at the soonest with a starting date of 1.5 to 2 years from now.

Earthpay – Online Payment processing startup committed to donating collected fees to charity

Monday, March 7th, 2011

This site has been a long time coming. I pretty much finished the prototype about 6 months ago and the CEO is working on funding to take it to the next level. Its a great way to easily contribute to charities around the world without costing you a single penny. It basically takes profits collected per transaction and donates them to your selected charities or causes. By integrating into your existing shopping cart, you can help save the world without costing you a single penny more than what you’d already be paying in processing fees. Take a look at the site and if you’d like more information please don’t hesitate to contact me or one of the other contacts on the site.

Rails 3 UJS custom ajax response observers

Wednesday, December 29th, 2010

I used to brush aside UJS as unnecessary and a source of maintenance problems because of the disconnect from the HTML. Developers coming in with no knowledge of the JS will have problems debugging complex JS that’s been unobtrusively created. I do still feel this is true but the benefit of UJS is in the simplicity and elegance of your JS code in the end. It might add a layer of complexity because of the detatchment from the HTML but the JS you end up developing will be cleaner, more concise, and ultimately much much more readable than embedding it into the HTML.

With that rant out of the way, lets examine a problem with the Prototype library. When doing Ajax calls we can set onSuccess, onFailure, and a few other observers. With UJS your Ajax observers might look something like this:


$('something').observe('ajax:success', function(right_evt) {
alert('success');
});
$('something').observe('ajax:failure', function(right_evt) {
alert('success');
});

But what if our Rails app makes smart use of HTTP status codes and returns status codes like 401 or 302? With Prototype, we can specify on401 or on302 but the catch all observers like onFailure won’t get called in those cases. For instance, just adding the following to the above code:


$('something').observe('ajax:on401', function(right_evt) {
alert('unauthenticated');
});

Has 2 problems. First, it might seem like this is possible with Prototype the Rails UJS, rails.js, doesn’t actually recognize any specific status code observers. So that on401 will just get ignored. There are 2 solutions to this. The first is to hack up rails.js and add a listener for on401 or whatever other status codes you want to specify. The problem is Prototype will ignore the other catch all observers like onFailure if you start specifying specific status codes. A better solution is to just catch the specific status codes that you want custom inside of the catch all onFailure observers. You can do that like this:


$('something').observe('ajax:failure', function(evt) {
if(evt.memo.status == 401) {
alert('unauthenticated');
return false;
};
alert('Failure');
});

Now this will handle 401s differently than other failures but still catch all the other failures without having to specify all of the status codes individually.

My Goodbye Letter to Engine Yard

Sunday, August 29th, 2010

That response sounds familiar but really isn’t that comforting or
realistic. You’re suggesting its rare to have instances disappear yet
it happened twice in less than 6 months. You’re suggesting I upgrade
to a more expensive server which may or may not solve the problem. You
don’t know what causes the instances to disappear but for some reason
a more expensive instance solves the problem. I’m not sure how those
dots are connected but it certainly doesn’t add up to me. You want me
to spend more money per month without any real justification,
quantitative data, or explanations.

To summarize:

* You don’t know why instances disappear.
* Its rare for instances to disappear.
* The solution is to spend more money per month.

From my perspective, you still haven’t resolved the underlying
problem. You suggest maybe hardware but hardware crashes twice in 6
months? That doesn’t sound realistic. The issue that concerns me is
that you haven’t addressed the underlying problem of why the instances
disappeared. You’ve only offered a solution that has no connection to
the actual problem: Get a better server. Of course, I could just go
straight to Amazon and bypass EY altogether if I were to get another
server.

Without really understanding why the instances are disappearing it
points to a bigger problem at EY than just this issue. It points to a
lack of concern for your clients, a lack of understanding of your own
technology, and a fundamental problem in how you attempt to solve the
problems presented to you. Its a ‘shuffle it under the rug’ approach
to solving your problems which doesn’t bode well with me. Your
solution is, simply put, one without any technical merit. Anyone in
sales could have given me the same answer. If I were to refer clients
to EY and they had other problems that you couldn’t figure out would
your solution be that they need to upgrade their servers?

I appreciate your delayed investigation into the issue but I
definitely won’t be moving more of my clients to EY anytime soon. I’m
actually glad you reached out to me recently because its given me the
motivation to review what I still see as this outstanding issue. As
such, I’ve decided to cancel my EY account.

> From: EY
>
> X and Y asked me to follow up with you on this.
> First of all, if someone on my team dropped the ball on getting back to you,
> we apologize. I will follow up with the engineers on my end.
> Re. instances disappearing – I found and reviewed the ticket for when this
> happened. It appears that it was no longer possible to SSH into your
> instance and when our engineer tried to terminate it, it was stuck in a
> shutting down state. I can’t answer why this happened in this particular
> case, but out of the several thousand AWS instances we manage on the EY
> AppCloud, occasionally we have observed that an instance can disappear or
> become unresponsive. This can happen, for example, if the physical server
> it is mounted on fails. The few times we have seen it, it has been for
> Small instances, which is what you had. Our default AWS instance size for
> EY AppCloud is now Medium, which we to date have not observed issues with.
> Hope that answers your question – and feel free to contact me for any
> additional questions about this.

Faster AJAX integration tests using Rails, Cucumber, Capybara, and envjs

Sunday, July 4th, 2010

I admit it. I put off using Cucumber for a few years while it became more stable. I took a look at it a few years ago and found it didn’t really work properly according to the then current documentation. So, I shelved it until recently. There seems a renewed interest in BDD and Cucumber recently so I decided to take another look. Needless to say, I was impressed and since I was starting a new project I figured it was the perfect time to start using Cucumber.

An hour or so into it, I still didn’t have my first test passing but I felt I was making progress so I persisted. Here is my first test:

Feature: Registration
  In order to use the site
  As a visitor
  I want to be able to create a new account

  @javascript
  Scenario: Signup Form
    Given there are no users with the email address test@domain.com
    And I am on the home page
    When I follow "Signup"
    Then I should see "First Name"

I added the ‘@javascript’ part later. I couldn’t figure out why my AJAX query wasn’t running in Cucumber. It was working fine in my browser so I asked my buddy Corey. He pointed me to Capybara.

Basically, there are 2 distinct pieces to the testing process in Cucumber.

1. the step definitions
2. the browser simulator

For the step definitions part, its not really about you write the steps but more about how those steps are translated back to the DOM and how they interface with the browser simulator. Cucumber uses Webrat as the default for step definitions and I may have just followed the introduction docs to end up with that as the default. The problem with Webrat is that it doesn’t support interfacing with the DOM very well and so it doesn’t allow access to page elements that have been updated by Javascript. That means any AJAX testing is completely out the window. Unfortunately, most websites today pretty much require Javascript so Webrat is pretty much useless to me.

Capybara is a Webrat alternative that does support Javascript. Capybara allows you to access DOM elements that have been updated by Javascript. Now here’s where things get tricky. Both Webrat and Capybara integrate with Selenium, a browser simulator. The same simulator is being run in both cases but only Capybara has the ability to access the updated DOM elements.

This allowed me to finish up my first test. I didn’t notice it at first but after I added a second test I realized how slowly these tests were running. I didn’t get any quantitative performance data to share but just sitting there waiting for 2 simple tests to run I was pretty amazed that anyone would even be testing like this. There would be no way this could scale to a suite of tests for an entire site. The time wasted in just waiting for all the tests to run would prevent any actual development to occur.

So I started looking for alternatives. The culprit in the slowdown here was Selenium. I came across a blog post by RubyFlare the was talking about a much faster alternative to Selenium called envjs. You need to install the capybara-envjs gem, the envjs gem, and reconfigure some things in Cucumber but its fairly simple.

Now that I had my brand new envjs browser simulator working with Cucumber and Capybara, I tried to run my first 2 tests on it. FAILED

What I discovered in the envjs output was:

ERROR: [Sun Jul 04 2010 17:44:10 GMT-0600 (CST)] {ENVJS} response XML does not apear to be well formed xml Line: 1377
ERROR: [Sun Jul 04 2010 17:44:10 GMT-0600 (CST)] {ENVJS} ReferenceError: $domparser is not defined

I started digging through the envjs code and basically discovered it doesn’t really support AJAX calls returning HTML. Just XML. Well back to the drawing board. It doesn’t seem like it would be incredibly difficult to add support for that to envjs and the rest of the chain of libraries up to Cucumber. When? I’m not going to start responding to all my AJAX queries with well formed XML instead of simple HTML anytime soon. It would just add an unnecessary layer of complexity to the application and I would have to do it just to use the envjs browser. No thanks. Back to the drawing board.

Since I did get my first AJAX tests to pass using Selenium, I reverted back to using that so I can at least progress for now, albeit slowly. I know a lot of people are frustrated by the slowness of Selenium so I doubt it’ll be too long before envjs supports HTML to be returned from AJAX calls.

EngineYard default configurations strike again

Friday, April 30th, 2010

It was just brought to my attention that my company’s homepage was pointing to the wrong app. This has happened a few times since I’ve switched to EngineYard. They don’t include www aliases in the default nginx configuration so you have to add them with a keep.domain.conf file. I recently migrated my company site to a new instance and left the default configuration. When I checked to make sure the app was setup on the new instance properly, http://onomojo.com , it worked fine and I didn’t think anything of it. Of course, I didn’t test http://www.onomojo.com which ended up pointing to an app that is still in the early phases of development since I didn’t customize the nginx config. What a huge goof. No wonder I’m getting mixed responses from potential new work lately. If you’re reading this and you saw the awkward half broken site with the test video please revisit the site again.

Engine Yard problems

Saturday, March 6th, 2010

Here I am with yet another proprietary EngineYard problem again. I simply clone an existing application that’s up and running fine and the new cloned environment doesn’t boot. Its giving me this error instead:

/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/rails/gem_dependency.rb:224:in `specification’: undefined method `version_requirements=’ for # (NoMethodError)

Here’s a recent thread about the same issue:

http://rubyforge.org/tracker/index.php?func=detail&aid=27868&group_id=126&atid=575

Looking into the differences in my ‘cloned’ environment and my actual environment and I see the gems list isn’t even the same. So basically Engine Yard’s clone command doesn’t actually clone the environment at all. It just took my old apps and tried to copy and paste everything onto a newly configured environment which obviously doesn’t work.

My original environment gems:

abstract (1.0.0)
actionmailer (2.3.5, 2.3.3, 2.2.2, 1.3.6)
actionpack (2.3.5, 2.3.3, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.5, 2.3.3, 2.2.2, 1.15.6)
activeresource (2.3.5, 2.3.3, 2.2.2)
activesupport (2.3.5, 2.3.3, 2.2.2, 1.4.4)
addressable (2.1.1)
aws-s3 (0.6.2)
builder (2.1.2)
daemons (1.0.10)
erubis (2.6.2)
eventmachine (0.12.6)
extlib (0.9.9)
eyrubygems (0.0.2)
facter (1.5.2)
fastercsv (1.5.1)
fastthread (1.0.7)
ferret (0.11.6)
gem_plugin (0.2.3)
hpricot (0.8.2, 0.8.1)
igrigorik-em-http-request (0.1.5)
json (1.1.3)
mime-types (1.16)
mixlib-cli (1.0.4)
mixlib-config (1.0.12)
mixlib-log (1.0.3)
mongrel (1.1.5.1)
mongrel_cluster (1.0.5)
ohai (0.2.0)
open4 (0.9.6)
rack (1.0.1, 0.4.0)
rails (2.3.5, 2.3.3, 2.2.2, 1.2.6)
rake (0.8.3)
rest-client (1.3.1, 0.9.2)
right_aws (1.10.0)
right_http_connection (1.2.4)
rmagick (2.8.0)
ruby-openid (2.1.2)
rubyforge (1.0.3)
sparklines (0.5.2)
stomp (1.0.6)
systemu (1.2.0)
will_paginate (2.3.12)
xml-simple (1.0.12)

And the ‘Cloned’ environment gems:

abstract (1.0.0)
actionmailer (2.3.3, 2.2.2, 1.3.6)
actionpack (2.3.3, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.3, 2.2.2, 1.15.6)
activeresource (2.3.3, 2.2.2)
activesupport (2.3.3, 2.2.2, 1.4.4)
addressable (2.1.1)
aws-s3 (0.6.2)
builder (2.1.2)
daemons (1.0.10)
erubis (2.6.2)
eventmachine (0.12.6)
extlib (0.9.9)
eyrubygems (0.0.2)
facter (1.5.2)
fastercsv (1.5.1)
fastthread (1.0.7)
ferret (0.11.6)
gem_plugin (0.2.3)
hpricot (0.8.2, 0.8.1)
igrigorik-em-http-request (0.1.5)
json (1.1.3)
mime-types (1.16)
mongrel (1.1.5.1)
mongrel_cluster (1.0.5)
ohai (0.2.0)
open4 (0.9.6)
rack (1.0.1, 0.4.0)
rails (2.3.3, 2.2.2, 1.2.6)
rake (0.8.3)
rest-client (0.9.2)
right_aws (1.10.0)
right_http_connection (1.2.4)
rmagick (2.8.0)
ruby-openid (2.1.2)
rubyforge (1.0.3)
sparklines (0.5.2)
stomp (1.0.6)
will_paginate (2.3.12)
xml-simple (1.0.12)

Clearly this new environment isn’t a ‘clone’ at all. You can’t just put feathers in your butt and call yourself a chicken. Don’t call this feature ‘clone’ when its really just recreating everything on a completely different setup if they’ve changed their default environment configuration. It should be called the ‘waste another 4 hours fixing our broken Engine Yard system’ feature. I think its a little more accurate.

Rojo: An easy to install Ruby on Rails CMS

Monday, February 8th, 2010

Rojo is Onomojo’s Ruby on Rails Content Management System. You can find it here:

http://github.com/onomojo/rojo

Today we made some changes to make the initial setup of a Rojo instance easier. The steps to setup a new Rojo instance are clearly detailed in the README. This should help eliminate most of the problems new users were encountering when trying to setup Rojo from scratch. Please let us know if you have any comments or suggestions for improving the setup process.

In the next few weeks, we will be releasing the plugins that we’ve developed for Rojo including a blog, video gallery, photo gallery, and more so stay tuned.