what_i_am.upto(2010)

Tag: sinatra

Introducing ActiveRecord Skeleton

by Christoph Olszowka on Feb.21, 2009, under ruby

It’s common knowledge that ActiveRecord can be used nicely without Rails in Ruby apps. Setting up the basic stuff is pretty easy by just requiring the activerecord gem in your app and defining a couple of models.

But what about migrations, yaml config and different environments? You have to set all this up manually, and even though this is not really hard, it’s still a time-consuming task. No more!

Enter: ActiveRecord Skeleton

I published a new project on GitHub today, called ActiveRecord Skeleton, and it really is just that: It will instantly give you YAML db config, database migrations (including Rake tasks for migrating the database as well as generating new migrations), a Rails-style project directory structure, and also the option of using environments by specifying the RACK_ENV environment variable. Why RACK_ENV? Well, you might also see this as a super-minimalist web app template, allowing you to set up a db-backed Sinatra (just an example, anything should work really) app instantly. All you really got to do is get the skeleton and add an application.rb file containing the following:

require 'sinatra'
require 'init'

get '/' do
  User.all.map {|u| u.name}.to_sentence
end

After running application.rb and pointing your browser to localhost:4567, you will see the list of existing users (assuming you’ve got the database and model set up properly, of course).

Want to create a user? Start irb, do a require 'init' followed by User.create.

This skeleton is really minimalist, as I didn’t want it to be over-opinionated. It’s meant to give an easy start for about any Ruby code you might want to have database-interaction with and is ready to go with sqlite 3 in the default setup, but you really can use it with any database ActiveRecord supports.

As a consequence of the minimalist approach, the skeleton does not supply tasks or folders for tests. The reason for this is simply that I did not want to force users of the skeleton into using a certain testing library. Test::Unit, Shoulda, RSpec, (your favorite testing framework mentioned here), just pick your favorite and add it your project. This is by no means a suggestion to ditch testing altogether though. You’re just supposed to pick your flavour of TDD or BDD.

Now head over to GitHub and give it a spin!

Leave a Comment :, , , more...

Giraffesoft releasing Gems and Plugins this week

by Christoph Olszowka on Feb.17, 2009, under ruby

The folks at Giraffesoft have decided to release some of the common code they’ve been using in their projects into plugins and gems on their brand-new company blog. In their own words:

At giraffesoft, we’re big believers in extracting functionality sooner rather than later. This practice has resulted in a ton of code that we use in all of our projects — nice and DRY.

Only problem is, we never get around to releasing this stuff. Starting a blog seemed like a good excuse to spend some time polishing up some code and, you know, writing READMEs.

So, starting today, we’ll be releasing an open source project every day this week. They’ll probably be mostly rails plugins. But, you never know. Something else might float in.

Actually, something else did float in right on the first day: Classy Resources for Sinatra, a gem allowing you to create restful web services that can be consumed by ActiveResource without manually defining all of the controller actions, but rather with a (very Railsish) configuration-style method:

define_resource :posts, :member => [:get, :put, :delete],
:collection => [:get, :post],
:formats => [:xml, :json, :yaml]

The thing is pretty customizable and even if it’s not exactly what you need, it still might be interesting to investigate the source code to see how to refactor common code into modules in Sinatra.

If you don’t know Sinatra yet, you really should go and check it out at Github and have a look at the Sinatra book.

Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Recommended read!

A few highly recommended sites...