debian-mirror-gitlab/app/presenters
2020-06-23 00:09:42 +05:30
..
blobs New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
ci New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
clusters New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
dev_ops_score New upstream version 12.6.1 2020-01-01 13:55:28 +05:30
gitlab New upstream version 13.1.0 2020-06-23 00:09:42 +05:30
projects New upstream version 13.1.0 2020-06-23 00:09:42 +05:30
releases New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
award_emoji_presenter.rb New upstream version 12.1.11 2019-09-30 21:07:59 +05:30
blob_presenter.rb New upstream version 12.8.6 2020-03-13 15:44:24 +05:30
clusterable_presenter.rb New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
commit_presenter.rb New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
commit_status_presenter.rb New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
event_presenter.rb New upstream version 12.6.6 2020-02-01 01:16:34 +05:30
generic_commit_status_presenter.rb New upstream version 11.2.8+dfsg 2018-11-18 11:00:15 +05:30
group_clusterable_presenter.rb New upstream version 12.6.1 2020-01-01 13:55:28 +05:30
group_member_presenter.rb New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
instance_clusterable_presenter.rb New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
issue_presenter.rb New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
label_presenter.rb New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
member_presenter.rb New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
members_presenter.rb New upstream version 11.2.8+dfsg 2018-11-18 11:00:15 +05:30
merge_request_presenter.rb New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
milestone_presenter.rb New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
pages_domain_presenter.rb New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
project_clusterable_presenter.rb New upstream version 12.6.1 2020-01-01 13:55:28 +05:30
project_hook_presenter.rb New upstream version 12.9.2 2020-04-08 14:13:33 +05:30
project_member_presenter.rb New upstream version 12.3.8 2019-12-04 20:38:33 +05:30
project_presenter.rb New upstream version 12.10.0 2020-04-22 19:07:51 +05:30
README.md New upstream version 13.0.0 2020-05-24 23:13:21 +05:30
release_presenter.rb New upstream version 13.1.0 2020-06-23 00:09:42 +05:30
sentry_error_presenter.rb New upstream version 12.8.5 2020-03-09 13:42:32 +05:30
service_hook_presenter.rb New upstream version 12.9.2 2020-04-08 18:37:08 +05:30
snippet_blob_presenter.rb New upstream version 12.9.2 2020-04-08 14:13:33 +05:30
snippet_presenter.rb New upstream version 13.1.0 2020-06-23 00:09:42 +05:30
todo_presenter.rb New upstream version 12.5.4 2019-12-26 22:10:19 +05:30
tree_entry_presenter.rb New upstream version 12.0.8 2019-09-04 21:01:54 +05:30
user_presenter.rb New upstream version 11.7.5 2019-02-15 15:39:39 +05:30
web_hook_log_presenter.rb New upstream version 12.6.1 2020-01-01 13:55:28 +05:30

Presenters

This type of class is responsible for giving the view an object which defines view-related logic/data methods. It is usually useful to extract such methods from models to presenters.

When to use a presenter?

When your view is full of logic

When your view is full of logic (if, else, select on arrays, etc.), it's time to create a presenter!

When your model has a lot of view-related logic/data methods, you can easily move them to a presenter.

Why are we using presenters instead of helpers?

We don't use presenters to generate complex view output that would rely on helpers.

Presenters should be used for:

  • Data and logic methods that can be pulled & combined into single methods from view. This can include loops extracted from views too. A good example is https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/7073/diffs.
  • Data and logic methods that can be pulled from models.
  • Simple text output methods: it's ok if the method returns a string, but not a whole DOM element for which we'd need HAML, a view context, helpers, etc.

Why use presenters instead of model concerns?

We should strive to follow the single-responsibility principle and view-related logic/data methods are definitely not the responsibility of models!

Another reason is as follows:

Avoid using concerns and use presenters instead. Why? After all, concerns seem to be a core part of Rails and can DRY up code when shared among multiple models. Nonetheless, the main issue is that concerns dont make the model object more cohesive. The code is just better organized. In other words, theres no real change to the API of the model.

https://www.toptal.com/ruby-on-rails/decoupling-rails-components

Benefits

By moving pure view-related logic/data methods from models & views to presenters, we gain the following benefits:

  • rules are more explicit and centralized in the presenter => improves security
  • testing is easier and faster as presenters are Plain Old Ruby Object (PORO)
  • views are more readable and maintainable
  • decreases the number of CE -> EE merge conflicts since code is in separate files
  • moves the conflicts from views (not always obvious) to presenters (a lot easier to resolve)

What not to do with presenters?

  • Don't use helpers in presenters. Presenters are not aware of the view context.
  • Don't generate complex DOM elements, forms, etc. with presenters. Presenters can return simple data like texts, and URLs using URL helpers from Gitlab::Routing but nothing much fancier.

Implementation

Presenter definition

Every presenter should inherit from Gitlab::View::Presenter::Simple, which provides a .presents the method which allows you to define an accessor for the presented object. It also includes common helpers like Gitlab::Routing and Gitlab::Allowable.

class LabelPresenter < Gitlab::View::Presenter::Simple
  presents :label

  def text_color
    label.color.to_s
  end

  def to_partial_path
    'projects/labels/show'
  end
end

In some cases, it can be more practical to transparently delegate all missing method calls to the presented object, in these cases, you can make your presenter inherit from Gitlab::View::Presenter::Delegated:

class LabelPresenter < Gitlab::View::Presenter::Delegated
  presents :label

  def text_color
    # color is delegated to label
    color.to_s
  end

  def to_partial_path
    'projects/labels/show'
  end
end

Presenter instantiation

Instantiation must be done via the Gitlab::View::Presenter::Factory class which detects the presenter based on the presented subject's class.

class Projects::LabelsController < Projects::ApplicationController
  def edit
    @label = Gitlab::View::Presenter::Factory
      .new(@label, current_user: current_user)
      .fabricate!
  end
end

You can also include the Presentable concern in the model:

class Label
  include Presentable
end

and then in the controller:

class Projects::LabelsController < Projects::ApplicationController
  def edit
    @label = @label.present(current_user: current_user)
  end
end

Presenter usage

%div{ class: @label.text_color }
  = render partial: @label, label: @label

You can also present the model in the view:

- label = @label.present(current_user: current_user)

%div{ class: label.text_color }
  = render partial: label, label: label