Sunday, January 27, 2008

Merb Supports rSpec Text Stories

Today I managed to release a new plugin for merb that provides basic support for rSpecs text stories. It's a bit experimental at the moment so if you want to pitch in and get it going smooth, come on down ;)

I have been hanging onto it for a little while, but I keep trying to get into the release early and often philosophy.

Get It

Like all merb plugins, this one is a gem. You can get it from gem
sudo gem install merb_stories
You can get it from source
svn checkout svn://rubyforge.org/var/svn/merb-plugins/merb_stories
cd merb_stories
rake install

Usage

At it's heart merb_stories is almost just a generator, but you do need to add it as a dependency to get going with it. You can safely remove it when you deploy, but for now it needs to be there to run the stories.

Generate A Story

In your Merb.root
ruby script/generate story my_story
This will generate all the support files, if they're missing, and also the story, story steps etc.

If you want to organise your stories a bit more, you can put them into sub-directories.
ruby script/generate story story_group/story
You can go as deep as you like with the directories.

Running Your Stories

It's really simple to run your stories. merb_stories supplies a rake task to remove any pain you might have.
rake story[my_story]
or if you used subdirectories
rake story[story_group/my_story]
You can also just run all of them at once.
rake story[all]


Thats really about all there is to it. To check out what to use stories for checkout the rSpec site.

Friday, January 18, 2008

Cool Stuff with the Merb Router

Today I found out something I didn't know about the merb router when looking for routing to subdomains.

I've got a route that looks like this:
  r.match(:first_subdomain => ":account_name") do |k|
k.match("/:controller/:action") #any old route match really
end
That will put the value of the first subdomain into params[:account_name]. Hang on though... That doesn't work by default.

How Is It So!!!?!!?

Well it turns out that when you pass a hash to r.match it uses the hash keys as method calls to the request object. It then compares the values returned to the ones you specify.

So you can have heaps of mini matchers in you matcher and all you have to do is specify a method on the request object and then use that method name as a hash key in the match. This is probably a bit dangerous so be careful with it. In this example this does the job at the top of the route file
module Merb
class Request
def first_subdomain
subdomains.first
end
end
end

Merbful Authentication Update

There have been a couple of little teething problems with the merbful_authentication gem, mostly because it's not automatically available to your app when you run the generators. There's all that you had to do to get going which really bugged me.

The Skinny

To setup your newly generated Authenticated user, now all you need to do is setup the routes. (and the mailer if your using activation). All the including and dependency is taken care of for you. This is current as of 0.1.2. If you care about why and how... read on.

Why and How

The main issues were with the controllers not having the mixin by default. Then when you try and include it you have to make it a dependency. Not very nice... I also kept getting tripped up on the merb_helpers dependency too.

Well you'll be pleased to know that now, when you generate an authenticated user and you app loads, all the authenticated system is mixed into Application. The controller that your controllers should all inherit from. So it's now available to all your controller automatically :)

Also merb_helpers has been made a dependency where it's needed, and also a dependency on the gem so you don't have to worry about it. Ahhh... gem plugins are so nice like that.

It's up on rubyforge so sudo gem install merbful_authentication should do the job.

If you want source with that see my other post for instructions on where to get it.

Happy Hacking!

Friday, January 11, 2008

Dynaically Setup Rake Tasks

This is probably really old news. I just setup my first rake task tonight though so I thought I'd put it up here. This is more for myself really so if it's not your cup of tea turn away now.

I've been playing with rSpec's text stories in Merb but I couldn't find a way to make a rake task that would take an argument for a particular story to run. e.g.
rake story:run my_story
So I've done the next best thing. I scan my directory for story files and create the rake tasks dynamically. Now I can use:
rake story:run:my_story
As well as
rake story:run:all
The code to make this dream a reality is
# Allows all found stories to be run individually by using
# rake story:run:story_name
namespace :story do
namespace :run do
all_stories = []
Dir.glob('stories/stories/**/*.rb').each do |path|
story = path.split("/").last[0..-4]
desc "Run spec story #{story}"
task "#{story}".to_sym do
sh %{ruby stories/stories/#{story}.rb}
end
all_stories << story
end

desc 'Run all stories'
task :all do
all_stories.each do |story|
puts
sh %{ruby stories/stories/#{story}.rb}
end
end
end
end

Monday, January 7, 2008

Merbful Authentication Update 0.1.1

Well, it's been out in the wild for a couple of days now, and there is at least one glaring error in the merbful_authentication code. The generator is called "authentication" instead of "authenticated" not too good for a port. That's changed now. Also there is some changes to give nice error messages when no ORM is selected or sequel is selected.

If you want to get the update you can get it the same as before.

Friday, January 4, 2008

Merbful Authentication is Born

Hooray... The initial release of merbful_authentication is cooked. It took a bit longer than I thought it would, mainly thanx to my unfamiliarity with test_unit. Well, that and I was away over the christmas holidays.

At the moment there is support for Datamapper and Activerecord ORM's. Hopefully Sequel will follow soon.

How to get it

The gem has been released on rubiforge, so the usual should do the trick
sudo gem install merbful_authentication
If you want source with that.

Git:
git clone git://activereload.net/restful_authentication.git merbful_authentication.git
git checkout -b local/merbful_authetication origin/merbful_authentication
Or SVN
svn co svn://rubyforge.org/var/svn/merbful-auth
From either of these you can package and install the gem
rake package
sudo gem install pkg/merbful_authentication-0.1.0.gem

Usage

It has the same usage as restful_authentication, without the stateful stuff. So (from README)
To use:

./script/generate authenticated user sessions \
--include-activation

The first parameter specifies the model that gets created in signup
(typically a user or account model). A model with migration is
created (if migrations are available), as well as a basic controller with the create method.

The second parameter specifies the sessions controller name. This is
the controller that handles the actual login/logout function on the
site.

The third parameter (--include-activation) generates the code for a
Mailer and the required support code. Don't forget to setup your mailer for this.

Required Setup

Unfortunately there is some setup required. At the moment, some named routes are required. You can set these up in you config/router.rb
      r.resources :users
r.match("/login").to(:controller => "Sessions", :action => "create").name(:login)
r.match("/logout").to(:controller => "Sessions", :action => "destroy").name(:logout)
r.match("/users/activate/:activation_code").to(:controller => "Users", :action => "activate").name(:user_activation)
That last one is only required if you include activation. Don't forget to setup your mailer configs if your using it too.

Also, you need to have merb_helpers installed. This is for the form generation stuff. In config/dependencies.rb
dependency "merb_helpers"
All crit, comments, bug reports, patches are welcome. Patches are more welcome of course ;)

Update: You will need to install merb and merb_helpers from trunk until the official 0.5 gem for merb comes out.