Saturday, July 25, 2009

Building a Merb application, part one

So we have the following requirements for a party invite app.

"I want to be able to send invites to people via email or post, then I want them to hit the page and enter a code in the invite. After this they will be able to tick their name (or names in the case of a couple) as a way to RSVP.

The dress up theme of this party is 'inappropriate' so I also want people to suggest something inappropriate to wear.

When they submit the form they will be taken to a page with detailed party info and they will get a selection of suggestions by other guests."

lets create our project and submit it to source control

> merb-gen app merb-rsvp

> cd merb-rsvp

Now we need to fix the dependencies file as detailed here.

merb_gems_version = "1.1"

dm_gems_version   = "0.9.11"

do_gems_version   = "0.9.12"

lets run

> rake db:automigrate

And submit it to source control, in this case it is http://github.com/donibuchanan/merb-rsvp/tree/master

Ok so what's the very first thing we need? Before we get into admin, we'll work on the 'welcome' page. This will be a protected resource.

So lets write the simplest possible user story.

As an invitee

I want login and accept my invite

How does this translate to code? We use cucumber, lets install that

> sudo aptitude install libxslt-dev libxml2 libxml2-dev

> sudo gem install cucumber

> sudo gem install roman-merb_cucumber

> sudo gem install webrat

> merb-gen cucumber --session_type webrat

> merb-gen feature accept_invite

Lets create a feature (which is a user story with different scenarios) to look like this

Feature: accept invite

To accept an invite

An invitee

Logs in and checks the I accept box

Scenario: Login

Given I am not authenticated

And I am a user "myfriend" with password "apassword"

And I have an invite

When I go to /invites

And I fill in "login" with "myfriend"

And I fill in "password" with "apassword"

And I press "Log In"

Then I should see "Are you coming?"




Before running features, we need to tweak the cucumber.rake file, located in lib/tasks. We change line 5 to





t.binary = '/usr/local/bin/cucumber'




Now run the feature



> rake features



Its all yellow! Don't worry about the blue bits from the authentication feature, its actually running if you see the three green lines under Examples.



Our feature is not very useful, we need to implement steps that match the text in the feature.



We will create a file under features/steps called setup_steps.rb and put this in





Given /^I am a user "(.*)" with password "(.*)"$/ do |login,password|

@user = User.new(:login=>login)

@user.password = @user.password_confirmation = password

@user.save

end

Given /^I have an invite$/ do

#we haven't defined an invite object yet

end




Now



> rake features



Gives us a couple of green lines and a bunch of red! This is good, now we build the app until it runs green.



-----------



First up, lets create invites



> merb-gen resource invite



Lets make a link between invites and users.



In models/invite.rb add this before the "end"





belongs_to :user




and in user add





has n, :invites




Lets protect the invites resource and alter the index so it only shows the invites for the current user



In config/router.rb wrap the resources call in an authenticate block





authenticate do
resources :invites
end




Lets update our invite step in setup_steps.rb for having an invite





Given /^I have an invite$/ do
@user.invites.build if @user.invites.empty?
end</p>




In merb/merb-auth/setup.rb, mixin some redirect back functionality, just before the class Merb::Authentication part.



# Mixin the redirect back mixin





require 'merb-auth-more/mixins/redirect_back'




Finally lets add change the view so the feature will pass. Replace the contents on views/invites/index.html.erb with





Are you coming?




Now run features again



> rake features



4 scenarios (4 passed)



YES! I know it was the simplest of features but this was to give you an idea of what a feature was, if you haven't seen them before. 



Part two will complete the user story, currently the user is not accepting the invite.


To get the code clone the repository and check out this commit.


> git clone git://github.com/donibuchanan/merb-rsvp.git


> cd merb-rsvp


> git checkout 8e6695ac746a67e01edb08f7fea4e48878b8d3d9

1 comment:

  1. Thanks for your nice post. I spent some time to find a way to run cucumber with merb. t.binary = '/usr/local/bin/cucumber' line helped me out.

    ReplyDelete