Entries from July 2008 ↓

(0.6.0) 4 Little Known Mack Features

I thought it might be fun to start posting about some of the little known features in Mack. There are a treasure trove of them in there, so let’s pick a couple and start there.

render(:url)

This is a great little feature, one of my personal favorites. In your views you can do things like this:

<%= render(:url, "http://www.mackframework.com) %>

that will render the contents of http://www.mackframework.com into your view. You can also do ‘local’ urls.

<%= render(:url, "/users/1") %>

will make an internal request to your application and render the results of “/users/1″ into your view. The optional 3rd parameter to render allows you to do things like set the HTTP method:

<%= render(:url, "/users/1", :method => :post) %>

or add parameters you want to pass to the URL you want to render: 

<%= render(:url, "/users/1", :method => :post, :parameters => {:id => 1}) %>

Error handling in routes

Routing allows you to define controllers/actions you want to catch and handle exceptions that happen in other controllers. Let’s look at the following routes.rb file:

Mack::Routes.build do |r|
  r.resource :users
  r.home_page "/", :controller => :default, :action => :index
  r.handle_errors ArgumentError, :controller => :problems, :action => :arguments
  r.handle_errors DataMapper::ObjectNotFoundError, :controller => :problems, :action => :not_found
  r.defaults
end

What’s going on with r.handle_errors you ask? Well, first we tell the routing system which error we want to capture in our controllers, DataMapper::ObjectNotFoundError, then we tell it which controller and which action we want to handle that error.

When an exception is thrown during a request Mack checks to see if that exception has been registered, if it has been then the request gets forwarded to the defined controller and action for handling. So in the above example if a DataMapper::ObjectNotFoundError is raised, the request will be forwarded to the ProblemsController, not_found action.

One of the really nice things about this is that you have access to the original request, so you can’t get the page the person was trying to access, any parameters that were passed, etc… You also have access the exception itself with the caught_exception method.

Server-side redirects

Let’s be honest, redirects are the most exciting topic, and this is the first of two sections on it! I’ll try to be brief. When dealing with redirects it can sometimes be helpful to do a server-side redirect. The difference, for those who don’t know, between a server-side redirect and a regular redirect is the following. With a regular redirect the response is sent back down to the client’s browser, which then issues another response back to the server for the new url that was specified in the previous response. You’ll often hear this referred to as a client-side redirect. A server-side redirect sends you to a different url on the server, without first sending down a response to the client. Because of this the client only gets one response.

To do a server-side redirect in Mack is very easy. Here’s what a client side redirect in an action would look like:

redirect_to(users_index_url)

To make that a server-side redirect you would simply pass an extra option to the redirect_to method:

redirect_to(users_index_url, :server_side => true)

Redirects in routes

This is a cool little feature. Let’s say that you have changed a few urls around. You want a quick way to redirect people who have bookmarked the old urls to the new urls. You could have a controller that did nothing but that, but that seems like a lot of extra work, and it’s really something that your routing system should be doing for you anyway. Enter redirects in routes.

Mack::Routes.build do |r|
  r.old_foo "/my_old_foo", :redirect_to => "/my_new_foo", :status => 301
end

From now on anything comes in to “/my_old_foo” will be redirected to “/my_new_foo” with a status of 301.

Mack 0.6.0 featured on this week’s RailsEnvy Podcast

http://www.railsenvy.com/2008/7/23/rails-envy-podcast-episode-039-07-23-2008

If you’re not familiar with the podcast, I highly recommend checking it out. It’s a very informative, and highly entertaining podcast about all things Ruby.

Good work guys, keep it up!

Release 0.6.0

Well, it’s finally here, Mack 0.6.0! This release has taken a long time, but I feel that it’s definitely worth it. This has to be the best release of Mack to date. During this release the Mack dev team grew by 100%, Darsono Sutedja, previously a contributor to Mack, is now a full time Mack core team developer! Darsono has done an absolutely great job on this release, thanks Darsono. With that out of the way, let’s jump straight to the good stuff, shall we?

DataMapper 0.9.2 Support

At long last Mack supports DataMapper 0.9.2. Because previous versions of Mack used DataMapper 0.3.2, there is some upgrade work that you’ll need to do, but trust me, it’s worth it. DataMapper 0.9.2 is a gigantic leap forward from 0.3.2. If you have problems installing the data_mapper gem, make sure that you don’t have gems.datamapper.org in your source path for rubygems. You can remove it with the following command:

gem source -r http://gems.datamapper.org 

RSpec Testing Support

RSpec is now the default testing framework for new Mack applications. This innovative testing framework makes it fun, and simple, to do behavior driven development. If you haven’t yet played with RSpec, I would highly recommend it. I wasn’t a big fan of ’spec’ testing prior to meeting Adam French, of DataMapper fame, and he turned me on to the joys of rspec. I think you’ll like it too, if you give it a chance. :)

