Archive for the ‘Ruby on Rails’ Category

CRUD Scaffold Generator

Friday, June 27th, 2008

Scaffold

Rails 2.1 removed the original scaffold generator and replaced it with a new RESTful scaffold generator.

Bummer.

I’m not talking about the dynamic scaffolding that comes from putting “scaffold :model” in a controller. That “dynamic scaffolding” was removed in Rails 2.0 and few of us miss it. I’m talking about when you type “ruby script/generate scaffold Model” from the command line and get generated model, controller and view files as a result.

While I understand that Rails is “opinionated software” and that they want to encourage everyone to get on the REST bandwagon, I think completely removing the old scaffold generator was a mistake for one simple reason:

It unnecessarily raises a barrier to entry for beginners.

When beginners first start on Rails there is a lot to digest. Installation issues, Ruby language, MVC architecture, ActiveRecord, Rails syntax, routes, migrations and deployment—just to name a few. In my opinion, asking someone eager to learn Rails—someone who may have a background in PHP but who still doesn’t understand code blocks or the basic Rails .find syntax—to also use REST from the start is only going to frustrate them and slow the growth of Rails adoption. They need to walk before they can run. We have added best practices for advanced users at the expense of the newbies.

(more…)

Are There Steps to Writing Code?

Tuesday, June 17th, 2008

A student sent me email to ask: “In a general sense, when writing code, is there a sequence of steps or a pattern to the logic when writing the code?”

It’s a good question. Experienced computer programmers have learned these steps—either through intuition or experience—but beginning programmers are often mystified. Once you learn the language syntax, how exactly do you use it to approach a programming problem? Especially if you want to try to use good programming techniques and to solve the problem in a smart and efficient way.

The answer to the question “Are there steps to writing code?” is “Yes”. After the jump, I’ll give three ways to develop the logic necessary to write good code.

(more…)

Baby.new

Friday, June 13th, 2008
piper = Baby.new(:name => 'Piper',
                 :born => '2008-06-09 18:22:00 EDT',
                 :weight => {:lbs => 6, :oz => 8},
                 :length => {:inches => 21.25})

skoglunds.children << piper

piper.daily do |p|
  8.times do
    p.eat
    p.poop
    p.sleep
  end
end

Piper

A List Apart #257

Tuesday, April 22nd, 2008

A List Apart Logo

A List Apart, Issue #257, has two articles on Ruby on Rails. Both articles are targeted toward Rails beginners and curious designers who may have heard the buzz but few details.

The information may be old news for many developers, but both articles are up-to-date, well-written introductions that are still worth bookmarking. Save them for the next time a beginner asks you for an explanation. Forward them to co-workers who don’t understand what you’ve been raving about. Print them out and leave them laying around so your most earnest PHP/.NET/Java friends can sneak a peak at them when no one is looking. Maybe they can even help explain to your parents/friends/significant-other what you do for a living.

Passenger

Monday, April 14th, 2008

Passenger Logo

From the beginning, one of the weaknesses of Ruby on Rails has been the complexity of deploying a Rails application. Deployment was not difficult once you had done it once or twice, but it required several extra steps, learning about additional technology (like Mongrel or FastCGI), having a fair amount of server admin skills and a bit of patience. Most significantly, deploying a Rails application was much harder than deploying a PHP application.

With PHP, you can just drop your application files on the server, configure Apache to server up those files and you are done! It is possible to do this thanks to a module for Apache that allows it to serve up PHP files without needing a “handler” in between. This Apache module can be referred to as “mod_php”. This has been one of PHP’s strengths and one of it’s advantages over Rails.

Last week that all changed when Passenger was released by Phusion. Passenger is also referred to as “mod_rails” because, like mod_php (and mod_perl, mod_python, etc.), it is a module that allows Apache to serve up Rails applications without needing a handler like Mongrel or FastCGI. Once Passenger is installed, deploying a Rails application becomes as easy as PHP—simply drop files on the server and configure Apache to serve them.

It is notable that it is nicknamed “mod_rails” and not “mod_ruby”. mod_ruby is a different project whose development seems to have stalled in mid-2006. The difference also indicates that Passenger currently works only with Rails and not with other Ruby frameworks (like Merb or Camping).

Phusion made Passenger very easy to install and set up. Ryan Bates at Railscasts has a demonstration of how easy it is. Notice that he does not launch WEBrick, start up Mongrel or use FastCGI, he just restarts Apache and the application becomes available!

