I’ve had the Rails AntiPatterns: Best Practice Ruby on Rails Refactoring (Addison-Wesley Professional Ruby Series) book on my Safari Books Online bookshelf for almost a year now, and finally started reading through it. It was “updated for Rails 3”, and there doesn’t appear to be a newer version, so I was a bit skeptical about how much I’d get out of the book.
This book has some overlap with Ruby Science, but it is more Rails-centric in its approach. Going through the book I found quite a few topics that I’ve not really read in-depth on in any other book or come across in a couple years of Rails development experience.
- Use of ActiveSupport::Concern included for specifying validations as part of a module.
- Discussion of Rails plugins (which later were mentioned as falling out of favor)
- serialize :value, Hash vs checkbox/boolean model
- How Rails helps in the building of Semantic HTML
- Gems Clearance and AuthLogic as simple authentication gems.
- MVP pattern and ActivePresenter (looks like Rails 3 was the end?)
- Emphasis on controller as a representation of a resource
- Rails responders for changing respond_to to respond_with(object)
- rescue *EXPECTED_EXCEPTIONS (EXPECTED_EXPECTIONS as a constant array) to whitelist exceptions that are anticipated.
- Mechanize and RestClient as automated interface gems with other sites.
- render :status => :unprocessable_entity (or :created and other symbols that represent HTTP status codes)
- Strategies for testing rake tasks, including FileUtils::NoWrite
- sqlite3 “:memory:” database for gem testing
- Thoughts on when to add DB indexes
- AR association methods count vs. length vs. size and their behavior and resource usage.
- .flatten() as a code smell for ruby code that should be represented as SQL.
- Run rake db:migrate && rake db:migrate:redo for all migrations created.
- Don’t go against AR bias against DB constraints except for NULL & default values.
- Never fail quietly, whether ignoring return values or overzealously catching all exceptions.
- rescue_from on controller to redirect specific exceptions.
I think the general concepts covered provided valuable background and food for thought even if not all the solutions are still the same. I don’t regret the help examining current practices in my daily Rails development.