mack-more

There is now a mack-more package. This will house all the optional gems available to the Mack framework. This include things like HAML and Markaby support, localization, etc… Use what you want, ignore the rest. This will help keep the core code clean, light, and of course, fast.

Localization/l10n Support

Darsono did a great job with the mack-localization gem. There is now optional support for internationalization in Mack applications. Check out the gem, it’s definitely worth it if you need to support multiple languages for your web application.

Much, much more!

Of course, there’s a lot more Mack than the stuff I just mentioned, there’s tons of bug fixes, some code clean up, some API changes, transactional testing support, the ability to catch specific errors in routes and have them diverted to a controller/action for handling.

There is a great wiki entry on upgrading your existing Mack project to 0.6.0 here. I highly recommend reading it whether or not you have an existing Mack application.

With this release the Mack team is also fully embracing Lighthouse. If you find bugs, or have suggestions please visit http://lighthouse.mackframework.com and log them there. You’ll also find the list of features that will eventually be added to Mack.

Changelog:

  • INCOMPATIBILITY NOTICE: Moved Mack::Configuration.root to Mack.root
  • INCOMPATIBILITY NOTICE: Moved Mack::Configuration.env to Mack.env
  • INCOMPATIBILITY NOTICE: Mack::Configuration.* path methods no longer exist. Use mack-paths instead.
  • INCOMPATIBILITY NOTICE: Dropped mack_ruby_core_extensions in favor or mack-facets
  • INCOMPATIBILITY NOTICE: Mack::Controller::Base is now Mack::Controller (and it’s now a module)
  • INCOMPATIBILITY NOTICE: Haml renderer is now part of mack-more.
  • INCOMPATIBILITY NOTICE: Markaby renderer is now part of mack-more.
  • INCOMPATIBILITY NOTICE: Changed request param’s accessor routine, from params(key) to params[key]
  • INCOMPATIBILITY NOTICE: Test::Unit::TestCase is no longer the default testing framework, RSpec is. If you wish to use Test::Unit::TestCase add the following config parameter to your config/app_config/default.yml file:
      mack::testing_framework: test_case
  • INCOMPATIBILITY NOTICE: ENV["_mack_env"] and ENV["_mack_root"] are no longer supported, please use ENV["MACK_ENV"] and ENV["MACK_ROOT"], or just Mack.env and Mack.root
  • INCOMPATIBILITY NOTICE: MACK_DEFAULT_LOGGER constant is no longer available. Please use Mack.logger instead.
  • INCOMPATIBILITY NOTICE: The ApplicationHelper module is now deprecated. Please move view level helpers into Mack::ViewHelpers::* and controller helpers into Mack::ControllerHelpers::<controller_name>
  • thin.ru and thin.yml are no longer needed, you may delete them.
  • RSpec assertions are now automatically added.
  • Added a Mack::Testing::Response class to make testing of responses easier.
  • moved test_extensions to testing and added the module Testing to the files in it.
  • Added a dependency on mack-more
  • Added a set of Rake tasks to update existing Mack applications.
  • [#30] Added Mack::Logging::Filter to filter out unwanted parameters from the logs.
  • In routing you can now define a route that will catch an Exception from another controller.
  • ORM will not be initialized if the app never specify which ORM to use.
  • All Mack unit tests are now written using rspec-1.1.4.
  • mack command now takes an optional -t flag to determine which testing framework to use–value could be test_case or rspec [default]
  • new setting added to app_config: testing_framework
  • gem: cachetastic 1.7.2
  • gem: application_configuration 1.5.0
  • gem: erubis 2.6.2
  • gem: genosaurus 1.2.1
  • gem: thin 0.8.2
  • gem: rspec 1.1.4

0.6.0 Coming VERY soon!

Well folks, as you know, things have been a bit quiet on the Mack front these days, but it’s all been for a very good reason. We’ve been prepping the next release, 0.6.0. This release is expected to hit the streets sometime the beginning of next week. There’s a lot of really great stuff in the release, DataMapper 0.9.2 support (finally!), RSpec testing support, a mack-more project to house a bunch of cool Mack related gems, Exception handling in Routing, and more… A full list will be available when the release happens.

In addition to the great new features, a lot of bugs have been worked out, and more of the API has solidified further. Because of some of these changes I’ve posted an upgrade guide from 0.5.x to 0.6.0. This guide can be found on the wiki

This guide is meant to make upgrading as quickly and as a painless possible. There are a couple of big changes, but they should be fairly simple to make.

I’m very happy with this release, it’s been long toiled over, and it shows. It’s very stable, fast, and fun. I think everyone is really going to like it. 

As more info about the release becomes available, I’ll keep everyone posted.