About the presenter template in rails. Is this the best way to do this?

In my model:

def presenter
   @presenter ||= ProfilePresenter.new(self)
   @presenter
end

The Presenter profile is a class that has methods such as get_link (), get_img_url (size), get_sex (), get_relationship_status (), and other methods that are not associated with the model, even the controller, but are used for some time in the view.

So now I use them by doing this:

Profile.presenter.get_link
# or
Profile.presenter.get_img_url('thumb') # returns the path of the image. is not used to make a db query

I sincerely think that I missed the real concept of the speakers .. but this is what I'm trying to archive, what can I call it?

+5
source share
1 answer

Typically, these things are handled using helper methods, such as:

def profile_link(profile)
  profile.link ? content_tag(:a, h(profile.name), :href => profile.link) : 'No profile'
end

, Presenter, . , anti-OO.

Presenter Rails Rails, , , , , .

, - :

class ProfilePresenter
  def initialize(view, profile)
    @profile = profile
    @view = view

    yield(self) if (block_given?)
  end

  def link
    @profile.link ? @view.content_tag(:a, @view.h(profile.name), :href => @profile.link) : 'No profile'
  end

  def method_missing(*args)
    @profile.send(*args)
  end
end

- :

<% ProfilePresenter.new(self, @profile) do |profile| %>
<div><%= profile.link %>
<% end %>

, , - , :

def presenter_for(model)
  "#{model.class}Presenter".constantize.new(self, model) do |presenter|
    yield(presenter) if (block_given?)
  end
end

, :

<% presenter_for(@profile) do |profile| %>
<div><%= profile.link %>
<% end %>
+16

All Articles