Green Bar

Now we upgrade the code to pass the test (and, as usual, throw in a couple more tweaks). Add this to public/stylesheets/application.css:

.wiki_green {  background-color: PaleGreen;  }
.wiki_red   {  background-color: Pink;       }

We translate green and red into legible background colors. Bright colors would irritate programmers trying to capture a bug.

Next, upgrade the @x.strong command to pass in the correct wiki_ style class:

  def format_tuple(hash, ypath)
    key = hash.keys.first.value
    ypath += ':' + key
    color = get_node_string(decode(ypath + ':color'))
    color = 'wiki_' + color unless color.blank?

    @x.strong :class => color, :id => 'title_' + ypath do
...

That turns nodes like test_WikiTestPage pale green, and it identifies the <STRONG> tag, so the next Ajax event can access it.

Now upgrade the find_faults action, and put everything together:

  def find_faults
    yar     = YarWiki.new(params[:page_name])
    y       = YAML.parse_file(yar.yaml_path)
    ypath   = params[:ypath]
    color_ypath = ypath + '//color'
    color   = y.select(color_ypath)

    render :update do |rjs|
      if color.size > 0
        new_color = if params[:transcript] =~ /class=.fault/ then 'red' else
'green' end

        if color.first.value != new_color
          color.first.value = new_color
          File.open(yar.yaml_path, 'w'){|f|  f.write(y.emit)  }
          title_node = 'title_node' + ypath.gsub('//', ':')
          rjs[title_node].className = 'wiki_'+new_color
          color_node = 'node' + color_ypath.gsub('//', ':')
          rjs[color_node].className = new_color
        end
      end
    end
  end

This code reads the YAML, finds the relevant ...

Get Test Driven Ajax (on Rails) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.