ruby-on-railsrubydirectorymvp

What is the Rails Presenters folder for?


What is the Rails Presenters folder for?

What goes in this folder?

Why is this folder needed?


Solution

  • presenters is a design pattern commonly reffered to as Model View Presenter(MVP)

    This is a derivation of the Model View Controller pattern and is used for creating user interfaces.

    It's useful for the Separation of Concerns for making code more DRY.

    Here's how Wikipedia describes it

    model - interface defining the data to be displayed or otherwise acted upon in the user interface.

    presenter - acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.

    view - a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.

    https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    Presenters in Ruby on Rails

    Presenters are simple classes that sit between the model and the view and provide a nice, DRY object oriented way of working with complex display logic.

    In Rails, the convention is for them to be located in the app/presenters folder

    Here is a userful article that explains the pattern and its use in Ruby on Rails.

    https://kpumuk.info/ruby-on-rails/simplifying-your-ruby-on-rails-code/