drapergem / draper

Decorators/View-Models for Rails Applications
MIT License
5.22k stars 527 forks source link

Create unique context for each item in a collection? #883

Closed samstickland closed 2 weeks ago

samstickland commented 4 years ago

We have an unusual use case where having this functionality would be useful to us:

module Draper
  class CollectionDecorator
    module UniqueContext
      def decorate_item(item)
        item_decorator.call(item, context: context.clone)
      end
    end

    prepend UniqueContext
  end
end

As you can see we've currently monkey patched. Would there be any interest in a PR?

Alexander-Senko commented 2 weeks ago

We have an unusual use cache…

I agree that it's not a common case. That's why pulling this in seems unreasonable. It will slow down every one else's code and increase memory footprint for no reason.

Moreover, a context that's not shared seems counter intuitive for me 🤔 It looks like you want it to be modified on per item basis that makes it something more than a collection context, IMO.

samstickland commented 2 weeks ago

That's a fair comment. It's been over 4 years since I raised this and with four more years of experience mutating the context doesn't seem right to me either