Chapter 10
Looks like I’m tackling shipping in this chapter.
Page 116. I need to add a “shipped_at” column to the orders table. So I open up create_db.rb and modify the create orders table code:
db.create_table(:orders,:name, :String,:email, :String,:address, :String,:pay_type, :String,:shipped_at, :Time)
Notice that the MySQL datatype datetime maps to the KirbyBase datatype :Time. I learned this lesson in an earlier chapter.
I now rerun create_db.rb to recreate the tables. As the book mentions, I’m tired of having to re-enter product info into the products table every time I re-create it. So, I’m going to follow the book’s lead and create a file called product_data.rb that I can run to repopulate the products table. The big difference is that my file will have Ruby code in it instead of SQL statements, since KirbyBase is pure-Ruby. Here’s what the product_data.rb file looks like:
require 'kirbybase'require 'time'db = KirbyBase.newproducts_tbl = db.get_table(:products)products_tbl.insert('Pragmatic Project Automation','A really great read!','/images/sk_auto_small.jpg',29.95,Time.parse('2004-12-25 05:00:00'))products_tbl.insert('Pragmatic Version Control','A really controlled read!','/images/sk_svn_small.jpg',29.95,Time.parse('2004-12-25 05:00:00'))
In line 2, I require ‘time’, so that I can use the Time.parse method to easily create Time objects. In line 4, I grab a handle to the products table so that I can easily reference it’s insert method. Notice, also, that since KirbyBase uses Ruby data types, I am passing it an actual Ruby Float object and a Ruby Time object.
I run product_data.rb and my products table is populated!
Page 117. The book creates a pending_shipping method in the Order model that will return all orders that have not been shipped yet. Here’s what the method in the book looks like:
def self.pending_shippingfind(:all, :conditions => "shipped_at is null")end
Instead of “null” KirbyBase uses Ruby’s nil object to signify what “null” means in other database management systems. Therefore, to rewrite the pending_shipping method so that KirbyBase can understand it, I create the method like so:
def self.pending_shippingfind(:all) {|rec| rec.shipped_at.nil?}end
Notice how we use Ruby’s nil? method and enclose the Ruby expression in a block.
Next, I create ship.rhtml.
Page 118. I create the partial template, _order_line.rhtml. I pretty up admin.rhtml.
Page 119. I refresh my browser and the “Orders to be Shipped” screen appears in all it’s glory.
Page 122 I implement the ship, do_shipping, and pluralize methods in admin_controller.rb. I add #mark_as_shipped to order.rb. I don’t need to change anything in these method. They work exactly the way they do in the book. I go back to my browser and mark one of the orders as shipped. The order disappears and the flash message says “One order marked as shipped”.
Just to be sure, I go open up orders.tbl. There, in the first record’s :shipped_at field, is “Wed Feb 22 15:25:44 EST 2006″. Ackbar’s still going strong!
On to the final chapter.
Assaph Mehr replied:
Actually you could have left the
:conditions => “shipped_at is null”
as is, and it would have been translated to
rec.shipped_at == nil
Not quite idiomatic Ruby, but it does the job. It goes this way because it is a two step process (IS -> == and NULL -> nil).
February 23, 2006 at 1:10 pm. Permalink.
houseonfire replied:
You are correct, with one small caveat. To get it to work, you have to make sure “is” and “null” are both upper case. So, the condition string would need to look like this:
“shipped_at IS NULL”
I think a simple change to the SQL_FRAGMENT_TRANSLATIONS regexp will fix this. I’ll email you a fix.
February 23, 2006 at 7:29 pm. Permalink.