How to share methods between presenters in Ruby on Rails 4

Asked

Viewed 64 times

0

I would like to add methods to my model through a Presenter, but some methods are shared among other presenters.

Ex: os métodos 'pode_ser_mostrado?', 'pode_ser_editado?', 'pode_ser_excluido?'  

What is the best way, create a presenter base to be extended by others? , or create a mixin included by presenters?

Example:

class ModeloPresenter < BasePresenter
end

or

class ModeloPresenter
  include 'base_presenter'
end

In case how to implement?

Example of how to use methods:

<%= link_to 'Mostrar', show_usuario(usuario.id) if UsuarioPresenter.pode_ser_mostrado? %>

<%= link_to 'Mostrar', show_cliente(cliente.id) if ClientePresenter.pode_ser_mostrado? %>

<%= link_to 'Mostrar', show_local(local.id) if LocalPresenter.pode_ser_mostrado? %>

I also accept if there are suggestions for better ways to implement the same.

1 answer

0

Easy, easy:

class MeuModelo << ActiveRecord::Base
  def pode_ser_mostrado?
    # implementação do método
  end
end
  • 1

    Can better elaborate the answer, if it really solves the OP problem?

  • see if it’s better explained now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.