Select_append and factories

Hello. I have a Threads relation that has_many :messages. When fetching thread list in the repository, I use select_append to add message_count value to the result. Something like this:

def with_count
  left_join(messages).select_append {
    [integer.count(messages[:thread_id]).as(:message_count)]
  }
end

This works fine when I really use the repository. But in cases when I want to provide just structs of threads to some code, and when I try to use rom-factory, I cannot set the message_count there, because it only lets me set the attributes from the schema. It kind of makes sense, but does not let me achieve what I want.

I end up abusing Ruby like this:

thread = Factory.structs[:thread]
def thread.message_count = 1

Obviously it does not feel right. So I’ve been wondering:

  • Is rom-factory missing functionality to do that?
  • Am I doing something completely against the philosophy of ROM?
  • Or maybe my “workaround” is not really a workaround, but intended way?

Factories are meant to work with relations. You can’t expect a relation factory to work with arbitrary query results at the moment. It would probably work if you defined a relation with a schema matching your current query but that would be probably an overkill. I suspect it should be possible to make factories work against relation views defined via view DSL though. They return relations with predefined schemas and this can be used to infer factories.