#############################################################################
##
##  depotC                                        goetz.pfeiffer@nuigalway.ie
##
site = "depot"

from rails import *

print("cd %s" % (site))
os.chdir(site)

actions = ["index"]
controller("store", actions)

commit("generated store controller.")

filename = "config/routes.rb"
line = """\
  root :to => 'store#index', :as => 'store'
"""
updateText(filename, 55, line, 0)

filename = "public/index.html"
os.remove(filename)
execute("git rm %s" % (filename))

commit("modified root of the web site and removed public/index.html.")

filename = "app/controllers/store_controller.rb"
line = """\
    @products = Product.all
"""
updateText(filename, 2, line, 0)

filename = "app/models/product.rb"
line = """\
  default_scope :order => 'title'
"""
updateText(filename, 1, line, 0)

filename = "app/views/store/index.html.erb"
text = """\
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>

<h1>Your Pragmatic Catalog</h1>

<% @products.each do |product| %>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%= product.title %></h3>
    <%= sanitize product.description %>
    <div class="price_line">
      <span class="price"><%= product.price %></span>
    </div>
  </div>
<% end %>
"""
createText(filename, text)

commit("created catalog listing.")

filename = "app/views/layouts/application.html.erb"
text = """\
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag "scaffold" %>
  <%= stylesheet_link_tag "depot", :media => "all" %>
"""
updateText(filename, 3, text, 2)
text = """\
<body id="store">
  <div id="banner">
    <%= image_tag("logo.png") %>
    <%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
      <a href="http://www....">Home</a><br />
      <a href="http://www..../faq">Questions</a><br />
      <a href="http://www..../news">News</a><br />
      <a href="http://www..../contact">Contact</a><br />
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
"""
updateText(filename, 9, text, 4)

filename = "public/stylesheets/depot.css"
text = """\
/* Styles for main page */

#banner {
  background: #9c9;
  padding-top: 10px;
  padding-bottom: 10px;
  border-bottom: 2px solid;
  font: small-caps 40px/40px "Times New Roman", serif;
  color: #282;
  text-align: center;
}

#banner img {
  float: left;
}

#columns {
  background: #141;
}

#main {
  margin-left: 13em;
  padding-top: 4ex;
  padding-left: 2em;
  background: white;
}

#side {
  float: left;
  padding-top: 1em;
  padding-left: 1em;
  padding-bottom: 1em;
  width: 12em;
  background: #141;
}

#side a {
  color: #bfb;
  font-size: small;
}
"""
updateText(filename, 66, text, 0)

commit("layouted catalog page.")

filename = "app/views/store/index.html.erb"
line = """\
      <span class="price"><%= number_to_currency(product.price) %></span>
"""
updateText(filename, 12, line)

commit("format price as currency.")

filename = "test/functional/store_controller_test.rb"
text = """\
    assert_select '#columns #side a', :minimum => 4
    assert_select '#main .entry', 3
    assert_select 'h3', 'Programming Ruby 1.9'
    assert_select '.price', /\$[,\d]+\.\d\d/
"""
updateText(filename, 6, text, 0)

commit("added functional testing of store controller.")

tag("C")
