proxying activerecord objects for display purposes?

Hi,

Quick software design question that I’m trying to get some clarity on.

Question
I’d like to show Article/OrderArticle with some adapted fields for display purposes. Would a proxy object, assigned in the controller, be a sensible approach? To work on price/kg instead of the full unit e.g. a box of “10kg”. This would adapt unit, unit_quantity, quantity, tolerance, result and price.

Background
I’d like to work with two kinds of units in foodsoft, supplier and foodcoop (for unit_subdivide - #209, foodcoop-adam#101).
Supplier units are shown when working with the supplier (fax, receive), foodcoop units are shown when members interact with the system (ordering, show articles_by/groups, …).
My idea is to have the controller generate a proxy object when foodcoops units are shown.
Perhaps Draper with different decorators for the same object would be a somewhat clean approach. Not sure if Draper is meant for having different decorators for the same object.

I’ve got a working hack that multiplies/divides in the view directly, but that’s not really maintainable.

If you have any thoughts on this, welcome!

Best,

  • Willem

Hi Willem,

I like the draper approach. You could have a UnitDecorator and special SupplierUnitDecorator and UserUnitDecorator or the like that inherit form UnitDecorator. Makes sense to me.

Best,
Manuel

Thanks, Manuel. I’ve tried today. But the group_orders form I have uses
a lot of associations, and I needed to decorate all of them as well.
This became quite a mess, as I tried to use ‘display’ (read: fake) unit
and price information - you can see it here
https://github.com/wvengen/foodsoft/compare/foodcoop-adam:feature-subdivide-adam...feature-subdivide-draper-adam?expand=1.
I’m abandoning that now, and think a data model redesign would be an
approach that brings more clarity (and would fix some other things as
well; see also previous foodsoft-dev message).

  • Willem