gitlab-org--gitlab-foss/app/presenters
2019-08-20 10:52:21 +00:00
..
blobs Use ActiveModel's type instead of virtus 2019-08-20 10:52:21 +00:00
ci Expose ci_default_git_depth via project API 2019-06-12 09:51:45 +01:00
clusters Add changelog entry 2019-05-27 08:23:17 +00:00
conversational_development_index
projects/settings Speed up loading and filtering deploy keys and their projects 2019-08-05 06:42:34 -07:00
award_emoji_presenter.rb GraphQL mutations for add, remove and toggle emoji 2019-06-28 12:03:33 +12:00
blob_presenter.rb Revert "Merge branch '65152-selective-highlight' into 'master'" 2019-08-09 00:13:09 +00:00
clusterable_presenter.rb Refactor: model errors for multi cluster validation 2019-07-11 17:22:58 +10:00
commit_presenter.rb Added commit type to tree GraphQL type 2019-06-28 08:30:29 +01:00
commit_status_presenter.rb Create framework for build prerequisites 2019-03-20 12:04:40 +11:00
generic_commit_status_presenter.rb
group_clusterable_presenter.rb Remove duplicate clusterable presenter method 2019-05-07 08:49:27 +12:00
group_member_presenter.rb
instance_clusterable_presenter.rb Instance level kubernetes clusters admin 2019-05-07 08:37:03 +12:00
issue_presenter.rb Added common fields to the IssueType 2019-05-31 13:19:29 -05:00
label_presenter.rb Fix display of promote to group label 2019-05-28 10:05:20 +00:00
member_presenter.rb Reconcile CE and EE differences in members/_member.html.haml 2019-06-01 22:19:22 -07:00
members_presenter.rb
merge_request_presenter.rb Split MR widget into cached and non-cached serializers 2019-08-09 21:01:55 +00:00
project_clusterable_presenter.rb Remove duplicate clusterable presenter method 2019-05-07 08:49:27 +12:00
project_member_presenter.rb
project_presenter.rb Improvements to Project overview UI 2019-04-19 13:02:35 +00:00
README.md
tree_entry_presenter.rb Add web_url to tree entry in GraphQL API 2019-06-05 08:46:32 +01:00
user_presenter.rb Suggests issues when typing title 2018-11-27 15:10:40 +00:00

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-ce/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 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 as texts, and URLs using URL helpers from Gitlab::Routing but nothing much more fancy.

Implementation

Presenter definition

Every presenter should inherit from Gitlab::View::Presenter::Simple, which provides a .presents 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