Learning Ruby

Reference, Mnemonic & Ramblings

Takeaways From Course 3 - Part 2

These are some key takeaways from the 3rd course for me based on the curriculum as well as the feedback received from the TAs for every module (aka Week).

In Module 3, focus was on learning…
  • to keep controllers skinny
  • ActiveRecord transactions (all or none!)
  • RSpec macros and shared_examples
  • Capybara introduction and first feature spec

Module 3

  • Outside-in development in action with feature-specs in Capybara: Starting with a feature-spec (BDD) and letting it drive the design (models/routes/controllers) seems to be coming a bit more naturally to me than the other way round
  • Single-Assertion Principle can (and should) be violated in feature-specs: not just to DRY up the code, but mainly because feature specs are expensive to run!
  • ActiveRecord transactions was one of the primary learnings when trying to update the video positions in the queue. Got introduced to ACID, thanks to the missus!
  • RSpec shared_examples and macros are very helpful in DRYing up the specs.
  • Got re-exposed to :xpath based search of elements in the view template! DOM tree, XPath, had heard and read about these a long time ago for some thing totally different!

Finer points:

  • QueueItem.find_by(id: params[:id], user: current_user) does not throw a “RecordNotFound” exception in rails. Doing this current_user.queue_items.find(params[:id]) does. Use either knowingly!
  • Do not prefix method names with get_ or set_. Instead come up with better names to indicate what they do.
  • Use method: delete instead of the default get for destroying a record so that the route cannot be bookmarked.
  • Use find_by instead of where(...).first for searching a single record.
  • Prefix self. when assigning value to an instance variable but it is not necessary when using the variable in an instance method.

comments powered by Disqus