Entries Tagged 'code improvements' ↓

Script.aculo.us Patch – InPlaceEditor Incorrectly Determining Row Count

Recently in a project that my brother and I were working on we ran across a problem with the Ajax.InPlaceEditor resulting in the number of rows for the editor being incorrectly determined. Here you can see the area before it’s used:
Ajax.InPlaceEditor before clicking
When I clicked on the edit icon I got:
Ajax.InPlaceEditor bugged
Clearly not what I wanted. After fiddling with all the possible pieces and configurable parameters I finally traced it down to the source and realized there was a bug in the original code:

createEditField: function() {
var text = (this.options.loadTextURL ? this.options.loadingText : this.getText());
if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) {

Looking in the 2nd check of the if statement you see it’s checking for line breaks in “this.getText()”. It should have been checking the “text” variable it had just created above it. Changing that code to:

createEditField: function() {
var text = (this.options.loadTextURL ? this.options.loadingText : this.getText());
if (1 >= this.options.rows && !/\r|\n/.test(text)) {

…fixed the problem! Now when I click my InPlaceEditor I get:
Ajax.InPlaceEditor fixed

To share my fix I went to the Script.aculo.us page on Submitting Patches but unfortunately their instructions didn’t work right away for me.

Luckily Ryan Bates posted a Railscast called Contributing to Rails that had the right information I needed. Once I had successfully produced my diff file I submitted the patch to the Rails Trac.

Hooray! My first ever patch!

Upgrading Restful Routes for Ruby on Rails 2.0

Yesterday I upgraded my project from 1.2.5 to RC1 of Ruby on Rails 2.0 and had some problems with my routes. During my upgrade process I found a really great post over at assert_buggy called How to upgrade Ruby on Rails Restful routes to 2.0.

Here you’ll find a really handy tool for checking everywhere in your code that you use any paths and allows you to fairly quickly create a hash linking from your old routes to the new correct routes, then run a script to update all of your code with the changes.

Unfortunately for me I had a LOT of custom routes, but I knew a lot of them still worked so I didn’t want to manually try and compare them with the “rake routes” output to see what was out of date.

At the time of this posting the code doesn’t have a way to show me only what’s messed up, and while I know the developer could add some functionality to check which of my routes was not found in a proper way, in the meantime I’ve come up with a cheap hack.

Just change line 22 from:

"#{m}" => ""

to

"#{m}" => "#{m}"

EDIT: The original author implemented my change and now requires no editing in order to use in this manner. Enjoy!

and do steps 3 and 6 which would be:

require 'route_migrator'
RouteMigrator.dump_current_route_methods
RouteMigrator.check_new_routes

and voila you’ll get some helpful output looking something like mine:

"The following new named routes you've specified seem to be not existent"
"home_room_path"
"partitioned_path"
"test_room_report_path"

Now instead of checking all 50+ of my crazy custom routes to figure out which ones don’t go anywhere, I’m given only the 3 from my code that don’t have a proper map.

My sincere thanks go out to assert_buggy!

Autotest crashes using RedGreen

At the time of this posting, the latest gem release of RedGreen has a bug. For those of you who don’t know, RedGreen is a handy tool to make specs/unit tests color according to the result of the test to make your testing output much easier to read. Unfortunately, at the time of this posting, the latest gems (Autotest, RedGreen, Ruby on Rails, RSpec) have an issue trying to play nicely together.

RedGreen actually will cause Autotest to crash when one of the following happens

  • You save your code with a compile error (eg. missing ‘end’ tag)
  • You add new spec files

The error looks like this:

/Library/Ruby/Gems/1.8/gems/ZenTest-3.6.1/lib/autotest/redgreen.rb:8: undefined method `match' for nil:NilClass (NoMethodError)

and will follow the compile error from your code, then stop autotest.

I posted a comment for the original author, Pat Eyler, and the comment was manually approved, but I haven’t received any actual response to know whether or not it will be updated. Just in case it won’t be, I hope this simple fix is useful for you.

To fix this problem, open up your

/Library/Ruby/Gems/1.8/gems/ZenTest-3.6.1/lib/autotest/redgreen.rb

file and change line 8 from:

if at.results.last.match(/^.* (\d+) failures, (\d+) errors$/)

to

if at.results.last && at.results.last.match(/^.* (\d+) failures, (\d+) errors$/)

Very simple, and it does the trick!