Add ability to specify specific struct class for auto_struct

An issue that has come up fairly regularly for us is that structs will map to an unintended classes when using auto_struct. One such case in when an association is aliased. Something like

# we want it to map to this
class PostCategory
end

class Post < ROM::Relation[:posts]
  schema do
    associations do
      has_one :post_category, as: :category
    end
  end
end

# the hacky fix
Category = PostCategory

What do you think about offering an option like: entity_implementation PostCategory? I think this would be much more predictable and won’t fail in confusing ways. Thoughts?

hey @ianks! We have a similar thread over here Aliased association doesn't map to the entity of the relation please chime in there. We could add this feature in 5.1.0 because we’re wrapping up 5.0.0 this week.

Sorry for the late reply, busy these last few weeks.

After thinking about this a little more, I think we definitely should add a way to ensure a proper struct/entity is generated. I see two options:

  1. Specify the exact class that should be inherited from when building the struct class.

  2. And/or specify the behavior the resulting struct should have by allowing modules to be included when building the class.

So something like:

class Product < ROM::Relation[:sql]
  decorate(
    AttachedFile[:warranty],
    Reviewable,
    Categorizable
  )
  # ...
end

This would be nice because it makes it much less tedious to setup commonly used data access methods, without having to drop into another file/etc. I often find myself hopping around to do this when all I really need is some simple behavior.

I think having the ability to do both 1 and 2 would be a huge win :slight_smile: