Poniard: a Dependency Injector for Rails
I just open sourced poniard, a dependency injector for Rails. It’s a newer version of code I posted a few weeks back that allows you to write controllers using plain ruby objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
module Controller class Registration def update(response, now_flash, update_form) form = update_form if form.save response.respond_with SuccessfulUpdateResponse, form else now_flash[:message] = "Could not save registration." response.render action: 'edit', ivars: {registration: form} end end SuccessfulUpdateResponse = Struct.new(:form) do def html(response, flash, current_event) flash[:message] = "Updated details for %s" % form.name response.redirect_to :registrations, current_event end def js(response) response.render json: form end end end end |
This makes it possible to test them in isolation, leading to a better appreciation of your dependencies and nicer code.