Skip to content

Add cop to avoid helpers in presenters

Description of the proposal

Add rubocop rule to catch when View Helpers are included in Presenters

        # Enforces that presenters don't include helpers.
        #
        # Presenters should be view-agnostic and not depend on view context.
        # Including helper modules in presenters couples them to the view layer
        # and makes them harder to test and reason about.
        #
        # @example
        #   # bad
        #   class BasePresenter
        #     include DiffHelper
        #
        #     def diffs_slice
        #       @diffs_slice ||= resource.first_diffs_slice(offset, diff_options)
        #     end
        #   end
        #
        # @example
        #   # good
        #   class BasePresenter
        #     attr_reader :diff_options
        #
        #     def initialize(diff_options)
        #       @diff_options = diff_options
        #     end
        #
        #     def diffs_slice
        #       @diffs_slice ||= resource.first_diffs_slice(offset, diff_options)
        #     end
        #   end
        #

Check-list

  • Make sure this MR enables a static analysis check rule for new usage but ignores current offenses.
  • Create a follow-up issue to fix the current offenses as a separate iteration: #551345 (closed)
Edited by Olaoluwa Oluro

Merge request reports

Loading