Hello,
I've been giving a lot of thought to building a gem which puts more validation into the controllers and routing and removes it elsewhere. My conclusion was that I didn't need to build a gem, Rails in fact already has everything needed to accomplish this, however - I just need to change the default pattern it provides. I'd like to share my thoughts and receive yours -
route validations, strong_parameters, and ActiveModel are sharing responsibility but are split across framework.
What if the instead of defining a single set of validation rules with a single class inheriting from AR per table, the default was to define several valid persistable-object states per table with a series of ActiveModel Prototypes.
What if setters, getters and validations were mixed in by virtue of these AM Prototype classes (I think default setters/getters are AR defined atm?). Further, the Prototype would guard persistence, like a state machine, since 'create', 'save', 'update/patch', 'destroy' would not be defined/mixed-into every Prototype, only ones appropriate for the action.
Really, all this already exists - for instance, routing allows you to define regex's to match incoming parameters -- but how many people are using this? (that's a real question, I have no idea). Then we have controller level validation with strong_parameters. Then we have model level validation. Why not move all this into some form of Prototype which defines what keys are open, required, what they can, should, should not look like (type checking), etc?
This approach also has one cool side benefit - nicer introspection which can aid front end generators / frameworks.
I'm posting here cause I haven't thought this through fully and when I try to write out a DSL for this I start going off course -- but to my delight a few have touched upon these things in this thread, if I'm reading your thoughts correctly.
- Kunal