Category Archives: development

On this page:

Rails: Preserving test data

I’m using Cucumber for testing my Rails project. The standard practice for automated testing in Rails is to make each test case completely self-contained and wipe out the test data after running the test. The test system accomplishes this by wrapping the operations in a transaction and rolling that transaction back at the end of the test. This is great, except when you’re developing code and you want to poke around the test environment to see what’s going on outside the handful of error messages you might get from a failed test.

I set up my test environment so that data stays in place after a test is run, and I modified my tests to delete data they need deleted. This is what I set in my features/support/env.rb:

Cucumber::Rails::World.use_transactional_fixtures = false

I also removed database_cleaner.

You can set this behaviour on a case-by-case basis with the tag @no-txn.

Running the tests individually with bundle exec cucumber ... now works. I still have to figure out why the database gets dropped when I do rake cucumber, though…

2011-04-24 Sun 16:21

Rails: Paperclip needs attributes defined by attr_accessible, not just attr_accessor

I wanted to add uploaded files to the survey response model defined by the Surveyor gem. I’d gotten most of the changes right, and the filenames were showing up in the model, but Paperclip wasn’t saving the files to the filesystem. As it turns out, Paperclip requires that your attributes (ex: :file_value> for my file column) be tagged with attr_accessible, not just attr_accessor.

Once you define one attr_accessible item, you need to define all the ones you need, or mass-assigning attributes with update_attributes will fail. This meant adding a whole bunch of attributes to my attr_accessor list, too.

If you’re using accepts_nested_attributes_for, you will also need to use attr_accessible there, too.

Sharing the note here just in case anyone else runs into it. Props to Tam on StackOverflow for the tip!

2011-04-01 Fri 12:41

Setting up Ruby on Rails on a Redhat Enterprise Linux Rackspace Cloud Server

1. Compile Ruby from source.

First, install all the libraries you’ll need to compile Ruby.

yum install gcc zlib libxml2-devel 
yum install gcc
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel

My particular application has problems with Ruby 1.9.2, so I compiled Ruby 1.8.7 instead. This can be downloaded from ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.gz

Unpack the source code for Ruby. Configure and install it with:

./configure
make
make install

Add /usr/local/bin to the beginning of your PATH.

2. Install Ruby Gems.

Downloadcd the latest Ruby Gems package and unpack it. I got mine from http://production.cf.rubygems.org/rubygems/rubygems-1.7.1.tgz . Change to the directory and run:

ruby setup.rb

3. Install Rails and rake

gem install rails rake

If all goes well, you should now have Rails and rake.

Troubleshooting:

*builder-2.1.2 has an invalid value for @cert_chain*

Downgrade Rubygems to version 1.6.2 with the following command.

gem update --system 1.6.2

(Stack Overflow)

sqlite3-ruby only supports sqlite3 versions 3.6.16+, please upgrade!

Compile sqlite from source:

wget http://www.sqlite.org/sqlite-amalgamation-3.7.0.1.tar.gz
tar zxvf sqlite-amalgamation-3.7.0.1.tar.gz
cd sqlite-amalgamation-3.7.0.1
./configure
make
make install
gem install sqlite3

LoadError: no such file to load – openssl

  1. Install openssl and openssl-devel.
    yum install openssl openssl-devel
    
  2. Go to your Ruby source directory and run the following commands:
    cd ext/openssl
    ruby extconf.rb
    make
    make install
    

LoadError: no such file to load – readline

yum install readline-devel

Change to your Ruby source directory and run the following:

cd ext/readline
ruby extconf.rb
make
make install

(Code snippets)

You can’t access port 80 from another computer.

Port 80 (the web server port) is blocked by default on Redhat Enterprise Linux 5.5. Edit /etc/sysconfig/iptables to allow it, adding a line like:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Make sure you put it above the REJECT all line.

Load your changes with

/etc/init.d/iptables restart

(Cyberciti)

2011-04-04 Mon 11:06

Running the Selenium IDE testing plugin with Firefox 4

Selenium is a web testing framework that allows you to test web applications involving HTML and Javascript. The plugin hasn’t been updated to indicate that it works with Firefox 4, so you can’t install it directly.

You can use the Firefox Add-on Compatibility Reporter to install Selenium and other Mozilla Firefox plugins that have not yet been marked as compatible. After you install the compatibility reporter and restart your browser, you should be able to install the official version of the Selenium plugin.

Props to the Mozilla support forum for the tip!

2011-04-01 Fri 12:21

Becoming a faster developer

(NOTE: Becoming a faster developer isn’t necessarily the same as becoming a more productive developer. Becoming a more productive developer is better. Speed isn’t everything!)

Following up on my post about typing speed, QWERTY/Dvorak/alternate layouts, and the idea that your keyboard layout probably has little to do with your performance (although it might have a little to do with your happiness), I wanted to think about what makes a fast programmer. People tell me I’m fast. I know people who can pull programs together even faster. We touch-type, for sure, but that’s the least of it.

A huge part of development speed is experience. If you’re familiar with a programming platform, the error messages, the structure, the way things work and the way things are named, you can learn new concepts and write correct code much faster than a newbie can.

What if you’re faced with a new framework? You’ll still get a speed boost if you can relate the concepts to other things you’ve learned. If you can figure out the control structures you need and the debugging techniques you can use, then it’s mostly a matter of translating to the new framework and picking up any quirks or local idioms.

So let’s break it down further. What are small, specific skills that can help a developer get really fast?

  • Touch-typing: Still practically a given. You need to be able to think about your code, not about typing code. If you aren’t yet a touch-typist, sit down and work on that.
  • Speed-reading: This comes in handy everywhere. Much of programming is reading: reading requirements, reading other people’s code, reading documentation, reading logs and debugging output. This is probably more useful than typing at a gazillion wpm. You need to be able to quickly spot the significant parts. Learn how to skim.
  • Working with complex structures: The better you get at understanding complex structures or logic, the faster you can come up with more effective solutions, and the less time you spend going back and forth. You don’t have to do it all in your head. Find systems that work well for you (notes? mindmaps? diagrams?) and use them.
  • Problem decomposition: Breaking big problems down into smaller testable steps can help you make quick progress and keep things manageable. This is also one of those skills that can give you lots of leverage on time. If you can get really good at spotting the core of the problem and figuring out the key parts of the solution, you can get something into place much faster. It’s also a useful skill for testing code thoroughly.
  • Good development practices: Source code control, testing, and all sorts of other development hygiene practices means less time spent fixing avoidable mistakes.
  • Collaboration: If you can get someone else to do the things you’re really slow at so that you can focus on the things you’re really fast at, the team can get much faster. This also includes documenting your work so that people don’t have to reinvent the wheel.

What would you add to this list?

Learning more about Websphere and web service development

So I finally figured out what was wrong with the way I was trying to generate my web services for Websphere 6.1. I’d been using “Generate Java bean skeleton” from the WSDL file, which worked fine for the 6.0 target, but which didn’t work for 6.1. The correct way to do it is to right-click on the service and choose “Generate – Top-down Service”.

I also spent some time figuring out how to correctly use the XSD the IT architect sent me in order to use it for the data types in the WSDL. This is how:

<xsd:import schemaLocation="....xsd" namespace="...">
</xsd:import>

One of the pieces that was missing for me was dealing with namespaces, but once I got my head around XML again, I added some namespaces and got the referred types working.

So I’ve retwiddled our web services and gotten them to work with the new data structures. My test cases pass again. Progress!

2011-03-21 Mon 20:03