<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>app/models/quiz.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -6,19 +6,15 @@ class QuizController &lt; ApplicationController
   def create
     page = Page.find(params[:page_id])
     config = config(page)
-    required = config[:questions]
-    results = config[:results].collect{|k,v| [k.to_i,&quot;#{page.url}#{v}&quot;]}.sort_by{|e| e.first}
-    if params[:questions] and required.size == params[:questions].size
-      total = params[:questions].collect{|k,v| v.to_i}.sum
-      result = (results.detect{|k,v| k &gt;= total} || results.last)
-      redirect_to result.last
+    quiz = Quiz.new(config, params[:questions])
+    if quiz.valid?
+      redirect_to &quot;#{page.url}#{quiz.result}&quot;
+    elsif page.published?
+      page.last_quiz = quiz
+      page.request, page.response = request, response
+      render :text =&gt; page.render
     else
-      if page.published?
-        page.request, page.response = request, response
-        render :text =&gt; page.render
-      else
-        render :text =&gt; %(Quiz not found.)
-      end
+      render :text =&gt; %(Quiz not found.)
     end
   end
   </diff>
      <filename>app/controllers/quiz_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -50,7 +50,7 @@ module QuizTags
   tag &quot;quiz:questions:each&quot; do |tag|
     result = []
     tag.locals.quiz_config['questions'].each_with_index do |q,i|
-      tag.locals.question_name = &quot;question_#{i}&quot;
+      tag.locals.question_name = &quot;question_#{i+1}&quot;
       tag.locals.question_text = q[0]
       tag.locals.question_options = q[1..-1]
       result &lt;&lt; tag.expand
@@ -59,26 +59,26 @@ module QuizTags
   end
   
   tag &quot;quiz:error&quot; do |tag|
-    result = nil
-    params = tag.locals.page.request.parameters
-    if required = params['required']
-      if tag.locals.question_name
-        if !params['questions'] or !params['questions'][tag.locals.question_name]
-          result = tag.expand
-        end
-      else
-        if !params['questions'] or params['questions'].size != required.size
-          result = tag.expand
+    quiz = tag.locals.page.last_quiz
+    if quiz
+      if name = tag.locals.question_name 
+        if quiz.errors.on(name)
+          tag.expand
         end
+      elsif !quiz.valid?
+        result = tag.expand
       end
     end
-    result
   end
   
   tag &quot;quiz:question&quot; do |tag|
     tag.locals.question_text
   end
   
+  tag &quot;quiz:question_name&quot; do |tag|
+    tag.locals.question_name
+  end
+  
   tag &quot;quiz:options&quot; do |tag|
     tag.expand
   end</diff>
      <filename>lib/quiz_tags.rb</filename>
    </modified>
    <modified>
      <diff>@@ -8,7 +8,10 @@ class QuizExtension &lt; Radiant::Extension
   end
   
   def activate
-    Page.send :include, QuizTags
+    Page.class_eval do
+      include QuizTags
+      attr_accessor :last_quiz
+    end
   end
   
   def deactivate</diff>
      <filename>quiz_extension.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,42 +1,68 @@
 require File.dirname(__FILE__) + '/../test_helper'
 
 class QuizTest &lt; ActionController::IntegrationTest
-  def test_quiz_basics
-    page = Page.create!(:title =&gt; 'Quiz!', :slug =&gt; '/', :breadcrumb =&gt; 'home', :status =&gt; Status[:published])
+  def setup
+    @page = Page.create!(:title =&gt; 'Quiz!', :slug =&gt; '/', :breadcrumb =&gt; 'home', :status =&gt; Status[:published])
+    PagePart.create!(:name =&gt; 'quiz', :page =&gt; @page, :content =&gt; quiz_config)
+    PagePart.create!(:name =&gt; 'body', :page =&gt; @page, :content =&gt; quiz_body)
 
-    quiz_part = PagePart.create!(:name =&gt; 'quiz', :page =&gt; page, :content =&gt; quiz_config)
-    body_part = PagePart.create!(:name =&gt; 'body', :page =&gt; page, :content =&gt; quiz_body)
-    
+    @url = %(/pages/#{@page.id}/quiz)
+  end
+  
+  def test_quiz_basics
     get '/'
-    assert_select %(form[action=/pages/#{page.id}/quiz][method=post]) do
-      assert_select %(input[name='questions[question_0]'][value=1])
-      assert_select %(input[name='questions[question_0]'][value=2])
-      assert_select %(input[name='questions[question_0]'][value=3])
+    assert_select %(form[action=#{@url}][method=post]) do
+      assert_select %(input[name='questions[question_1]'][value=1])
+      assert_select %(input[name='questions[question_1]'][value=2])
+      assert_select %(input[name='questions[question_1]'][value=3])
     end
     
-    url = %(/pages/#{page.id}/quiz)
-    post url, :questions =&gt; {:question_0 =&gt; '1', :question_1 =&gt; '1', :question_2 =&gt; '1'}
-    puts html_document.root
+    post @url, :questions =&gt; {:question_1 =&gt; '1', :question_2 =&gt; '1', :question_3 =&gt; '1'}
     assert_redirected_to '/result1'
 
-    post url, :questions =&gt; {:question_0 =&gt; '1', :question_1 =&gt; '1', :question_2 =&gt; '2'}
+    post @url, :questions =&gt; {:question_1 =&gt; '1', :question_2 =&gt; '1', :question_3 =&gt; '2'}
     assert_redirected_to '/result2'
 
-    post url, :questions =&gt; {:question_0 =&gt; '1', :question_1 =&gt; '2', :question_2 =&gt; '2'}
+    post @url, :questions =&gt; {:question_1 =&gt; '1', :question_2 =&gt; '2', :question_3 =&gt; '2'}
     assert_redirected_to '/result2'
 
-    post url, :questions =&gt; {:question_0 =&gt; '1', :question_1 =&gt; '2', :question_2 =&gt; '3'}
+    post @url, :questions =&gt; {:question_1 =&gt; '1', :question_2 =&gt; '2', :question_3 =&gt; '3'}
     assert_redirected_to '/result3'
 
-    post url, :questions =&gt; {:question_0 =&gt; '3', :question_1 =&gt; '3', :question_2 =&gt; '3'}
+    post @url, :questions =&gt; {:question_1 =&gt; '3', :question_2 =&gt; '3', :question_3 =&gt; '3'}
     assert_redirected_to '/result3'
   end
   
+  def test_error_handling
+    post @url
+    assert_response :success
+    assert_select '#error'
+    assert_select '#error_on_question_1'
+    assert_select '#error_on_question_2'
+    assert_select '#error_on_question_3'
+    
+    post @url, :questions =&gt; {:question_1 =&gt; '1'}
+    assert_response :success
+    assert_select '#error'
+    assert_select '#error_on_question_1', false
+    assert_select '#error_on_question_2'
+    assert_select '#error_on_question_3'
+    
+    post @url, :questions =&gt; {:question_1 =&gt; '1', :question_2 =&gt; '1', :question_3 =&gt; '1'}
+    assert_response :redirect
+    assert_select '#error', false
+    assert_select '#error_on_question_1', false
+    assert_select '#error_on_question_2', false
+    assert_select '#error_on_question_3', false
+  end
+  
   def quiz_body
     return &lt;&lt;-BODY.chomp
 &lt;r:quiz:form&gt;
+  &lt;r:error&gt;&lt;p id=&quot;error&quot;&gt;&lt;/p&gt;&lt;/r:error&gt;
   &lt;r:questions:each&gt;
     &lt;h2&gt;&lt;r:question /&gt;&lt;/h2&gt;
+    &lt;r:error&gt;&lt;p id=&quot;error_on_&lt;r:question_name /&gt;&quot;&gt;&lt;/p&gt;&lt;/r:error&gt;
     &lt;ol&gt;
       &lt;r:options:each&gt;
         &lt;li&gt;&lt;r:option_radio /&gt; &lt;r:option_label /&gt;&lt;/li&gt;</diff>
      <filename>test/integration/quiz_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>30230d941afa61605f63f920f62dfe6915582734</id>
    </parent>
  </parents>
  <author>
    <name>Nathaniel Talbott</name>
    <email>nathaniel@terralien.com</email>
  </author>
  <url>http://github.com/ntalbott/radiant-quiz-extension/commit/00d07d8b319dc58b1ca712564106f67bb2f0dfa7</url>
  <id>00d07d8b319dc58b1ca712564106f67bb2f0dfa7</id>
  <committed-date>2008-06-11T14:19:01-07:00</committed-date>
  <authored-date>2008-06-11T14:19:01-07:00</authored-date>
  <message>Added test for error handling, and refactored how it is handled.</message>
  <tree>e1c5772d7b957e16a8dbb220d8827c9f02095ec0</tree>
  <committer>
    <name>Nathaniel Talbott</name>
    <email>nathaniel@terralien.com</email>
  </committer>
</commit>
