Cart creation-101217222728-phpapp01


Published on

Presentation give by Alan Hecht.

  • Be the first to comment

  • Be the first to like this

Cart creation-101217222728-phpapp01

  1. 1. Cart Creation<br />
  2. 2. What we’ll cover<br />Using Sessions<br />Public, Private, and Protected Methods in Ruby<br />Relationships between models<br />Active Record Callbacks & Validation<br />Adding a button to a page<br />More Functional Testing<br />
  3. 3. Generate a shopping cart<br />rails generate scaffold cart<br />No foreign keys present<br />Only one of the tables has a foreign key<br />Need to recover the cart with every request<br />
  4. 4. Rails Sessions<br />By default, session info stored in a browser session cookie (4k limit)<br />Database or memcached also available<br />Don’t store data that can easily be outdated<br />Serialized ActiveRecordobjects<br />Cryptographically signed but unencrypted<br />Don’t store critical information solely in the session<br />
  5. 5. Associate the cart with a session<br />app/controllers/application_controller.rb<br />private<br />defcurrent_cart<br />Cart.find(session[:cart_id])<br />rescue ActiveRecord::RecordNotFound<br /> cart = Cart.create<br /> session[:cart_id] =<br /> cart<br /> end<br />
  6. 6. Public, Private, & Protected Methods<br />Different from Java & C#<br />Public methods can be invoked by anyone <br />Private methods accessible by an object from a derived class<br />Protected methods accessible by other objects & objects from derived classes<br />Inheritance doesn’t determine method visibility<br />Private & protected methods visible from subclasses<br />
  7. 7. Storing items in the cart<br />rails generate scaffold line_itemproduct_id:integercart_id:integer<br />By convention, singular of table name appended with “_id”<br />Need to establish relationship in both the database and the model<br />Model declarations add ability to build and navigate the relationship<br />
  8. 8. ActiveRecord relationships<br />One to One<br />has_one :contained<br />belongs_to :container<br />One to Many<br />has_many :contained<br />belongs_to :container<br />Table with foreign keys has the ‘belongs_to’<br />
  9. 9. Add Relationship Specifiers<br />class Cart < ActiveRecord::Base<br />has_many :line_items, :dependent => :destroy<br />end<br />class LineItem < ActiveRecord::Base<br />belongs_to :product<br />belongs_to :cart<br />end<br />class Product < ActiveRecord::Base<br />has_many :line_items<br />end<br />
  10. 10. What if we destroy a cart?<br />Line_items dependent on cart<br />:dependent => :destroy indicates that line_items are destroyed along with the cart<br />destroy involves ActiveRecord validation logic, delete only removes from the database<br />
  11. 11. What if we destroy a product?<br />Make sure it isn’t contained in a cart<br />
  12. 12. ActiveRecord Callbacks & Validations<br />Callbacks invoked before & after operations<br />For example save, delete, create, validate<br />Can view unformatted data in ‘before’ callbacks<br />Dates & Currency<br />Returning the actual value ‘false’ stops the operation<br />Add a handler or define as a method<br />
  13. 13. Add a button<br />Won’t be creating a new controller or a new action<br />Creating an item in the cart, so use line_item controller<br />Looking at generated code in line_item_controller.rb, we’ll use the ‘Create’ action<br />‘Create’ action called using HTTP Post<br />button_to‘button label’, ‘URL Path’, ‘HTML options’<br />Add ‘_path’ to controller to get URL path (i.e. line_item_path)<br />Pass in hash values to URL path as parameters<br />id extracted from ActiveRecord objects passed in<br />app/views/store/index.html.erb<br /><%= button_to ‘Add to Cart’, line_items_path(:product_id => product) %><br />
  14. 14. Modify line_item create<br />By default, create parameter is a serialized line_item object - params[:line_item]<br />Create a line_item based on the product id and add it to the cart<br />
  15. 15. Modify line_item create (cont.)<br />app/controllers/line_items_controller.rb<br />
  16. 16. Update the functional test<br />test/functional/line_items_controller_test.rb<br />assigns() accesses instance variables in the controller being tested<br />
  17. 17. Try it out<br />http://localhost:3000/<br />Cart scaffolding didn’t show any attributes of what was just added<br />
  18. 18. Modify the view<br />app/views/cart/show.html.erb<br /><h2>Your Pragmatic Cart</h2><br /><ul><br /> <% for item in @cart.line_items %><br /> <li><%= item.product.title %></li><br /> <% end %><br /></ul><br />
  19. 19. Try it out (again)<br />http://localhost:3000/<br />Cart now shows what is contained<br />Need to show each item only once (next chapter)<br />
  20. 20. Homework<br />Change the view so that clicking on a book’s image will also add the product to the cart<br />Add a new session variable to record how many times the user accessed the store controller’s index action<br />Add the counter to the template and display it on top of the catalog page<br />Reset the counter to zero when adding to the cart<br />Change template to display counter when > 5<br />