Panther Software

A Developer's Adventures in Coding

Commentable: A Polymorphic Nested Comments Directive for AngularJS

This blog post is based on my experiences developing a Commentable widget (more on this later) first using Backbone.js and more recently, AngularJS.

A Polymorphic Commentable Widget?

To be specific, it is:

  • some Javascript + HTML that allows a website visitor to add a comment on a specific page
  • the commenting functionality is not tied to any specific object. In other words, the same code can be used to comment against an Article, a Cat Photo or any business object without any modification
  • the same code should display previous comments in a nested fashion
  • provide a facility to reply to a comment
  • provide a facility to edit or delete any of your own comments

User Registration From Scratch Using Form Objects in Rails

I recently watched a presentation by Bryan Helmkamp titled Refactoring Fat Models with Patterns. Bryan based his talk on his blog 7 Patterns to Refactor Fat ActiveRecord Models, in which he describes seven patterns used to simplify models and adhere to the Single Responsibility Principle. I highly recommend studying both these resources.

From the patterns Bryan described, the Form Object pattern struck a chord as it seemed to be an elegant solution for a problem I have developed multiple implementations for but never felt completely satisfied with the result. I refer to User Registration and the lesser issue of User Authentication.

Does User Registration Logic Belong in a Model?

IMHO, no because registration/signup is a one-off event for a User yet code responsible for this remains in the User class and must be accounted for whenever a User object is instantiated during testing.

This becomes even more apparent when additional validation could be required during registration that rely on remote services (i.e. lookup the user’s IP against a spammer blacklist). Adding this logic to the User model (be it in a method or ActiveRecord callback) adds external dependencies to the User class which again must be accounted for during testing.

Adding a Contact Us Form to an Octopress Website

Octopress is a static website generator. The advantage of a static website is that no complex backend processes, such as installing application servers, databases or frameworks such Wordpress or Ruby on Rails are required. All that is required is a web server.

Lacking any dynamic interaction with a server does have one shortcoming: Octopress websites are not able to host Contact Us forms or any type of content that requires server side processing.

Luckily, there are several free web services that will generate and host Contact Us forms. One such is Foxyform employs a simple contact us form generator that produces a Javascript snippet that will dynamically create a contact us form on any page. There are posted to Foxyform’s servers, which in turn will forward these to the registered email address.