Recently, I had to introduce a concern that applied to multiple models. But this concern needed some specifics on each of the model where it was applied.
This model was Sizeable, it is a concern I use to compute and retrieve the total size of an object in our application. Objects exists in a variety of forms but their size always end up being the sum of the sizes of their defining ActiveStorage blobs. What changes from model to model is the list of blobs that are responsible for the size computation.
What I ended-up doing is this:
- one main concern with all common methods used in the size computation or retrieval
- one sub-concern per model to override the method to list the size-contributing blobs
My problem is about the naming & structure of those files. My main concern should be inside the concerns folder since it apply to many models in my application.
But where should I put the specific sub-concerns for each of my models?
I could put them in a model-specific folder
models/thing/sizeable.rb but I don’t like that. It would distance them too much from the main concern they are dependent of. It also would mean that I would have a lot of indistinguishable
sizeable.rb files in my application, one for every model.
I could put them in a concern sub-folder
models/concerns/sizeable/thing.rb but I don’t like that either. It would mean that I would have a lot of indistinguishable
thing.rb files in my application, potentially one for every concern of the model.
I could put them in a concern sub-folder with a specific name
models/concerns/sizeable/sizeable_thing.rb. This would solve all my file name problems but that appears a little overkill for a file path.
The solution I ended up doing is
models/sizeable/sizeable_thing.rb where I completely discard the
Do you have suggestions for me on a better file structure or concerns organization?