Next → Twelfth Step, Validation and Errors Tenth Step, Configuration ← Prev

Eleventh Step, Refactor with AspectHelper

Now, if you take a closer look at the Controller you will see:

def create
  title = request['title']
  TodoList[title] = {:done => false}
  redirect R(self)
end
 
def open title
  task_status title, false
  redirect R(self)
end
 
def close title
  task_status title, true
  redirect R(self)
end
 
def delete title
  TodoList.delete title
  redirect R(self)
end

We did some refactoring before, by introducing #task_status, but here we have repetition again: redirect Rs() after each method did its job.

However, we can take advantage of one of the helpers Ramaze offers, the AspectHelper. It allows you to easily wrap actions in your controller with other methods

In your Controller, replace the previous chunk with following:

def create
  title = request['title']
  TodoList[title] = {:done => false}
end
 
def open title
  task_status title, false
end
 
def close title
  task_status title, true
end
 
def delete title
  TodoList.delete title
end
 
helper :aspect
after(:create, :open, :close, :delete){ redirect(Rs()) }

Alright, that looks a lot nicer already and is definitely easier to maintain.

There is a symmetrical before aspect that you could take advantage of as well, and in case you want to add required authentication for all actions of a Controller you could use before_all and after_all instead of a list of action-names.

Next → Twelfth Step, Validation and Errors Tenth Step, Configuration ← Prev

 
Back to top
tutorials/todolist-11.txt · Last modified: 2008/01/31 04:53 by reacher