0

Many presenters, have something of the form of

class MyClassPresenter < SimpleDelegator
  def something_extra
  end
end

view_obj = MyClassPresenter.new my_class_instance

I want to transverse the instance:

view_obj.nested_obj.nested_obj.value

This means creating multiple presentation objects, which in effect start just copying the models.

class MyClassPresenter < SimpleDelegator
  def something_extra
  end

  def nest_obj
    AnotherPresenter.new(__get_obj__.nest_obj)
  end
end

To demonstrate a real world example a bit better

class UserPresenter < SimpleDelegator
  def home_page_url
    "/home/#{__get_obj__.id}"
  end
end

class MyController < ApplicationController
  def show
    @user = UserPresenter.new(current_user) # devise's current_user
  end
end

/my/show.html.slim

  /! Show profile comments
  - for comment in @user.profile.comments
    | Comment on:
    = comment.created_at
    = comment.content

The main object being passed in is @user, however, the presenter doesn't cover that far. I could create @comments, but I would like the code to be more flexible to however the front-end engineers wanted to take it.

How have other people handled multiple layers for a presenter?

Would the code look something like this? (ugh)

  /! Show profile comments
  - for comment in @user.profile.comments
    - display_comment = CommentPresenter.new(comment)
    | Comment on:
    = display_comment.comment.created_at
    = display_comment.content

-daniel

Daniel
  • 7,006
  • 7
  • 43
  • 49
  • I'm having a hard time understanding what you actually want to do and what you're dealing with. – Jonathan Allard Sep 28 '14 at 21:06
  • @JonathanAllard Most presenter classes just handle a flat record. But how should a presenter class handle a model that has other models (and perhaps again) embeded in the model to be presented? – Daniel Nov 20 '14 at 22:33
  • I don't get why you're using "presenters" at all. I've never seen that. Why not just use the model attributes and use Helpers for more complex formulations? Are you sure this is the "Rails/Ruby way"? – Jonathan Allard Nov 20 '14 at 23:43
  • I have had some success with using presenters. (Major savings during a front end refactor). I'm not fond of putting business logic into views (or helpers). However, more complex presenters as above is difficult. I've been resolving things with https://github.com/apotonick/cells cells. – Daniel Nov 21 '14 at 00:04

0 Answers0