September 2007
12 posts
All the kings horses
When you get a report from your site users or your clients complaining about seeing flash messages twice or seeing a flash message on the incorrect page, don’t listen to your coworker/boss when they tell you it must be a rails bug. It’s a feature.
When you do this…
def update
@user = User.find params[:id]
if @user.update_attributes params[:user]
flash[:success] = 'User...
Heading to Ruby East
A few of us are going to be trekin’ down to the Penn State campus for Ruby East over the weekend. If you’re attending, then just keep an eye out for the super-fly swaggering dudes. We’ll be standing behind and slightly to the left of them.
Chad Pytel, Dan Croak and myself will all be there, so drop on by and say hi.
See you there!
save bang your head, active record will drive you...
So in my never-ending quest to remove conditional logic from code, I began writing my Rails actions like:
def create
@user = User.new params[:user]
@user.save!
redirect_to user_path(@user)
rescue ActiveRecord::RecordNotSaved
flash[:notice] = 'Unable to create user'
render :action => :new
end
Instead of the traditional way using conditional logic like:
def...
forget about the view
Recently I was looking at some of ActiveRecord’s class level validation methods and realizing I don’t really use a lot of them. Until I took a look at #validates_inclusion_of.
Say we got
class Event < ActiveRecord::Base
TYPES = %w(daily weekly monthly)
end
schema
events (id, title, event_type)
view
app/views/events/new.rhtml
<%= form.select :event_type,...
i did not know that
Here’s something I discovered the other day:
ActiveRecord infers model attributes from the database. Each attribute gets a getter, setter, and query method.
A query method? I know this is true for boolean columns but non boolean columns?
class User < ActiveRecord::Base
end
users (id, name)
def test_should_say_if_it_does_or_does_not_have_a_name_when_sent_name?
...
1 tag
its not your responsibility
Sometimes in Rails you ask yourself: how do I test this?
One example is ActiveRecord associations.
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
end
Now from the documentation for ActiveRecord::Base#has_many we know it’s going to generate a method for us called #comments, so let’s test that.
def test_should_have_many_comments
...
time to step up
I’ve ignored testing my views in Rails for far too long. So I’ve started to try some stuff out in my existing functional tests.
assert_select
This is a really powerful assertion added to Test::Unit::TestCase that allows you to use CSS selectors when testing views and is the basis for all my view testing.
Say we have locations. A Location object has only 2 fields:
name – varchar
...
for the record
Ok, I just want to set this one in stone.
Rails’ polymorphic associations.
Ask yourself 2 questions:
Can this object belong to more than 1 type of object
class Car < ActiveRecord::Base
end
class House < ActiveRecord::Base
end
class LineItem < ActiveRecord::Base
belongs_to :salable, :polymorphic => true
end
Here’s a domain model from an app in which you can...
no need
Ruby’s modules give us opportunities to eliminate unnecessary classes from our designs.
In this example we have a citation, something that could be found in any academic paper, and 2 different ways of formatting it
MLA (Modern Language Association)
CMS (Chicago Manual of Style)
The obvious implementation ported from say Java to Ruby would look like:
class Citation
class <<...
Brittle Tests
A friend and I were talking about the scaffold mailer tests that are generated by rails, and it got me on a bit of a rant. Here’s what you start out with:
class MailerTest < Test::Unit::TestCase # ...stuff... def setup # ...more stuff... @expected = TMail::Mail.new @expected.set_content_type "text", "plain", { "charset" => CHARSET } @expected.mime_version = '1.0' end ...
let's get ready to rumble
We’re happy to be sponsoring Rails Rumble with what we think is one of the coolest prizes:
Good luck to everyone who participates.
do we need that
In Ruby there’s 2 ways to add behavior to a class:
inheritance
modules
Now inheritance is the same in Ruby as just about everywhere else.
Modules bring something different to the table that’s not as common in lanugages. Modules are a form of multiple inheritance. A couple languages that provide support for classes and objects do offer multiple inheritance e.g. C++ and lisp’s...