Skip to content

Instantly share code, notes, and snippets.

@drhenner
Created January 16, 2014 19:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drhenner/8462170 to your computer and use it in GitHub Desktop.
Save drhenner/8462170 to your computer and use it in GitHub Desktop.
Form Objects vs. Inheritance
# The purpose of this gist is to propose use of inheritance when upgrading objects to have "more functionality".
# For example:
# Lets say you have a `company` model and your project manager purposes a huge pivot to your existing application.
# Now new fields are "required" for the new companies you create. Unfortunately adding validators to the existing
# `company` model will cause things to blow up. You could create a form object for the new forms with form specific
# validators. Several issues are now introduced:
# 1) There can be several form objects for the company. Each with a slight difference in logic.
# 2) Any time you change something with the company model, every form object needs to be looked at.
# Instead you can simply create a new object that inherits from Company...
class FirmCompany < Company
end
# Now the new validators can be added to the `FirmCompany` object. The goal would be to move all the validators to the
# original `Company` object once the old data is cleaned up. In the mean time the application will not blow up while
# the old data is being sanitized.
@amartin857
Copy link

i definitely agree there is a time and place for form objects. i think the problem here was it made the code more complex by repeating validations and logic for simple CRUD actions that had no side effects (unlike something akin to user registration which may trigger notifications, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment