JackHQ

0 notes

Rails Best Practices - CodeSchool

Our entire team had the opportunity to complete the Rails Best Practices course from Code School. The course covered topics on all three components of the MVC architecture in Rails. The following are the most useful tips I gathered from each:

Models

attr_accessible: A well-known technique for locking down your model attributes, but worth the reminder… why not begin coding each model by setting the attr_accessible attributes? It can be especially scary to incorporate attr_accessible into a model that has been in production for a long time without it. Unless of course you have proper test coverage :)

scopes: Demonstrated the proper use of scopes for reusable query snippets. ARel was such an important addition to Rails that I couldn’t imagine reverting to finders with conditions. At JRS we had a heated debate about scopes verses class methods for creating reusable query snippets and the consensus was that whatever you choose, you should be consistent throughout your application. That being said, the majority of our team prefers class methods.

Views

databaseless forms: the folks at Code School demonstrated a nifty trick forms that don’t correspond to tables in a database. Developers occasionally paint themselves into a corner with their framework, but the simplicity of Ruby and the modularity of Rails, make non-standard tasks such as these trivial and intuitive. Simply mixing into a bare Ruby class, ActiveModel::Validations (for form validations, duh) and ActiveModel::Conversion (for converting form params to model instances and vice versa), provides all of the tools needed to create a form that behaves similarly as a table backed form.

content_tag: a useful method for generating small snippets of HTML from a helper method. Lately it seems there has been some backlash in the Rails community against helpers, in my opinion this is the result of the recent change in Rails to include all helpers in every view by default. I believe this decision encouraged poor organization of code and separation of responsibility. So assuming we agree that helper methods still have their place, the content_tag method is useful for generating HTML via pure Ruby without concatenating overly escaped strings.

Controllers

presenters: having implemented an action using presenters, I can report that there was a sense of satisfaction in removing bloat from our controllers, however, I had the feeling that we sacrificed some readability of our code, but only slightly.

%h3= @team.name

%ul= render @members

just seems to read better to me than:

%h3= @team_presenter.name

%ul= render @team_presenter.members

but that is really nitpicking. The advantages provided by code brevity and separation of responsibility are evident. I will be using presenters going forward whenever my controllers evolve beyond reasonable complexity.

Overall, Rails Best Practices provided a number of useful tips, a nice refresher on some common Rails conventions and plenty of