brown bag: oo design

  • dependency inversion: depend on abstractions, don’t depend on concretions.
  • Liskov substitution: don’t violate superclass by using is_kind_of; they should return the same type

Test

  • does it have one responsibility?
  • it is DRY?
  • does everything in it changes at the same rate?
  • is it open for extension but not for modification?
  • does it depend on things that change less often than it does?

REST

representational state transfer

  • its idea is to use HTTP to make calls between machines to get resources
  • return the representation of the resource (i.e., the content type of the resource), not the resource itself.
  • resources :actions is equivalent of defining four name routes to 7 controller actions: index, show, create, update, delete, edit, new
  • both new and edit use GET method
  • index action is usually show plural/collection resource, so is create
  • resouces is actions for members, not collections, but you can add collections inside the resource loop.
  • Example to know the difference between representation and resource == representation: form submission, resources: xml string, html files
  • one URI corresponds to one resource; resource can be any information, one picture, one paragraph, one video
  • representation is realized through respond_to

Learn REST

Routing

  • URL mapping to action by routing in config/route.rb
  • the routing rule created first has higher priority
  • “rake routes” show all your routes
  • “rake routes CONTROLLER=products” show all your routes in productions controller
  • limit http method used to access a route match ‘products/:id’ => ‘products#show’, :via => post which is equivalent to  get ‘products/:id’ => ‘products#show’
  • name a route by :as => ‘my_route_name’ so that you can link_to “MY ROUTE”, my_route_name_path
  • specify a parameter in a link: link_to “MY ROUTE”, my_route_name_path(:id=>item.id)
  • parameter can be objects, which is transformed to a hash by object.to_param in the model

Log

Most programming context in Rails (MVC) have a logger attribute

  • view: <%= debug @user %>
  • controller: logger.debug "The object is #{@object}"
  • controller: return render :text => "The object is #{@object}" The render :text call will dump the text to the screen and halt execution of the current action.
  • Model: logger.debug

How can we watch the log in development

  • look at log/development.log
  • tail -f app_root/log/development.log which will scroll the log as it gets stuff appended to it.
  • -f means monitor a file, which will display the last 10 lines and append new lines to the display as new lines are added to the file
  • to interrupt tail while monitoring, use ctrl+C
  • less -R file -> interpret the color codes and escape sequences.

Debug rails applications

FasterCSV has nothing to do with Rails framework. Is a Ruby library.