This is the first release of Passenger, so it is very possible that there will be bug fixes and improvements in the near future. There has already been some discussion about how to reduce the memory usage of Passenger (currently twice the memory usage of a single Mongrel instance). It has also been noted that if an application sits idle for a while it takes a little while for it to load again (I find that FastCGI does this too). Fixes, work-arounds and good advice will certainly be surfacing over the next few weeks as developers test it out on a wide range of web applications and server set ups.

One thing is for sure: the Ruby on Rails community is excited at the potential Passenger has for making deployment easier and thereby lowering a key barrier to entry for new developers. PHP took off once it became an Apache standard. Now Rails is poised to do the same.

Testing in Rails: Part 11 - Running Unit Tests

Wednesday, February 27th, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

One final post about unit tests and then we will move on to functional testing.

Running All Tests in a Single Test File

Up to now we have run our unit tests by simply asking Ruby to process the file that contains our tests.

>ruby test/unit/wine_test.rb

When you are first developing unit tests on a single model, this may be the easiest way to do it. It will run all the tests in the WineTest file.

(more…)

Testing in Rails: Part 10 - Assertions

Wednesday, February 20th, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

I dreamed I was assertive, image copyright Celia Perez
© Celia Perez

At this point in the series, we have covered most aspects of unit testing in Ruby on Rails. We learned the basics both in Ruby and in Rails and discussed how to write meaningful tests. We set up fixtures to make working with sample data easier. We learned to write tests for ActiveRecord objects and their relationships, validations, additional attributes and callbacks.

The only thing we have not done is write tests for the custom methods in our Winery and Wine models (such as: location, find_newest, age, is_antique?). But I feel confident that you have the testing skills to make that an easy task.

To ensure that you have everything you need for writing unit tests on your own, in this section I want to expand on the assertions you can utilize for unit testing.

(more…)

Testing in Rails: Part 9 - Attributes and Callbacks

Thursday, February 7th, 2008

Wine Rack

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.

In previous sections, we learned to unit test the ActiveRecord associations and validations in our classes. Both are extremely common and appear in most Rails models. While less common, attribute definition methods and callbacks are still used somewhat frequently and are worth learning to test.

Hopefully by now your knowledge and confidence about writing tests is growing!

Attribute Definition Methods

When I refer to attribute definition methods, a classification I made up, I am referring to the methods that define and describe attributes in our model. There are two kinds. First, there are methods in the Ruby language that define attribute reader and writer methods: attr, attr_accessor, attr_reader, and attr_writer. Second, there are methods in the Rails framework that limit the access to attributes: attr_accessible, attr_protected, and attr_readonly.

(more…)

Testing in Rails: Part 8 - Validations

Wednesday, January 23rd, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Now that we have unit tests to insure that our models are related properly, we are ready to test our the validations in our models.

There are two approaches to testing your validations. It will be useful to examine both techniques because it will help you to write better tests overall. First, we need to write tests that confirm the code we have in place is working properly—as we have done thus far. Second, we need to think about special cases that we might not have coded into our validations already. This is where writing tests can really improve your code, and validations are a great place to see it in action.

(more…)

Testing in Rails: Part 7 - ActiveRecord Relationships

Tuesday, January 8th, 2008

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Unit Testing ActiveRecord

Wine Grapes

In the unit tests we wrote previously, we tested everything in the class. The rule of thumb was that everything needs to be tested. That has not changed now that we are working with ActiveRecord—each method still needs a test.

But what testing should be done on the basic database CRUD (Create, Read, Update, Delete)? After all, that is what is most different about classes that inherit from ActiveRecord, right? It may surprise you to learn that we do not need to test basic CRUD.

Why not? ActiveRecord handles all of our database activity for us. It knows how to connect to the database, how to create, find, read, update, delete, use conditions and sort. That is the reason why we are using a framework like Rails to begin with: because we can inherit all that ActiveRecord goodness without having to rewrite it ourselves. We can trust that ActiveRecord will do its job correctly.

photo by Tomás Castelazo

That is not blind trust—ActiveRecord has its own unit tests that the core Rails team uses to insure that it works properly. Therefore we can assume that everything our class inherits from ActiveRecord is solid and tested. It was unit tested before we installed it. We just need to focus on our code in the subclass.

The corollary to “Test Everything” is: “If it is code you added, it is code you need to test.”

(more…)