How to add functionality to a Model that depends on Controller or View

1

I would like to add the following methods to my Models when presented in the View, such as can_be_showed , can_be_edited , can_be_destroyed , among others, however in some cases these methods may depend on Controller , such as depending on the user who is logged in.

I have tried using the Presenter pattern, but I encountered a lot of problems, such as rendering Persenter as a Model:

<%= render my_presenter %>#=render partial: 'my_models/my_model, locals: {my_model: @my_model}

Pass it as a parameter to a routing method:

link_to 'title', edit_my_model(@my_presenter)

Or create a form based on it

form_for(@my_presenter) do |f|

Would anyone have a hint, followed by an example? I would like something that would give me the opportunity to reuse as much code as possible.

    
asked by anonymous 27.01.2015 / 04:31

1 answer

1

My solution would be to create the methods: can_be_showed_by(current_user) can_be_edited_by(current_user) and can_be_destroyed_by(current_user) and thus be able to use the logged in user and could take advantage of the whole code in other routines, since the current_user is only an instance of the User.

    
10.02.2015 / 00:18