Adding features to a Model that depend on the Controller or View

Asked

Viewed 82 times

1

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

I’ve tried using the pattern Presenter, but I found many problems, such as to render the Persenter like 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 any tips, followed by example? , I would like something that would give me the opportunity to reuse as much code as possible.

  • 1

    Have a look at https://github.com/elabs/pundit

  • I heard about Draper Decorator at the Rails 2014 https://github.com/drapergem/draper conference, first I will test this, something I inform the result here

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 that way you can use the logged in user and you can take advantage of all the code in other routines, since current_user is just an instance of User.

Browser other questions tagged

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