Chapter 7
It looks like we are now going to work on what the customer sees in our app.
Page 71. I generate the Store controller. I fire up my browser, and the default index screen shows up.
Page 72. I add:
@products = Product.salable_items
to the index method in store_controller.rb.
Page 73. We now come to the first example of how KirbyBase differs significantly from other Rails backends. The book wants me to add the following method to the product.rb model:
def self.salable_itemsfind(:all,:conditions => "date_available <= now()",:order => "date_available desc")end
The above expression uses the MySQL now() function to get the current date and time. Since KirbyBase allows you to use any Ruby expression in your query, I can easily rewrite the above method to work with KirbyBase like this:
def self.salable_itemsfind(:all,:conditions => "date_available <= Time.now",:order => "date_available desc")end
Notice that all I had to change was MySQL’s now() function to a call to Ruby’s Time.now method.
An alternative implementation of this method would be to make it more KirbyBase-esque. Since KirbyBase uses actual Ruby expressions as it’s query language, you can pass a block to KirbyBase as your query. Assaph has worked hard on Ackbar to make it easy to use KirbyBase from within Rails. Because of this, I could have rewritten the salable_items method like so:
def self.salable_itemsfind(:all, :order => "date_available desc") { |rec|rec.date_available <= Time.now }end
Either way, it works!
I add the code from the book to views/store/index.rhtml. I switch back to my browser. Looks good. The records I have added show up in descending date order. I add another record with a future date, go back to the index screen and the future dated record does not show up. Looks like the salable_items method is working.
Page 75. Time to clean up the look of the store index. I add the code for views/layouts/store.rhtml. I tidy up views/store/index.rhtml.
Page 76. I add depot.css to public/stylesheets. Hit refresh on my browser and now my online store looks a lot better.
That’s it for Chapter 7. This was an easy one.
No Comments Yet
Be the first to comment!